No longer use `glibtop_machine.h' for Linux.

1998-07-03  Martin baulig  <martin@home-of-linux.org>

	* macros/gnome-libgtop-sysdeps.m4: No longer use
	`glibtop_machine.h' for Linux.

	* sysdeps/linux/glibtop_machine.h: Removed.

	* sysdeps/guile/guile.awk: Using `scm_append'
	instead of `gh_append'.

	* sysdeps/guile/names/guile-names.awk: dito.

	* sysdeps/linux/*.c: Using code from stable branch again.

	* include/glibtop/parameter.h: New file.

	* lib/parameter.c: New file.

	* lib/{open, init}.c: Done some more work on server
	initialization
This commit is contained in:
Martin baulig
1998-07-03 17:19:25 +00:00
committed by Martin Baulig
parent 94715ef718
commit 9532dadc2e
26 changed files with 557 additions and 355 deletions

View File

@@ -1,3 +1,24 @@
1998-07-03 Martin baulig <martin@home-of-linux.org>
* macros/gnome-libgtop-sysdeps.m4: No longer use
`glibtop_machine.h' for Linux.
* sysdeps/linux/glibtop_machine.h: Removed.
* sysdeps/guile/guile.awk: Using `scm_append'
instead of `gh_append'.
* sysdeps/guile/names/guile-names.awk: dito.
* sysdeps/linux/*.c: Using code from stable branch again.
* include/glibtop/parameter.h: New file.
* lib/parameter.c: New file.
* lib/{open, init}.c: Done some more work on server
initialization
1998-06-21 Martin Baulig <martin@home-of-linux.org> 1998-06-21 Martin Baulig <martin@home-of-linux.org>
* perl/*: Added perl interface. * perl/*: Added perl interface.

View File

@@ -26,6 +26,8 @@
#include <glibtop/close.h> #include <glibtop/close.h>
#include <glibtop/xmalloc.h> #include <glibtop/xmalloc.h>
#include <glibtop/parameter.h>
#include <glibtop/union.h> #include <glibtop/union.h>
#include <glibtop/sysdeps.h> #include <glibtop/sysdeps.h>
@@ -38,7 +40,8 @@ main (int argc, char *argv [])
{ {
glibtop_union data; glibtop_union data;
glibtop_sysdeps sysdeps; glibtop_sysdeps sysdeps;
unsigned c, count, i, *ptr; unsigned c, method, count, port, i, *ptr;
char buffer [BUFSIZ];
pid_t pid, ppid; pid_t pid, ppid;
count = PROFILE_COUNT; count = PROFILE_COUNT;
@@ -47,7 +50,23 @@ main (int argc, char *argv [])
bindtextdomain (PACKAGE, GTOPLOCALEDIR); bindtextdomain (PACKAGE, GTOPLOCALEDIR);
textdomain (PACKAGE); textdomain (PACKAGE);
glibtop_init (); glibtop_init_r (&glibtop_global_server, 0, GLIBTOP_INIT_NO_OPEN);
glibtop_get_parameter (GLIBTOP_PARAM_METHOD, &method, sizeof (method));
printf ("Method = %d\n", method);
count = glibtop_get_parameter (GLIBTOP_PARAM_COMMAND, buffer, BUFSIZ);
buffer [count] = 0;
printf ("Command = '%s'\n", buffer);
count = glibtop_get_parameter (GLIBTOP_PARAM_HOST, buffer, BUFSIZ);
buffer [count] = 0;
glibtop_get_parameter (GLIBTOP_PARAM_PORT, &port, sizeof (port));
printf ("Host = '%s' - %u\n\n", buffer, port);
for (c = 0; c < count; c++) for (c = 0; c < count; c++)
glibtop_get_cpu (&data.cpu); glibtop_get_cpu (&data.cpu);

View File

@@ -35,7 +35,8 @@ typedef struct _glibtop glibtop;
struct _glibtop struct _glibtop
{ {
int fd; /* Server file descriptor */ unsigned flags;
unsigned method; /* Server Method */
#ifdef HAVE_GLIBTOP_MACHINE_H #ifdef HAVE_GLIBTOP_MACHINE_H
glibtop_machine machine; /* Machine dependent data */ glibtop_machine machine; /* Machine dependent data */
#endif #endif

View File

@@ -5,4 +5,5 @@ glibtop_HEADERS = close.h loadavg.h prockernel.h procstate.h \
proctime.h shm_limits.h version.h cpu.h msg_limits.h \ proctime.h shm_limits.h version.h cpu.h msg_limits.h \
procmem.h procuid.h swap.h write.h error.h open.h \ procmem.h procuid.h swap.h write.h error.h open.h \
procsegment.h read.h sysdeps.h xmalloc.h global.h \ procsegment.h read.h sysdeps.h xmalloc.h global.h \
output.h procsignal.h read_data.h union.h types.h output.h procsignal.h read_data.h union.h types.h \
parameter.h

View File

@@ -26,8 +26,19 @@
__BEGIN_DECLS __BEGIN_DECLS
#define _GLIBTOP_INIT_STATE_INIT 0x10000
#define _GLIBTOP_INIT_STATE_OPEN 0x20000
#define _GLIBTOP_INIT_STATE_SERVER 0x40000
#define GLIBTOP_INIT_NO_OPEN 1
#define GLIBTOP_INIT_NO_INIT 2
#define GLIBTOP_OPEN_NO_OVERRIDE 1 #define GLIBTOP_OPEN_NO_OVERRIDE 1
#define GLIBTOP_METHOD_DIRECT 1
#define GLIBTOP_METHOD_PIPE 2
#define GLIBTOP_METHOD_INET 3
extern void glibtop_open_l __P((glibtop *, const char *, const unsigned long, const unsigned)); extern void glibtop_open_l __P((glibtop *, const char *, const unsigned long, const unsigned));
extern void glibtop_open_r __P((glibtop *, const char *, const unsigned long, const unsigned)); extern void glibtop_open_r __P((glibtop *, const char *, const unsigned long, const unsigned));

View File

@@ -0,0 +1,42 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Top Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#ifndef __GLIBTOP_PARAMETER_H__
#define __GLIBTOP_PARAMETER_H__
#include <glibtop.h>
#include <glibtop/open.h>
__BEGIN_DECLS
#define GLIBTOP_PARAM_METHOD 1
#define GLIBTOP_PARAM_FEATURES 2
#define GLIBTOP_PARAM_COMMAND 3
#define GLIBTOP_PARAM_HOST 4
#define GLIBTOP_PARAM_PORT 5
#define glibtop_get_parameter(p1,p2,p3) glibtop_get_parameter_l(glibtop_global_server,p1,p2,p3)
#define glibtop_set_parameter(p1,p2,p3) glibtop_set_parameter_l(glibtop_global_server,p1,p2,p3)
extern size_t glibtop_get_parameter_l __P((glibtop *, const unsigned, void *, size_t));
extern void glibtop_set_parameter_l __P((glibtop *, const unsigned, const void *, size_t));
__END_DECLS
#endif

View File

@@ -8,7 +8,8 @@ CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG
lib_LTLIBRARIES = libgtop.la lib_LTLIBRARIES = libgtop.la
libgtop_la_SOURCES = init.c open.c close.c command.c read.c read_data.c write.c lib.c libgtop_la_SOURCES = init.c open.c close.c command.c read.c read_data.c \
write.c lib.c parameter.c
BUILT_SOURCES = lib.c BUILT_SOURCES = lib.c

View File

@@ -20,23 +20,168 @@
Boston, MA 02111-1307, USA. */ Boston, MA 02111-1307, USA. */
#include <glibtop.h> #include <glibtop.h>
#include <glibtop/open.h> #include <glibtop/xmalloc.h>
#include <glibtop/parameter.h>
#ifndef DEFAULT_PORT
#define DEFAULT_PORT 42800
#endif
static glibtop _glibtop_global_server; static glibtop _glibtop_global_server;
glibtop *glibtop_global_server = NULL; glibtop *glibtop_global_server = &_glibtop_global_server;
void
_init_server (glibtop *server, const unsigned features)
{
char *command, *temp;
/* Try to get server command, but don't override if already
* set via glibtop_set_parameter () */
if (server->server_command == NULL) {
const char *temp = getenv ("LIBGTOP_SERVER") ?
getenv ("LIBGTOP_SERVER") : GTOP_SERVER;
server->server_command = glibtop_strdup_r (server, temp);
}
if (server->server_rsh == NULL) {
const char *temp = getenv ("LIBGTOP_RSH") ?
getenv ("LIBGTOP_RSH") : "/usr/bin/ssh";
server->server_rsh = glibtop_strdup_r (server, temp);
}
/* Try to get server method, but don't override if already
* set via glibtop_set_parameter () */
if (server->method) return;
/* If server->command doesn't start with a colon, then it is
* the full pathname of the server executable. */
if (server->server_command [0] != ':') {
if (features & glibtop_server_features) {
/* We really need the server. */
server->method = GLIBTOP_METHOD_PIPE;
} else {
/* Fine. No server is needed, so we call the
* sysdeps functions directly. */
server->method = GLIBTOP_METHOD_DIRECT;
}
return;
}
/* If the first character of 'server_command' is a colon,
* the first field is the method to connect to the server. */
/* Everything up to the next colon is the method. */
command = glibtop_strdup_r (server, server->server_command+1);
temp = strstr (command, ":");
if (temp) *temp = 0;
/* Dispatch method. */
if (!strcmp (command, "direct")) {
/* Use sysdeps dir instead of connecting to server
* even if using the server would be required on
* the current system. */
server->method = GLIBTOP_METHOD_DIRECT;
} else if (!strcmp (command, "inet")) {
server->method = GLIBTOP_METHOD_INET;
/* Connect to internet server. */
if (temp == NULL) {
/* If no value was set, we use 'localhost'. */
if (server->server_host == NULL)
server->server_host = glibtop_strdup_r
(server, "localhost");
} else {
char *temp2 = strstr (temp+1, ":");
if (temp2) *temp2 = 0;
/* Override default. */
if (server->server_host)
glibtop_free_r (server, (char *) server->server_host);
server->server_host = glibtop_strdup_r
(server, temp+1);
temp = temp2;
}
if (temp == NULL) {
/* If no value was set, we use DEFAULT_PORT. */
if (server->server_port == 0)
server->server_port = DEFAULT_PORT;
} else {
char *temp2 = strstr (temp+1, ":");
if (temp2) *temp2 = 0;
if (sscanf (temp+1, "%ld", &server->server_port) != 1)
server->server_port = DEFAULT_PORT;
temp = temp2 ? temp2 + 1 : temp2;
}
} else {
glibtop_error_r (server, "Unknown server method '%s'",
server->server_command+1);
}
glibtop_free_r (server, command);
}
glibtop * glibtop *
glibtop_init_r (glibtop **server, const unsigned long features, glibtop_init_r (glibtop **server_ptr, const unsigned long features,
const unsigned flags) const unsigned flags)
{ {
if (*server != NULL) glibtop *server;
return *server;
if (glibtop_global_server == NULL) { if (server_ptr == NULL)
glibtop_global_server = &_glibtop_global_server; return NULL;
if (*server_ptr == NULL)
*server_ptr = glibtop_global_server;
server = *server_ptr;
/* Should we do the initialization? */
if (flags & GLIBTOP_INIT_NO_INIT)
return server;
/* Do the initialization, but only if not already initialized. */
if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) {
_init_server (server, features);
server->flags |= _GLIBTOP_INIT_STATE_INIT;
}
/* Should we open the server? */
if (flags & GLIBTOP_INIT_NO_OPEN)
return server;
/* Open server, but only if not already opened. */
if ((server->flags & _GLIBTOP_INIT_STATE_OPEN) == 0)
glibtop_open_l (glibtop_global_server, "glibtop", glibtop_open_l (glibtop_global_server, "glibtop",
features, flags); features, flags);
}
return *server = glibtop_global_server; return server;
} }

View File

@@ -4,6 +4,7 @@ BEGIN {
print ""; print "";
print "#include <glibtop.h>"; print "#include <glibtop.h>";
print "#include <glibtop/open.h>";
print "#include <glibtop/sysdeps.h>"; print "#include <glibtop/sysdeps.h>";
print "#include <glibtop/command.h>"; print "#include <glibtop/command.h>";
@@ -29,7 +30,9 @@ function output(feature) {
print "{"; print "{";
print "\tglibtop_init_r (&server, GLIBTOP_SYSDEPS_"toupper(feature)", 0);"; print "\tglibtop_init_r (&server, GLIBTOP_SYSDEPS_"toupper(feature)", 0);";
print ""; print "";
print "\tif (server->features & GLIBTOP_SYSDEPS_"toupper(feature)") {"; print "\tif ((server->flags & _GLIBTOP_INIT_STATE_SERVER) &&";
print "\t (server->features & GLIBTOP_SYSDEPS_"toupper(feature)"))";
print "\t{";
if (feature ~ /^proc_/) { if (feature ~ /^proc_/) {
print "\t\t"prefix"glibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", sizeof (pid_t),"; print "\t\t"prefix"glibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", sizeof (pid_t),";

View File

@@ -33,81 +33,15 @@ void
glibtop_open_l (glibtop *server, const char *program_name, glibtop_open_l (glibtop *server, const char *program_name,
const unsigned long features, const unsigned flags) const unsigned long features, const unsigned flags)
{ {
char version [BUFSIZ], buffer [BUFSIZ], *temp, *temp2;
glibtop_sysdeps sysdeps;
int connect_type, ret;
memset (server, 0, sizeof (glibtop));
server->name = program_name; server->name = program_name;
/* Is the user allowed to override the server ? */ /* It is important to set _GLIBTOP_INIT_STATE_OPEN here when we
* do recursive calls to glibtop_init_r (). */
if (flags & GLIBTOP_OPEN_NO_OVERRIDE) server->flags |= _GLIBTOP_INIT_STATE_OPEN;
return;
/* Try to get data from environment. */ if (server->method == GLIBTOP_METHOD_INET) {
int connect_type;
temp = getenv ("LIBGTOP_SERVER") ?
getenv ("LIBGTOP_SERVER") : GTOP_SERVER;
server->server_command = glibtop_strdup_r (server, temp);
temp = getenv ("LIBGTOP_RSH") ?
getenv ("LIBGTOP_RSH") : "rsh";
server->server_rsh = glibtop_strdup_r (server, temp);
/* If the first character of 'server_command' is a colon,
* the first field is the method to connect to the server. */
if (server->server_command [0] == ':') {
/* Everything up to the next colon is the method. */
temp = strstr (server->server_command+1, ":");
if (temp) *temp = 0;
/* Dispatch method. */
if (!strcmp (server->server_command+1, "direct")) {
/* Use sysdeps dir instead of connecting to server
* even if using the server would be required on
* the current system. */
return;
} else if (!strcmp (server->server_command+1, "inet")) {
/* Connect to internet server. */
if (temp == NULL) {
server->server_host = glibtop_strdup_r
(server, "localhost");
} else {
temp2 = strstr (temp+1, ":");
if (temp2) *temp2 = 0;
server->server_host = glibtop_strdup_r
(server, temp+1);
temp = temp2;
}
if (temp == NULL) {
server->server_port = DEFAULT_PORT;
} else {
temp2 = strstr (temp+1, ":");
if (temp2) *temp2 = 0;
ret = sscanf (temp+1, "%d", &server->server_port);
if (ret != 1)
server->server_port = DEFAULT_PORT;
temp = temp2 ? temp2 + 1 : temp2;
}
fprintf (stderr, "Connecting to '%s' port %d.\n", fprintf (stderr, "Connecting to '%s' port %d.\n",
server->server_host, server->server_port); server->server_host, server->server_port);
@@ -120,10 +54,15 @@ glibtop_open_l (glibtop *server, const char *program_name,
return; return;
} }
}
/* If the server has been started, ask it for its features. */
if (server->flags & _GLIBTOP_INIT_STATE_SERVER) {
glibtop_sysdeps sysdeps;
glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL, glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL,
sizeof (glibtop_sysdeps), &sysdeps); sizeof (glibtop_sysdeps), &sysdeps);
server->features = sysdeps.features; server->features = sysdeps.features;
} }
}

62
lib/parameter.c Normal file
View File

@@ -0,0 +1,62 @@
/* $Id$ */
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Top Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <glibtop.h>
#include <glibtop/parameter.h>
#define _write_data(ptr,size) \
if ((data_ptr == NULL) || (data_size < size)) return -size; \
if (ptr == NULL) { strcpy (data_ptr, ""); return 1; } \
memcpy (data_ptr, ptr, size); \
return size;
#define _strlen(ptr) (ptr ? strlen (ptr) : 0)
size_t
glibtop_get_parameter_l (glibtop *server, const unsigned parameter,
void *data_ptr, size_t data_size)
{
switch (parameter) {
case GLIBTOP_PARAM_METHOD:
_write_data (&server->method,
sizeof (server->method));
case GLIBTOP_PARAM_FEATURES:
_write_data (&server->features,
sizeof (server->features));
case GLIBTOP_PARAM_COMMAND:
_write_data (server->server_command,
_strlen(server->server_command));
case GLIBTOP_PARAM_HOST:
_write_data (server->server_host,
_strlen(server->server_host));
case GLIBTOP_PARAM_PORT:
_write_data (&server->server_port,
sizeof (server->server_port));
}
return 0;
}
void
glibtop_set_parameter_l (glibtop *server, const unsigned parameter,
const void *data_ptr, size_t data_size)
{
}

View File

@@ -1,2 +1,3 @@
Makefile.in Makefile.in
Makefile Makefile
gnuserv

View File

@@ -55,7 +55,7 @@ BEGIN {
print ""; print "";
print "\tif (ptr) {"; print "\tif (ptr) {";
print "\t\tfor (i = 0; i < proclist.number; i++)"; print "\t\tfor (i = 0; i < proclist.number; i++)";
print "\t\t\tlist = gh_append"; print "\t\t\tlist = scm_append";
print "\t\t\t\t(gh_list (list,"; print "\t\t\t\t(gh_list (list,";
print "\t\t\t\t\t gh_list (gh_ulong2scm ((unsigned long) ptr [i])),"; print "\t\t\t\t\t gh_list (gh_ulong2scm ((unsigned long) ptr [i])),";
print "\t\t\t\t\t SCM_UNDEFINED));"; print "\t\t\t\t\t SCM_UNDEFINED));";

View File

@@ -22,7 +22,7 @@ function output(feature) {
print "\tlist = gh_list (SCM_UNDEFINED);"; print "\tlist = gh_list (SCM_UNDEFINED);";
print ""; print "";
print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)";
print "\t\tlist = gh_append"; print "\t\tlist = scm_append";
print "\t\t\t(gh_list (list,"; print "\t\t\t(gh_list (list,";
print "\t\t\t\t gh_list (gh_str02scm ((char *) glibtop_names_"feature" [i])),"; print "\t\t\t\t gh_list (gh_str02scm ((char *) glibtop_names_"feature" [i])),";
print "\t\t\t\t SCM_UNDEFINED));"; print "\t\t\t\t SCM_UNDEFINED));";
@@ -41,7 +41,7 @@ function output(feature) {
print "\tlist = gh_list (SCM_UNDEFINED);"; print "\tlist = gh_list (SCM_UNDEFINED);";
print ""; print "";
print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)";
print "\t\tlist = gh_append"; print "\t\tlist = scm_append";
print "\t\t\t(gh_list (list,"; print "\t\t\t(gh_list (list,";
print "\t\t\t\t gh_list (gh_ulong2scm (glibtop_types_"feature" [i])),"; print "\t\t\t\t gh_list (gh_ulong2scm (glibtop_types_"feature" [i])),";
print "\t\t\t\t SCM_UNDEFINED));"; print "\t\t\t\t SCM_UNDEFINED));";
@@ -60,7 +60,7 @@ function output(feature) {
print "\tlist = gh_list (SCM_UNDEFINED);"; print "\tlist = gh_list (SCM_UNDEFINED);";
print ""; print "";
print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)";
print "\t\tlist = gh_append"; print "\t\tlist = scm_append";
print "\t\t\t(gh_list (list,"; print "\t\t\t(gh_list (list,";
print "\t\t\t\t gh_list (gh_str02scm (gettext"; print "\t\t\t\t gh_list (gh_str02scm (gettext";
print "\t\t\t\t\t\t\t(glibtop_labels_"feature" [i]))),"; print "\t\t\t\t\t\t\t(glibtop_labels_"feature" [i]))),";
@@ -80,7 +80,7 @@ function output(feature) {
print "\tlist = gh_list (SCM_UNDEFINED);"; print "\tlist = gh_list (SCM_UNDEFINED);";
print ""; print "";
print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)";
print "\t\tlist = gh_append"; print "\t\tlist = scm_append";
print "\t\t\t(gh_list (list,"; print "\t\t\t(gh_list (list,";
print "\t\t\t\t gh_list (gh_str02scm (gettext"; print "\t\t\t\t gh_list (gh_str02scm (gettext";
print "\t\t\t\t\t\t\t(glibtop_descriptions_"feature" [i]))),"; print "\t\t\t\t\t\t\t(glibtop_descriptions_"feature" [i]))),";

View File

@@ -35,8 +35,7 @@ static const unsigned long _glibtop_sysdeps_cpu =
void void
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{ {
char buffer [BUFSIZ], *tmp; FILE *f;
int fd = 0, ret;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -44,25 +43,15 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
buf->flags = _glibtop_sysdeps_cpu; buf->flags = _glibtop_sysdeps_cpu;
fd = open (FILENAME, O_RDONLY); f = fopen ("/proc/stat", "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
FILENAME, strerror (errno));
ret = read (fd, buffer, BUFSIZ); fscanf (f, "cpu %lu %lu %lu %lu\n",
if (ret == -1) &buf->user, &buf->nice, &buf->sys, &buf->idle);
glibtop_error_r (server, "read (%s): %s",
FILENAME, strerror (errno));
tmp = skip_token (buffer); /* "cpu" */
buf->user = strtoul (tmp, &tmp, 10);
buf->nice = strtoul (tmp, &tmp, 10);
buf->sys = strtoul (tmp, &tmp, 10);
buf->idle = strtoul (tmp, &tmp, 10);
buf->total = buf->user + buf->nice + buf->sys + buf->idle; buf->total = buf->user + buf->nice + buf->sys + buf->idle;
buf->frequency = 100; buf->frequency = 100;
close (fd);
fclose (f);
} }

View File

@@ -33,8 +33,7 @@ static const unsigned long _glibtop_sysdeps_loadavg =
void void
glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf)
{ {
char buffer [BUFSIZ], *tmp; FILE *f;
int fd = 0, ret;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -42,19 +41,11 @@ glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf)
buf->flags = _glibtop_sysdeps_loadavg; buf->flags = _glibtop_sysdeps_loadavg;
fd = open (FILENAME, O_RDONLY); f = fopen ("/proc/loadavg", "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
FILENAME, strerror (errno));
ret = read (fd, buffer, BUFSIZ); fscanf (f, "%lf %lf %lf\n",
if (ret == -1) &buf->loadavg [0], &buf->loadavg [1], &buf->loadavg [2]);
glibtop_error_r (server, "read (%s): %s",
FILENAME, strerror (errno));
buf->loadavg [0] = strtod (buffer, &tmp); fclose (f);
buf->loadavg [1] = strtod (tmp, &tmp);
buf->loadavg [2] = strtod (tmp, &tmp);
close (fd);
} }

View File

@@ -36,8 +36,7 @@ static const unsigned long _glibtop_sysdeps_mem =
void void
glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
{ {
char buffer [BUFSIZ], *tmp; FILE *f;
int fd = 0, ret;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -45,28 +44,13 @@ glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
buf->flags = _glibtop_sysdeps_mem; buf->flags = _glibtop_sysdeps_mem;
fd = open (FILENAME, O_RDONLY); f = fopen ("/proc/meminfo", "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
FILENAME, strerror (errno));
ret = read (fd, buffer, BUFSIZ); fscanf (f, "%*[^\n]\nMem: %lu %lu %lu %lu %lu %lu\n",
if (ret == -1) &buf->total, &buf->used, &buf->free, &buf->shared, &buf->buffer, &buf->cached);
glibtop_error_r (server, "read (%s): %s",
FILENAME, strerror (errno));
tmp = strchr (buffer, '\n');
tmp = skip_token (tmp); /* "Mem:" */
tmp = skip_token (tmp); /* total memory */
buf->total = strtoul (tmp, &tmp, 10);
buf->used = strtoul (tmp, &tmp, 10);
buf->free = strtoul (tmp, &tmp, 10);
buf->shared = strtoul (tmp, &tmp, 10);
buf->buffer = strtoul (tmp, &tmp, 10);
buf->cached = strtoul (tmp, &tmp, 10);
buf->user = buf->total - buf->free - buf->shared - buf->buffer; buf->user = buf->total - buf->free - buf->shared - buf->buffer;
close (fd); fclose (f);
} }

View File

@@ -34,8 +34,9 @@ static const unsigned long _glibtop_sysdeps_proc_kernel =
void void
glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid) glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -49,23 +50,22 @@ glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
@@ -77,5 +77,7 @@ glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
&buf->maj_flt, &buf->cmaj_flt, &buf->kstk_esp, &buf->maj_flt, &buf->cmaj_flt, &buf->kstk_esp,
&buf->kstk_eip, &buf->wchan); &buf->kstk_eip, &buf->wchan);
fclose (f);
buf->flags = _glibtop_sysdeps_proc_kernel; buf->flags = _glibtop_sysdeps_proc_kernel;
} }

View File

@@ -33,8 +33,9 @@ static const unsigned long _glibtop_sysdeps_proc_mem =
void void
glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid) glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -48,23 +49,22 @@ glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */ sscanf(tmp + 2, /* skip space after ')' too */
@@ -72,23 +72,26 @@ glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %lu " "%*d %*d %*d %*d %*d %*d %*u %*u %*d %lu "
"%lu %lu", &buf->vsize, &buf->rss, &buf->rss_rlim); "%lu %lu", &buf->vsize, &buf->rss, &buf->rss_rlim);
fclose (f);
sprintf (input, "/proc/%d/statm", pid); sprintf (input, "/proc/%d/statm", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
sscanf (buffer, "%ld %ld %ld", input [nread] = 0;
sscanf (input, "%ld %ld %ld",
&buf->size, &buf->resident, &buf->share); &buf->size, &buf->resident, &buf->share);
fclose (f);
buf->flags = _glibtop_sysdeps_proc_mem; buf->flags = _glibtop_sysdeps_proc_mem;
} }

View File

@@ -35,8 +35,9 @@ void
glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
pid_t pid) pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -50,23 +51,22 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */ sscanf(tmp + 2, /* skip space after ')' too */
@@ -75,23 +75,26 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
"%*u %*u %lu %lu %lu", &buf->start_code, "%*u %*u %lu %lu %lu", &buf->start_code,
&buf->end_code, &buf->start_stack); &buf->end_code, &buf->start_stack);
fclose (f);
sprintf (input, "/proc/%d/statm", pid); sprintf (input, "/proc/%d/statm", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
sscanf (buffer, "%*d %*d %*d %ld %ld %ld %ld", input [nread] = 0;
sscanf (input, "%*d %*d %*d %ld %ld %ld %ld",
&buf->trs, &buf->lrs, &buf->drs, &buf->dt); &buf->trs, &buf->lrs, &buf->drs, &buf->dt);
fclose (f);
buf->flags = _glibtop_sysdeps_proc_segment; buf->flags = _glibtop_sysdeps_proc_segment;
} }

View File

@@ -32,8 +32,9 @@ static const unsigned long _glibtop_sysdeps_proc_signal =
void void
glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid) glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -47,23 +48,22 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */ sscanf(tmp + 2, /* skip space after ')' too */
@@ -73,5 +73,7 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
&buf->signal, &buf->blocked, &buf->sigignore, &buf->signal, &buf->blocked, &buf->sigignore,
&buf->sigcatch); &buf->sigcatch);
fclose (f);
buf->flags = _glibtop_sysdeps_proc_signal; buf->flags = _glibtop_sysdeps_proc_signal;
} }

View File

@@ -34,9 +34,10 @@ static const unsigned long _glibtop_sysdeps_proc_state =
void void
glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid) glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
struct stat statb; struct stat statb;
int fd, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -62,29 +63,27 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
buf->uid = statb.st_uid; buf->uid = statb.st_uid;
buf->gid = statb.st_gid; buf->gid = statb.st_gid;
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
memset (buf->cmd, 0, sizeof (buf->cmd)); memset (buf->cmd, 0, sizeof (buf->cmd));
sscanf (buffer, "%d (%39c", &pid, buf->cmd); sscanf (input, "%d (%39c", &pid, buf->cmd);
sscanf(tmp + 2, "%c", &buf->state); /* skip space after ')' too */ sscanf(tmp + 2, "%c", &buf->state); /* skip space after ')' too */
close (fd);
buf->flags = _glibtop_sysdeps_proc_state; buf->flags = _glibtop_sysdeps_proc_state;
} }

View File

@@ -34,8 +34,9 @@ static const unsigned long _glibtop_sysdeps_proc_time =
void void
glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid) glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -49,23 +50,22 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */ sscanf(tmp + 2, /* skip space after ')' too */
@@ -74,5 +74,7 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
&buf->utime, &buf->stime, &buf->cutime, &buf->cstime, &buf->utime, &buf->stime, &buf->cutime, &buf->cstime,
&buf->timeout, &buf->it_real_value, &buf->start_time); &buf->timeout, &buf->it_real_value, &buf->start_time);
fclose (f);
buf->flags = _glibtop_sysdeps_proc_time; buf->flags = _glibtop_sysdeps_proc_time;
} }

View File

@@ -38,8 +38,9 @@ static const unsigned long _glibtop_sysdeps_proc_uid =
void void
glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid) glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
{ {
char buffer [BUFSIZ], input [BUFSIZ], *tmp; char input [BUFSIZ], *tmp;
int fd = 0, nread; int nread;
FILE *f;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -53,22 +54,21 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
sprintf (input, "/proc/%d/status", pid); sprintf (input, "/proc/%d/status", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* Search substring 'Pid:' */ /* Search substring 'Pid:' */
tmp = strstr (buffer, "Pid:"); tmp = strstr (input, "Pid:");
if (tmp == NULL) return; if (tmp == NULL) return;
@@ -76,25 +76,26 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
"Gid: %u %u %*u %*u\n", &buf->pid, &buf->ppid, "Gid: %u %u %*u %*u\n", &buf->pid, &buf->ppid,
&buf->uid, &buf->euid, &buf->gid, &buf->egid); &buf->uid, &buf->euid, &buf->gid, &buf->egid);
fclose (f);
sprintf (input, "/proc/%d/stat", pid); sprintf (input, "/proc/%d/stat", pid);
fd = open (input, O_RDONLY); f = fopen (input, "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
input, strerror (errno));
nread = read (fd, buffer, BUFSIZ); nread = fread (input, 1, BUFSIZ, f);
if (nread == -1)
glibtop_error_r (server, "read (%s): %s",
input, strerror (errno));
buffer [nread] = 0; if (nread < 0) {
close (fd); fclose (f);
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */ /* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */ /* split into "PID (cmd" and "<rest>" */
tmp = strrchr (buffer, ')'); tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */ *tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */ /* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */ sscanf(tmp + 2, /* skip space after ')' too */
@@ -116,5 +117,7 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
/* when tty wasn't full devno */ /* when tty wasn't full devno */
buf->tty = 4*0x100 + buf->tty; buf->tty = 4*0x100 + buf->tty;
fclose (f);
buf->flags = _glibtop_sysdeps_proc_uid; buf->flags = _glibtop_sysdeps_proc_uid;
} }

View File

@@ -34,8 +34,7 @@ static unsigned long _glibtop_sysdeps_swap =
void void
glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
{ {
char buffer [BUFSIZ], *tmp; FILE *f;
int fd = 0, ret;
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -43,24 +42,11 @@ glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
buf->flags = _glibtop_sysdeps_swap; buf->flags = _glibtop_sysdeps_swap;
fd = open (FILENAME, O_RDONLY); f = fopen ("/proc/meminfo", "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
FILENAME, strerror (errno));
ret = read (fd, buffer, BUFSIZ); fscanf (f, "%*[^\n]\n%*[^\n]\nSwap: %lu %lu %lu\n",
if (ret == -1) &buf->total, &buf->used, &buf->free);
glibtop_error_r (server, "read (%s): %s",
FILENAME, strerror (errno));
tmp = strchr (buffer, '\n'); fclose (f);
tmp = strchr (tmp+1, '\n');
tmp = skip_token (tmp); /* "Swap:" */
buf->total = strtoul (tmp, &tmp, 10);
buf->used = strtoul (tmp, &tmp, 10);
buf->free = strtoul (tmp, &tmp, 10);
close (fd);
} }

View File

@@ -33,8 +33,7 @@ static unsigned long _glibtop_sysdeps_uptime =
void void
glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf)
{ {
int fd, ret; FILE *f;
char buffer [BUFSIZ];
glibtop_init_r (&server, 0, 0); glibtop_init_r (&server, 0, 0);
@@ -42,17 +41,10 @@ glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf)
buf->flags = _glibtop_sysdeps_uptime; buf->flags = _glibtop_sysdeps_uptime;
fd = open (FILENAME, O_RDONLY); f = fopen ("/proc/uptime", "r");
if (fd == -1) if (!f) return;
glibtop_error_r (server, "open (%s): %s",
FILENAME, strerror (errno));
ret = read (fd, buffer, BUFSIZ); fscanf (f, "%lf %lf\n", &buf->uptime, &buf->idletime);
if (ret == -1)
glibtop_error_r (server, "read (%s): %s",
FILENAME, strerror (errno));
sscanf (buffer, "%lf %lf\n", &buf->uptime, &buf->idletime); fclose (f);
close (fd);
} }