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 <christian.brauner@canonical.com>
This commit is contained in:
Christian Brauner 2016-11-06 19:48:58 +01:00
parent 5e8b774630
commit 000dfda7f3
No known key found for this signature in database
GPG Key ID: 8EB056D53EECB12D
2 changed files with 36 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 *);