From: Roy Marples Date: Fri, 7 Feb 2014 20:31:29 +0000 (+0000) Subject: Use fscanf instead of get_line when parsing /proc/cpuinfo X-Git-Tag: v6.3.0~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=faaa132c3355bc890fa0ad732a992deb92a89904;p=thirdparty%2Fdhcpcd.git Use fscanf instead of get_line when parsing /proc/cpuinfo --- diff --git a/dhcpcd.c b/dhcpcd.c index 33d2f835..70848ee7 100644 --- 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); diff --git a/if-linux.c b/if-linux.c index 632e89d9..da4e0ab0 100644 --- a/if-linux.c +++ b/if-linux.c @@ -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; diff --git a/platform-bsd.c b/platform-bsd.c index ea346ae5..08744745 100644 --- a/platform-bsd.c +++ b/platform-bsd.c @@ -53,18 +53,17 @@ # 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 diff --git a/platform-linux.c b/platform-linux.c index ff245b20..8f15e975 100644 --- a/platform-linux.c +++ b/platform-linux.c @@ -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 diff --git a/platform.h b/platform.h index b9f71a5a..b79ec483 100644 --- a/platform.h +++ b/platform.h @@ -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 *);