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

@@ -27,6 +27,8 @@
__BEGIN_DECLS
#ifdef _IN_LIBGTOP
static inline char *
skip_token (const char *p)
{
@@ -35,6 +37,17 @@ skip_token (const char *p)
return (char *)p;
}
static inline char *
skip_multiple_token (const char *p, int count)
{
int i;
for (i = 0; i < count; i++)
p = skip_token (p);
return (char *)p;
}
static inline char *
skip_line (const char *p)
{
@@ -42,6 +55,57 @@ skip_line (const char *p)
return (char *) p++;
}
static inline int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
{
char filename [BUFSIZ];
int fd, len;
sprintf (filename, fmt, pid);
fd = open (filename, O_RDONLY);
if (fd < 0) return -1;
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) return -1;
close (fd);
buffer [len] = '\0';
return 0;
}
static inline int
proc_stat_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/stat", pid);
}
static inline int
proc_status_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/status", pid);
}
static inline int
proc_statm_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/statm", pid);
}
static inline char *
proc_stat_after_cmd (char *p)
{
p = strrchr (p, ')');
if (!p) return p;
*p++ = '\0';
return p;
}
#endif
#define GLIBTOP_SUID_CPU 0
#define GLIBTOP_SUID_MEM 0
#define GLIBTOP_SUID_SWAP 0