The API makes a deep copy of a NULL-terminated string list. Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx> Reviewed-by: John Ferlan <jferlan@xxxxxxxxxx> --- Notes: Version 2: - no change src/util/virstring.c | 37 +++++++++++++++++++++++++++++++++++++ src/util/virstring.h | 3 +++ 2 files changed, 40 insertions(+) diff --git a/src/util/virstring.c b/src/util/virstring.c index 0288d1e677..820b282ac5 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -239,6 +239,43 @@ virStringListRemove(char ***strings, } +/** + * virStringListCopy: + * @dst: where to store the copy of @strings + * @src: a NULL-terminated array of strings + * + * Makes a deep copy of the @src string list and stores it in @dst. Callers + * are responsible for freeing both @dst and @src. + * + * Returns 0 on success, -1 on error. + */ +int +virStringListCopy(char ***dst, + const char **src) +{ + char **copy = NULL; + size_t i; + + if (!src) + return 0; + + if (VIR_ALLOC_N(copy, virStringListLength(src) + 1) < 0) + goto error; + + for (i = 0; src[i]; i++) { + if (VIR_STRDUP(copy[i], src[i]) < 0) + goto error; + } + + *dst = copy; + return 0; + + error: + virStringListFree(copy); + return -1; +} + + /** * virStringListFree: * @str_array: a NULL-terminated array of strings to free diff --git a/src/util/virstring.h b/src/util/virstring.h index 1290fcce15..cfd91be314 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings, void virStringListRemove(char ***strings, const char *item); +int virStringListCopy(char ***dst, + const char **src); + void virStringListFree(char **strings); void virStringListFreeCount(char **strings, size_t count); -- 2.14.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list