From 35e01a9601292b2f609e65c2ddb3990cba8f378e Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 30 Jul 2014 14:40:22 +0200 Subject: [PATCH] system.c: parse quoted version info from /etc/openwrt_release Signed-off-by: Felix Fietkau --- system.c | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/system.c b/system.c index 2dbed51..4b3b82c 100644 --- a/system.c +++ b/system.c @@ -38,7 +38,7 @@ static int system_board(struct ubus_context *ctx, struct ubus_object *obj, { void *c; char line[256]; - char *key, *val; + char *key, *val, *next; struct utsname utsname; FILE *f; @@ -111,24 +111,53 @@ static int system_board(struct ubus_context *ctx, struct ubus_object *obj, while (fgets(line, sizeof(line), f)) { - key = strtok(line, "=\""); - val = strtok(NULL, "\"\n"); + char *dest; + char ch; - if (!key || !val) + key = line; + val = strchr(line, '='); + if (!val) continue; + *(val++) = 0; + if (!strcasecmp(key, "DISTRIB_ID")) - blobmsg_add_string(&b, "distribution", val); + key = "distribution"; else if (!strcasecmp(key, "DISTRIB_RELEASE")) - blobmsg_add_string(&b, "version", val); + key = "version"; else if (!strcasecmp(key, "DISTRIB_REVISION")) - blobmsg_add_string(&b, "revision", val); + key = "revision"; else if (!strcasecmp(key, "DISTRIB_CODENAME")) - blobmsg_add_string(&b, "codename", val); + key = "codename"; else if (!strcasecmp(key, "DISTRIB_TARGET")) - blobmsg_add_string(&b, "target", val); + key = "target"; else if (!strcasecmp(key, "DISTRIB_DESCRIPTION")) - blobmsg_add_string(&b, "description", val); + key = "description"; + else + continue; + + dest = blobmsg_alloc_string_buffer(&b, key, strlen(val)); + while (val && (ch = *(val++)) != 0) { + switch (ch) { + case '\'': + case '"': + next = strchr(val, ch); + if (next) + *next = 0; + + strcpy(dest, val); + + if (next) + val = next + 1; + + dest += strlen(dest); + break; + case '\\': + *(dest++) = *(val++); + break; + } + } + blobmsg_add_string_buffer(&b); } blobmsg_close_array(&b, c); -- 2.11.0