[pacman-dev,1/2] add alpm_list_append_strdup

Message ID 20170218220950.4734-1-andrew.gregory.8@gmail.com
State Accepted, archived
Headers show
Series [pacman-dev,1/2] add alpm_list_append_strdup | expand

Commit Message

Andrew Gregory Feb. 18, 2017, 10:09 p.m. UTC
Makes error detection and handling easier for a common operation.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
---
 lib/libalpm/alpm_list.c | 20 ++++++++++++++++++++
 lib/libalpm/alpm_list.h |  1 +
 2 files changed, 21 insertions(+)

Comments

Allan McRae April 4, 2017, 2:03 a.m. UTC | #1
On 19/02/17 08:09, Andrew Gregory wrote:
> Makes error detection and handling easier for a common operation.
> 
> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>

OK

Are you intending to spread the use of this function?

A

Patch

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
@@ -132,6 +132,26 @@  alpm_list_t SYMEXPORT *alpm_list_append(alpm_list_t **list, void *data)
 }
 
 /**
+ * @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.
  *
  * @param list the list to add to
diff --git a/lib/libalpm/alpm_list.h b/lib/libalpm/alpm_list.h
index d7306cc5..1cb237d0 100644
--- a/lib/libalpm/alpm_list.h
+++ b/lib/libalpm/alpm_list.h
@@ -58,6 +58,7 @@  void alpm_list_free_inner(alpm_list_t *list, alpm_list_fn_free fn);
 /* item mutators */
 alpm_list_t *alpm_list_add(alpm_list_t *list, void *data);
 alpm_list_t *alpm_list_append(alpm_list_t **list, void *data);
+alpm_list_t *alpm_list_append_strdup(alpm_list_t **list, const char *data);
 alpm_list_t *alpm_list_add_sorted(alpm_list_t *list, void *data, alpm_list_fn_cmp fn);
 alpm_list_t *alpm_list_join(alpm_list_t *first, alpm_list_t *second);
 alpm_list_t *alpm_list_mmerge(alpm_list_t *left, alpm_list_t *right, alpm_list_fn_cmp fn);