summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorSteven Barth <steven@midlink.org>2014-11-05 11:17:09 +0100
committerSteven Barth <steven@midlink.org>2014-11-05 11:17:09 +0100
commit6f52ef3cc9f01e952e7f32d10c7f8612e635e80c (patch)
treebda1190f6fa5684e56ad5165fca9cacfac0ee31f /utils
parentdc3ea949544090f91a24b05c5fde0bd9ab87f2ce (diff)
downloadunitd-6f52ef3cc9f01e952e7f32d10c7f8612e635e80c.tar
unitd-6f52ef3cc9f01e952e7f32d10c7f8612e635e80c.zip
get_cmdline_val: search for entire name, not just suffix
also fix writing of 0-byte to buffer Signed-off-by: Steven Barth <steven@midlink.org>
Diffstat (limited to 'utils')
-rw-r--r--utils/utils.c45
1 files changed, 18 insertions, 27 deletions
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;
}