Commit Diff


commit - 41d076f23c766a71323a3c7fbe9889cb54752f1a
commit + dd8d34bfed2b33ee645455192b05112a431b656b
blob - 5df3e41163e57c4b5be0c39c6045fac4c594beb9
blob + 510b7b0d1edc70f514ce87d1b609121ea463af7d
--- stringlist.c
+++ stringlist.c
@@ -89,3 +89,44 @@ sl_free(StringList *sl, int all)
 	}
 	free(sl);
 }
+
+/*
+ * sl_makestr(): Flatten a string list to a string, separating
+ * strings in the list with the given separator.
+ * Return NULL on failure. Result must be freed by caller.
+ */
+char *
+sl_makestr(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 NULL;
+
+	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 + 814cdc5bcf85c539d771be86c9b1e7504016b3fd
--- 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_makestr(StringList *, const char *);
 __END_DECLS
 
 #endif /* _STRINGLIST_H */