From 9ce7a46f6578a86b72f20acd7b0e55b1b4ebea09 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Mon, 27 Oct 2014 16:53:44 +0100 Subject: Add add_strings_lists() utility function Reviewed-by: Jakub Hrozek --- src/util/util.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util/util.h | 18 ++++++++++++++++ 2 files changed, 83 insertions(+) (limited to 'src/util') diff --git a/src/util/util.c b/src/util/util.c index d78d37d97..2acb8604a 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -815,3 +815,68 @@ const char * const * get_known_services(void) return svc; } + +errno_t add_strings_lists(TALLOC_CTX *mem_ctx, const char **l1, const char **l2, + bool copy_strings, char ***_new_list) +{ + size_t c; + size_t l1_count = 0; + size_t l2_count = 0; + size_t new_count = 0; + char **new; + int ret; + + if (l1 != NULL) { + for (l1_count = 0; l1[l1_count] != NULL; l1_count++); + } + + if (l2 != NULL) { + for (l2_count = 0; l2[l2_count] != NULL; l2_count++); + } + + new_count = l1_count + l2_count; + + new = talloc_array(mem_ctx, char *, new_count + 1); + if (new == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_array failed.\n"); + return ENOMEM; + } + new [new_count] = NULL; + + if (copy_strings) { + for(c = 0; c < l1_count; c++) { + new[c] = talloc_strdup(new, l1[c]); + if (new[c] == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n"); + ret = ENOMEM; + goto done; + } + } + for(c = 0; c < l2_count; c++) { + new[l1_count + c] = talloc_strdup(new, l2[c]); + if (new[l1_count + c] == NULL) { + DEBUG(SSSDBG_OP_FAILURE, "talloc_strdup failed.\n"); + ret = ENOMEM; + goto done; + } + } + } else { + if (l1 != NULL) { + memcpy(new, l1, sizeof(char *) * l1_count); + } + + if (l2 != NULL) { + memcpy(&new[l1_count], l2, sizeof(char *) * l2_count); + } + } + + *_new_list = new; + ret = EOK; + +done: + if (ret != EOK) { + talloc_free(new); + } + + return ret; +} diff --git a/src/util/util.h b/src/util/util.h index 69074c93c..ffc8a87ea 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -426,6 +426,24 @@ errno_t sss_hash_create_ex(TALLOC_CTX *mem_ctx, hash_delete_callback *delete_callback, void *delete_private_data); +/** + * @brief Add two list of strings + * + * Create a new NULL-termintated list of strings by adding two lists together. + * + * @param[in] mem_ctx Talloc memory context for the new list. + * @param[in] l1 First NULL-termintated list of strings. + * @param[in] l2 Second NULL-termintated list of strings. + * @param[in] copy_strings If set to 'true' the list items will be copied + * otherwise only the pointers to the items are + * copied. + * @param[out] new_list New NULL-terminated list of strings. Must be freed + * with talloc_free() by the caller. If copy_strings + * is 'true' the new elements will be freed as well. + */ +errno_t add_strings_lists(TALLOC_CTX *mem_ctx, const char **l1, const char **l2, + bool copy_strings, char ***_new_list); + /* Copy a NULL-terminated string list * Returns NULL on out of memory error or invalid input */ -- cgit