From d412e76b28894271a29ce53eac47de562fc86a59 Mon Sep 17 00:00:00 2001 From: Stephen Gallagher Date: Wed, 28 Jul 2010 11:28:13 -0400 Subject: Add dup_string_list() utility function --- src/util/util.c | 31 +++++++++++++++++++++++++++++++ src/util/util.h | 6 ++++++ 2 files changed, 37 insertions(+) diff --git a/src/util/util.c b/src/util/util.c index 206984c7..10bfb647 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -237,7 +237,38 @@ fail: return NULL; } +char **dup_string_list(TALLOC_CTX *memctx, const char **str_list) +{ + int i = 0; + int j = 0; + char **dup_list; + + if (!str_list) { + return NULL; + } + + /* Find the size of the list */ + while (str_list[i]) i++; + + dup_list = talloc_array(memctx, char *, i+1); + if (!dup_list) { + return NULL; + } + /* Copy the elements */ + for (j = 0; j < i; j++) { + dup_list[j] = talloc_strdup(dup_list, str_list[j]); + if (!dup_list[j]) { + talloc_free(dup_list); + return NULL; + } + } + + /* NULL-terminate the list */ + dup_list[i] = NULL; + + return dup_list; +} /* Take two string lists (terminated on a NULL char*) * and return up to three arrays of strings based on diff --git a/src/util/util.h b/src/util/util.h index da92ae68..6bcc9984 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -332,6 +332,12 @@ int split_on_separator(TALLOC_CTX *mem_ctx, const char *str, char **parse_args(const char *str); + +/* Copy a NULL-terminated string list + * Returns NULL on out of memory error or invalid input + */ +char **dup_string_list(TALLOC_CTX *memctx, const char **str_list); + /* Take two string lists (terminated on a NULL char*) * and return up to three arrays of strings based on * shared ownership. -- cgit