From 000dfda7f34171fc0596c0aa47dac3d0cc8d3bbc Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Sun, 6 Nov 2016 19:48:58 +0100 Subject: [PATCH] utils: add lxc_append_string() lxc_append_string() appends strings without separator. This is mostly useful for reading in whole files line-by-line. Signed-off-by: Christian Brauner --- src/lxc/utils.c | 34 ++++++++++++++++++++++++++++++++++ src/lxc/utils.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/src/lxc/utils.c b/src/lxc/utils.c index c912fe8f0..d6f8ecf03 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -1930,3 +1930,37 @@ out: fclose(f); return bret; } + +static int lxc_append_null_to_list(void ***list) +{ + int newentry = 0; + void **tmp; + + if (*list) + for (; (*list)[newentry]; newentry++) { + ; + } + + tmp = realloc(*list, (newentry + 2) * sizeof(void **)); + if (!tmp) + return -1; + + *list = tmp; + (*list)[newentry + 1] = NULL; + + return newentry; +} + +int lxc_append_string(char ***list, char *entry) +{ + int newentry = lxc_append_null_to_list((void ***)list); + char *copy; + + copy = strdup(entry); + if (!copy) + return -1; + + (*list)[newentry] = copy; + + return 0; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index a0fa0e2a5..50e184712 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -258,6 +258,8 @@ extern char *lxc_append_paths(const char *first, const char *second); extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep); extern char **lxc_string_split(const char *string, char sep); extern char **lxc_string_split_and_trim(const char *string, char sep); +/* Append string to NULL-terminated string array. */ +extern int lxc_append_string(char ***list, char *entry); /* some simple array manipulation utilities */ typedef void (*lxc_free_fn)(void *);