diff options
author | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-04-11 19:45:26 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@tele2.at> | 2018-04-11 19:45:26 +0200 |
commit | 6642b2b2bf3d16e0632989c1e29c672882e0d283 (patch) | |
tree | f39581ab909a41fe07c005cbb81f2ec273d057ea /lib | |
parent | 71d98c150f6242fcf1b8a5845e46db56caad3885 (diff) |
Don't pre-allocate argument vector for command lines
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/include/service.h | 4 | ||||
-rw-r--r-- | lib/include/util.h | 12 | ||||
-rw-r--r-- | lib/src/delsvc.c | 1 | ||||
-rw-r--r-- | lib/src/rdsvc.c | 29 | ||||
-rw-r--r-- | lib/src/split_argv.c | 22 |
5 files changed, 7 insertions, 61 deletions
diff --git a/lib/include/service.h b/lib/include/service.h index 471ccbb..595c60e 100644 --- a/lib/include/service.h +++ b/lib/include/service.h @@ -46,8 +46,8 @@ enum { typedef struct exec_t { struct exec_t *next; - char **argv; /* NULL terminated argument vector */ - char buffer[]; /* backing store for argv */ + int argc; /* number of elements in argument vector */ + char args[]; /* argument vectot string blob */ } exec_t; typedef struct service_t { diff --git a/lib/include/util.h b/lib/include/util.h index d824b22..d6b68f1 100644 --- a/lib/include/util.h +++ b/lib/include/util.h @@ -113,18 +113,6 @@ int unescape(char *src); int pack_argv(char *str); /* - Split a space seperated string into a sequence of null-terminated - strings. Return a NULL terminated array of strings pointing to the - start of each sub string. - - It basically runs pack_argv on 'str' and then constructs the argv - vector from that, with each entry pointing into 'str'. - - The returned array must be freed with free(). -*/ -char **split_argv(char *str); - -/* Search through an array of enum_map_t entries to resolve a string to a numeric value. The end of the map is indicated by a sentinel entry with the name set to NULL. diff --git a/lib/src/delsvc.c b/lib/src/delsvc.c index f4f9c76..fd10cfe 100644 --- a/lib/src/delsvc.c +++ b/lib/src/delsvc.c @@ -30,7 +30,6 @@ void delsvc(service_t *svc) e = svc->exec; svc->exec = e->next; - free(e->argv); free(e); } diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c index c8ba590..1a8cf7e 100644 --- a/lib/src/rdsvc.c +++ b/lib/src/rdsvc.c @@ -38,26 +38,6 @@ static int try_unescape(char *arg, rdline_t *rd) return 0; } -static char **try_split_argv(char *str, rdline_t *rd) -{ - char **argv = split_argv(str); - - if (argv == NULL) { - switch (errno) { - case EINVAL: - fprintf(stderr, "%s: %zu: malformed string constant\n", - rd->filename, rd->lineno); - break; - default: - fprintf(stderr, "%s: %zu: %s\n", - rd->filename, rd->lineno, strerror(errno)); - break; - } - } - - return argv; -} - static char *try_strdup(const char *str, rdline_t *rd) { char *out = strdup(str); @@ -96,11 +76,12 @@ static int svc_exec(service_t *svc, char *arg, rdline_t *rd) return -1; } - strcpy(e->buffer, arg); + strcpy(e->args, arg); - e->argv = try_split_argv(e->buffer, rd); - if (e->argv == NULL) { - free(e); + e->argc = pack_argv(e->args); + if (e->argc < 0) { + fprintf(stderr, "%s: %zu: malformed string constant\n", + rd->filename, rd->lineno); return -1; } diff --git a/lib/src/split_argv.c b/lib/src/split_argv.c index b95720d..5930e2d 100644 --- a/lib/src/split_argv.c +++ b/lib/src/split_argv.c @@ -80,25 +80,3 @@ fail_str: errno = EINVAL; return -1; } - -char **split_argv(char *str) -{ - char **argv = NULL; - int i, count; - - count = pack_argv(str); - if (count <= 0) - return NULL; - - argv = malloc(sizeof(argv[0]) * (count + 1)); - if (argv == NULL) - return NULL; - - for (i = 0; i < count; ++i) { - argv[i] = str; - str += strlen(str) + 1; - } - - argv[i] = NULL; - return argv; -} |