From 79872ea6ca5867631c1ec5405721af12bea818b2 Mon Sep 17 00:00:00 2001 From: Michel Stam Date: Mon, 13 Oct 2014 16:14:34 +0200 Subject: Use one generic routine to access /proc/cmdline Signed-off-by: Michel Stam --- utils/utils.c | 42 ++++++++++++++++++++++++++++++++++++++++++ utils/utils.h | 1 + 2 files changed, 43 insertions(+) (limited to 'utils') diff --git a/utils/utils.c b/utils/utils.c index 59d02f1..5e67310 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -15,6 +15,12 @@ #include #include #include "utils.h" +#include +#include +#include +#include +#include +#include void __blobmsg_list_init(struct blobmsg_list *list, int offset, int len, blobmsg_list_cmp cmp) @@ -120,3 +126,39 @@ blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2) return true; } + +char* get_cmdline_val(const char* name, char* out, int len) +{ + char pattern[COMMAND_LINE_SIZE + 1]; + char line[COMMAND_LINE_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; + + r = read(fd, line, COMMAND_LINE_SIZE); + if ( r <= 0 ) { + close(fd); + 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; + } + + regfree(&pat_cmdline); + + return res; +} diff --git a/utils/utils.h b/utils/utils.h index 37fa216..c2c1cb4 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -49,5 +49,6 @@ int blobmsg_list_fill(struct blobmsg_list *list, void *data, int len, bool array void blobmsg_list_free(struct blobmsg_list *list); bool blobmsg_list_equal(struct blobmsg_list *l1, struct blobmsg_list *l2); void blobmsg_list_move(struct blobmsg_list *list, struct blobmsg_list *src); +char* get_cmdline_val(const char* name, char* out, int len); #endif -- cgit v1.2.3