Major code cleanups. We now use open () and read ().

This commit is contained in:
Martin Baulig
1998-08-16 18:46:53 +00:00
parent 739ce693dc
commit 59bb8d0ced
8 changed files with 251 additions and 268 deletions

View File

@@ -42,44 +42,34 @@ glibtop_init_proc_kernel_s (glibtop *server)
void
glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
{
char input [BUFSIZ], *tmp;
int nread;
FILE *f;
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_KERNEL, 0);
memset (buf, 0, sizeof (glibtop_proc_kernel));
sprintf (input, "/proc/%d/stat", pid);
f = fopen (input, "r");
if (!f) return;
nread = fread (input, 1, BUFSIZ, f);
if (nread < 0) {
fclose (f);
if (proc_stat_to_buffer (buffer, pid))
return;
}
input [nread] = 0;
/* This is from guile-utils/gtop/proc/readproc.c */
/* split into "PID (cmd" and "<rest>" */
tmp = strrchr (input, ')');
*tmp = '\0'; /* replace trailing ')' with NUL */
/* parse these two strings separately, skipping the leading "(". */
sscanf(tmp + 2, /* skip space after ')' too */
"%*c %*d %*d %*d %*d %*d %Lu %Lu %Lu %Lu %Lu "
"%*d %*d %*d %*d %*d %*d %*u %*u %*d %*u "
"%*u %*u %*u %*u %*u %Lu %Lu %*d %*d %*d %*d %Lu",
&buf->k_flags, &buf->min_flt, &buf->cmin_flt,
&buf->maj_flt, &buf->cmaj_flt, &buf->kstk_esp,
&buf->kstk_eip, &buf->wchan);
p = proc_stat_after_cmd (buffer);
if (!p) return;
p = skip_multiple_token (p, 6);
buf->k_flags = strtoul (p, &p, 0);
buf->min_flt = strtoul (p, &p, 0);
buf->cmin_flt = strtoul (p, &p, 0);
buf->maj_flt = strtoul (p, &p, 0);
buf->cmaj_flt = strtoul (p, &p, 0);
p = skip_multiple_token (p, 15);
buf->kstk_esp = strtoul (p, &p, 0);
buf->kstk_eip = strtoul (p, &p, 0);
fclose (f);
p = skip_multiple_token (p, 4);
buf->nwchan = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_kernel;
}