diff options
author | Kay Sievers <kay.sievers@vrfy.org> | 2007-05-03 23:21:11 +0200 |
---|---|---|
committer | Kay Sievers <kay.sievers@vrfy.org> | 2007-05-03 23:21:11 +0200 |
commit | 9f1f67b168b78c24e3ecfc4c50e00bf142f6746f (patch) | |
tree | 34410ef068922a98a0318b955f5cf6723dfb2c9d | |
parent | 1d9cdcdf91f9ad0159be530a5b23170e73bffbe8 (diff) |
udevd: use fgets() to read /proc files
Based on a patch by:
George Beshers <gbeshers@sgi.com>
-rw-r--r-- | udevd.c | 90 |
1 files changed, 33 insertions, 57 deletions
@@ -318,61 +318,42 @@ static void msg_queue_insert(struct udevd_uevent_msg *msg) static int mem_size_mb(void) { - int f; - char buf[8192]; - long int len; - const char *pos; - long int memsize; - - f = open("/proc/meminfo", O_RDONLY); - if (f == -1) - return -1; - - len = read(f, buf, sizeof(buf)-1); - close(f); + FILE* f; + char buf[4096]; + long int memsize = -1; - if (len <= 0) + f = fopen("/proc/meminfo", "r"); + if (f == NULL) return -1; - buf[len] = '\0'; - pos = strstr(buf, "MemTotal: "); - if (pos == NULL) - return -1; + while (fgets(buf, sizeof(buf), f) != NULL) { + long int value; - if (sscanf(pos, "MemTotal: %ld kB", &memsize) != 1) - return -1; + if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) { + memsize = value / 1024; + break; + } + } - return memsize / 1024; + return memsize; } static int cpu_count(void) { - int f; - char buf[65536]; - int len; - const char *pos; + FILE* f; + char buf[4096]; int count = 0; - f = open("/proc/stat", O_RDONLY); - if (f == -1) + f = fopen("/proc/stat", "r"); + if (f == NULL) return -1; - len = read(f, buf, sizeof(buf)-1); - close(f); - if (len <= 0) - return -1; - buf[len] = '\0'; - - pos = strstr(buf, "cpu"); - if (pos == NULL) - return -1; - - while (pos != NULL) { - if (strncmp(pos, "cpu", 3) == 0 &&isdigit(pos[3])) + while (fgets(buf, sizeof(buf), f) != NULL) { + if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) count++; - pos = strstr(&pos[3], "cpu"); } + fclose(f); if (count == 0) return -1; return count; @@ -380,29 +361,24 @@ static int cpu_count(void) static int running_processes(void) { - int f; - char buf[32768]; - int len; - int running; - const char *pos; + FILE* f; + char buf[4096]; + int running = -1; - f = open("/proc/stat", O_RDONLY); - if (f == -1) + f = fopen("/proc/stat", "r"); + if (f == NULL) return -1; - len = read(f, buf, sizeof(buf)-1); - close(f); - if (len <= 0) - return -1; - buf[len] = '\0'; - - pos = strstr(buf, "procs_running "); - if (pos == NULL) - return -1; + while (fgets(buf, sizeof(buf), f) != NULL) { + int value; - if (sscanf(pos, "procs_running %u", &running) != 1) - return -1; + if (sscanf(buf, "procs_running %u", &value) == 1) { + running = value; + break; + } + } + fclose(f); return running; } |