Commit Diff


commit - 4c069210fd4934e5336219e2102e6fd93d3d5eb9
commit + 1e0d215510627c58cc63bdfe4282801251ee9c75
blob - 5df3e41163e57c4b5be0c39c6045fac4c594beb9
blob + 61b1e283e8555a54cac92a2925f65d1eaa1fbf0e
--- stringlist.c
+++ stringlist.c
@@ -89,3 +89,44 @@ sl_free(StringList *sl, int all)
 	}
 	free(sl);
 }
+
+/*
+ * sl_str(): Flatten a string list to a string, separating
+ * strings in the list with a separator.
+ * Return NULL on failure. Result must be freed by caller.
+ */
+char *
+sl_str(StringList *sl, const char *sep)
+{
+	size_t len = 0;
+	int i;
+	char *s;
+
+	for (i = 0; i < sl->sl_cur; i++) {
+		len += strlen(sl->sl_str[i]);
+		if (i + 1 < sl->sl_cur)
+			len += strlen(sep);
+	}
+
+	if (len == 0)
+		return strdup("");
+
+	s = malloc(len + 1);	
+	if (s == NULL)
+		return NULL;
+
+	s[0] = '\0';
+	for (i = 0; i < sl->sl_cur; i++) {
+		if (strlcat(s, sl->sl_str[i], len + 1) >= len + 1) {
+			free(s);
+			return NULL;
+		}
+		if (i + 1 < sl->sl_cur &&
+		    strlcat(s, sep, len + 1) >= len + 1) {
+			free(s);
+			return NULL;
+		}
+	}
+
+	return s;
+}
blob - 442657be4144cffa62829c3a401d2600e0105a47
blob + dda36dfb8ddded5c9a286750c3ad4aa0eaa05232
--- stringlist.h
+++ stringlist.h
@@ -46,6 +46,7 @@ StringList	*sl_init(void);
 void		 sl_add(StringList *, char *);
 void		 sl_free(StringList *, int);
 char		*sl_find(StringList *, char *);
+char *		sl_str(StringList *, const char *);
 __END_DECLS
 
 #endif /* _STRINGLIST_H */