Files
libgtop/sysdeps/linux/glibtop_private.c
James Henstridge aa5ac8fc10 remove unused file.
2005-12-12  James Henstridge  <james@jamesh.id.au>

	* doc/auto-macros.texi.in: remove unused file.

	* doc/libgtop2.texi: don't include auto-macros.texi.

	* doc/Makefile.am: don't build auto-macros.texi, since none of its
	macros are used in the documentation any more.

	* include/glibtop/global.h: don't include <config.h> from headers.

	* .../*.c: make sure <config.h> is the first thing included.

	* acinclude.m4: m4_include() libgtop-sysdeps.m4 rather than
	inlining it.

	* acinclude.m4, libgtop-sysdeps.m4: use third argument to
	AC_DEFINE() to set the comment.

	* configure.in: modernise a bit.

	* acconfig.h: remove file, since it is deprecated

	* autogen.sh: require automake 1.9 instead of 1.4.
2005-12-12 09:38:15 +00:00

150 lines
2.3 KiB
C

#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include "glibtop_private.h"
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <unistd.h>
unsigned long long
get_scaled(const char *buffer, const char *key)
{
const char *ptr;
char *next;
unsigned long long value = 0;
if ((ptr = strstr(buffer, key)))
{
ptr += strlen(key);
value = strtoull(ptr, &next, 0);
if (strchr(next, 'k'))
value *= 1024;
else if (strchr(next, 'M'))
value *= 1024 * 1024;
}
return value;
}
char *
skip_token (const char *p)
{
p = next_token(p);
while (*p && !isspace(*p)) p++;
p = next_token(p);
return (char *)p;
}
/*
* Read functions
*/
enum TRY_FILE_TO_BUFFER
{
TRY_FILE_TO_BUFFER_OK = 0,
TRY_FILE_TO_BUFFER_OPEN = -1,
TRY_FILE_TO_BUFFER_READ = -2
};
int try_file_to_buffer(char *buffer, const char *format, ...)
{
char path[4096];
int fd;
ssize_t len;
va_list pa;
va_start(pa, format);
/* C99 also provides vsnprintf */
g_vsnprintf(path, sizeof path, format, pa);
va_end(pa);
buffer [0] = '\0';
if((fd = open (path, O_RDONLY)) < 0)
return TRY_FILE_TO_BUFFER_OPEN;
len = read (fd, buffer, BUFSIZ-1);
close (fd);
if (len < 0)
return TRY_FILE_TO_BUFFER_READ;
buffer [len] = '\0';
return TRY_FILE_TO_BUFFER_OK;
}
void
file_to_buffer(glibtop *server, char *buffer, const char *filename)
{
switch(try_file_to_buffer(buffer, filename))
{
case TRY_FILE_TO_BUFFER_OPEN:
glibtop_error_io_r (server, "open (%s)", filename);
case TRY_FILE_TO_BUFFER_READ:
glibtop_error_io_r (server, "read (%s)", filename);
}
}
static unsigned long
read_boot_time(glibtop *server)
{
char buffer[BUFSIZ];
char *btime;
file_to_buffer(server, buffer, "/proc/stat");
btime = strstr(buffer, "btime");
if (!btime) {
glibtop_warn_io_r(server, "cannot find btime in /proc/stat");
return 0UL;
}
btime = skip_token(btime);
return strtoul(btime, NULL, 10);
}
unsigned long
get_boot_time(glibtop *server)
{
static unsigned long boot_time = 0UL;
if(G_UNLIKELY(!boot_time))
{
boot_time = read_boot_time(server);
}
return boot_time;
}
size_t
get_page_size(void)
{
static size_t pagesize = 0;
if(G_UNLIKELY(!pagesize))
{
pagesize = getpagesize();
}
return pagesize;
}