From: Steven Barth Date: Wed, 5 Nov 2014 10:17:09 +0000 (+0100) Subject: get_cmdline_val: search for entire name, not just suffix X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=6f52ef3cc9f01e952e7f32d10c7f8612e635e80c;p=oweals%2Fprocd.git get_cmdline_val: search for entire name, not just suffix also fix writing of 0-byte to buffer Signed-off-by: Steven Barth --- diff --git a/utils/utils.c b/utils/utils.c index e782e44..c0d1cd5 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -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; }