From a138db3c07553cfd252495a3c406e11457075e79 Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Sat, 18 Feb 2017 17:09:49 -0500 Subject: add alpm_list_append_strdup Makes error detection and handling easier for a common operation. Signed-off-by: Andrew Gregory Signed-off-by: Allan McRae --- lib/libalpm/alpm_list.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/libalpm/alpm_list.c') diff --git a/lib/libalpm/alpm_list.c b/lib/libalpm/alpm_list.c index 36b01c71..0f1b819c 100644 --- a/lib/libalpm/alpm_list.c +++ b/lib/libalpm/alpm_list.c @@ -131,6 +131,26 @@ alpm_list_t SYMEXPORT *alpm_list_append(alpm_list_t **list, void *data) return ptr; } +/** + * @brief Duplicate and append a string to a list. + * + * @param list the list to append to + * @param data the string to duplicate and append + * + * @return the newly added item + */ +alpm_list_t SYMEXPORT *alpm_list_append_strdup(alpm_list_t **list, const char *data) +{ + alpm_list_t *ret; + char *dup; + if((dup = strdup(data)) && (ret = alpm_list_append(list, dup))) { + return ret; + } else { + free(dup); + return NULL; + } +} + /** * @brief Add items to a list in sorted order. * -- cgit v1.2.3-24-g4f1b