]> err.no Git - systemd/commitdiff
udevd: use fgets() to read /proc files
authorKay Sievers <kay.sievers@vrfy.org>
Thu, 3 May 2007 21:21:11 +0000 (23:21 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Thu, 3 May 2007 21:21:11 +0000 (23:21 +0200)
Based on a patch by:
  George Beshers <gbeshers@sgi.com>

udevd.c

diff --git a/udevd.c b/udevd.c
index 645b068c0eea99daf58958b789ec0e96e26fabfc..fe01d8d4b909b6eef41719f758780b1dabfd8280 100644 (file)
--- a/udevd.c
+++ b/udevd.c
@@ -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;
 }