From: INAGAKI Hiroshi Date: Tue, 12 Mar 2024 04:05:46 +0000 (+0900) Subject: utils: add key-value offset support to get_cmdline_val() X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=49ea930a862c8eeb4996170b34a930f5848895e9;p=project%2Fprocd.git utils: add key-value offset support to get_cmdline_val() Add offset support of key-value pairs to get_cmdline_val() for getting multiple values of the same keys. Signed-off-by: INAGAKI Hiroshi --- diff --git a/utils/utils.c b/utils/utils.c index f0c4a90..e5b1297 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -163,10 +163,10 @@ char *get_active_console(char *out, int len) return NULL; } -char* get_cmdline_val(const char* name, char* out, int len) +char *get_cmdline_val_offset(const char *name, char *out, int len, int offset) { char line[CMDLINE_SIZE + 1], *c, *sptr; - int fd = open("/proc/cmdline", O_RDONLY); + int i, fd = open("/proc/cmdline", O_RDONLY); ssize_t r = read(fd, line, sizeof(line) - 1); close(fd); @@ -175,7 +175,7 @@ char* get_cmdline_val(const char* name, char* out, int len) line[r] = 0; - for (c = strtok_r(line, " \t\n", &sptr); c; + for (i = 0, c = strtok_r(line, " \t\n", &sptr); c; c = strtok_r(NULL, " \t\n", &sptr)) { char *sep = strchr(c, '='); if (sep == NULL) @@ -185,6 +185,8 @@ char* get_cmdline_val(const char* name, char* out, int len) if (strncmp(name, c, klen) || name[klen] != 0) continue; + if (i++ < offset) + continue; strncpy(out, &sep[1], len); out[len-1] = 0; return out; diff --git a/utils/utils.h b/utils/utils.h index 898bcc4..d0c621e 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -51,9 +51,12 @@ 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); +char *get_cmdline_val_offset(const char *name, char *out, int len, int offset); char *get_active_console(char *out, int len); +#define get_cmdline_val(name, out, len) \ + get_cmdline_val_offset(name, out, len, 0) + int patch_fd(const char *device, int fd, int flags); int patch_stdio(const char *device);