Skip to content

Commit

Permalink
utils: add lxc_append_string()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christian Brauner committed Nov 9, 2016
1 parent 5e8b774 commit 000dfda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lxc/utils.c
Expand Up @@ -1930,3 +1930,37 @@ bool task_blocking_signal(pid_t pid, int signal)
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;
}
2 changes: 2 additions & 0 deletions src/lxc/utils.h
Expand Up @@ -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 *);
Expand Down

0 comments on commit 000dfda

Please sign in to comment.