get_cmdline_val: search for entire name, not just suffix
authorSteven Barth <steven@midlink.org>
Wed, 5 Nov 2014 10:17:09 +0000 (11:17 +0100)
committerSteven Barth <steven@midlink.org>
Wed, 5 Nov 2014 10:17:09 +0000 (11:17 +0100)
also fix writing of 0-byte to buffer

Signed-off-by: Steven Barth <steven@midlink.org>
utils/utils.c

index e782e4428842684d725f361d769f101a9659ed3c..c0d1cd536ae6870d1386d7ac3b8be0428b5db3a0 100644 (file)
@@ -129,36 +129,27 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2)
 
 char* get_cmdline_val(const char* name, char* out, int len)
 {
-       char pattern[CMDLINE_SIZE + 1];
-       char line[CMDLINE_SIZE + 1];
-       char *res = NULL, *tty;
-       int r, fd;
-       regex_t pat_cmdline;
-       regmatch_t matches[2];
-
-       fd = open("/proc/cmdline", O_RDONLY);
-       if (fd < 0)
-               return NULL;
+       char line[CMDLINE_SIZE + 1], *c, *sptr;
+       int fd = open("/proc/cmdline", O_RDONLY);
+       ssize_t r = read(fd, line, sizeof(line) - 1);
+       close(fd);
 
-       r = read(fd, line, CMDLINE_SIZE);
-       if ( r <= 0 ) {
-               close(fd);
+       if (r <= 0)
                return NULL;
-       }
-       line[r] = '\0';
-       close(fd);
 
-       sprintf( pattern, "%s=([^ \n]*)", name);
-       regcomp(&pat_cmdline, pattern, REG_EXTENDED);
-       if (!regexec(&pat_cmdline, line, 2, matches, 0)) {
-               line[matches[1].rm_eo] = '\0';
-               tty = (line + matches[1].rm_so);
-               strncpy(out, tty, len);
-               tty[len-1] = '\0';
-               res = out;
-       }
+       line[r] = 0;
+
+       for (c = strtok_r(line, " \t\n", &sptr); c;
+                       c = strtok_r(NULL, " \t\n", &sptr)) {
+               char *sep = strchr(c, '=');
+               ssize_t klen = sep - c;
+               if (klen < 0 || strncmp(name, c, klen) || name[klen] != 0)
+                       continue;
 
-       regfree(&pat_cmdline);
+               strncpy(out, &sep[1], len);
+               out[len-1] = 0;
+               return out;
+       }
 
-       return res;
+       return NULL;
 }