]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use fscanf instead of get_line when parsing /proc/cpuinfo
authorRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 20:31:29 +0000 (20:31 +0000)
committerRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 20:31:29 +0000 (20:31 +0000)
dhcpcd.c
if-linux.c
platform-bsd.c
platform-linux.c
platform.h

index 33d2f83561d2b6b0fc0f0d2547b33919b1818c8a..70848ee7dd1ca8e954099283b0ccfbed3255e2fc 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -1063,7 +1063,7 @@ signal_init(void (*func)(int, siginfo_t *, void *), sigset_t *oldset)
 int
 main(int argc, char **argv)
 {
-       char *pidfile;
+       char *pidfile, *p;
        struct interface *ifp;
        uint16_t family = 0;
        int opt, oi = 0, sig = 0, i;
@@ -1071,7 +1071,6 @@ main(int argc, char **argv)
        pid_t pid;
        struct timespec ts;
        struct utsname utn;
-       const char *platform;
        struct control_ctx control_ctx;
 
        pidfile = NULL;
@@ -1090,13 +1089,13 @@ main(int argc, char **argv)
                }
        }
 
-       platform = hardware_platform();
-       if (uname(&utn) == 0)
-               snprintf(vendor, VENDORCLASSID_MAX_LEN,
-                   "%s-%s:%s-%s:%s%s%s", PACKAGE, VERSION,
-                   utn.sysname, utn.release, utn.machine,
-                   platform ? ":" : "", platform ? platform : "");
-       else
+       if (uname(&utn) == 0) {
+               p = vendor;
+               p += snprintf(vendor, VENDORCLASSID_MAX_LEN,
+                   "%s-%s:%s-%s:%s", PACKAGE, VERSION,
+                   utn.sysname, utn.release, utn.machine);
+               hardware_platform(p, VENDORCLASSID_MAX_LEN - (p - vendor));
+       else
                snprintf(vendor, VENDORCLASSID_MAX_LEN,
                    "%s-%s", PACKAGE, VERSION);
 
index 632e89d9daf252a4be4525ec271c5867980f9b8e..da4e0ab09c2555cbcd8a7dcebdd02e0336ffdc9a 100644 (file)
@@ -865,11 +865,10 @@ in6_addr_flags(const char *ifname, const struct in6_addr *addr)
        }
        *p = '\0';
 
-       while ((p = get_line(fp))) {
-               i = sscanf(p, "%32[a-f0-9] %x %x %x %x"
-                   " %"TOSTRING(IF_NAMESIZE)"s\n",
-                   address, &ifindex, &prefix, &scope, &flags, name);
-               if (i != 6 || strlen(address) != 32) {
+       while (fscanf(fp, "%32[a-f0-9] %x %x %x %x %"TOSTRING(IF_NAMESIZE)"s\n",
+           address, &ifindex, &prefix, &scope, &flags, name) == 6)
+       {
+               if (strlen(address) != 32) {
                        fclose(fp);
                        errno = ENOTSUP;
                        return -1;
index ea346ae50ea31256191abb82a691fb489417aa57..0874474569073fd0343a3e1e3193cfc8508ce2e3 100644 (file)
 #  define SYS_NMLN 256
 #endif
 
-static char march[SYS_NMLN];
-
-char *
-hardware_platform(void)
+int
+hardware_platform(char *str, size_t len)
 {
        int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
+       char march[SYS_NMLN];
        size_t len = sizeof(march);
 
        if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
-               march, &len, NULL, 0) != 0)
-               return NULL;
-       return march;
+           march, &len, NULL, 0) != 0)
+               return -1;
+       return snprintf(str, len, ":%s", march);
 }
 
 #ifdef INET6
index ff245b2069a1c39c43f8f7c2253ba145b24571de..8f15e975e8696c822ddefd190d0e06f45674c700 100644 (file)
@@ -79,37 +79,31 @@ static char **restore;
 static ssize_t nrestore;
 #endif
 
-char *
-hardware_platform(void)
+int
+hardware_platform(char *str, size_t len)
 {
        FILE *fp;
-       char *buf, *p;
+       char buf[255];
 
        if (mproc == NULL) {
                errno = EINVAL;
-               return NULL;
+               return -1;
        }
 
        fp = fopen("/proc/cpuinfo", "r");
        if (fp == NULL)
-               return NULL;
+               return -1;
 
-       p = NULL;
-       while ((buf = get_line(fp))) {
+       while (fscanf(fp, "%s : ", buf) != EOF) {
                if (strncmp(buf, mproc, strlen(mproc)) == 0) {
-                       p = strchr(buf, ':');
-                       if (p != NULL && ++p != NULL) {
-                               while (*p == ' ')
-                                       p++;
-                               break;
-                       }
+                       fscanf(fp, "%s", buf);
+                       fclose(fp);
+                       return snprintf(str, len, ":%s", buf);
                }
        }
        fclose(fp);
-
-       if (p == NULL)
-               errno = ESRCH;
-       return p;
+       errno = ESRCH;
+       return -1;
 }
 
 #ifdef INET6
index b9f71a5a239841bf408c582021c50844d1e5f628..b79ec483c264bbadb33d1ab119a7c2d553fd3a00 100644 (file)
@@ -28,7 +28,7 @@
 #ifndef PLATFORM_H
 #define PLATFORM_H
 
-char *hardware_platform(void);
+int hardware_platform(char *, size_t);
 #ifdef INET6
 int check_ipv6(const char *, int);
 int ipv6_dadtransmits(const char *);