return true;
}
+static bool test_list_add_printf_NULL(struct torture_context *tctx)
+{
+ char **list = NULL;
+ str_list_add_printf(&list, "x=%d", 1);
+ torture_assert(tctx, list==NULL, "str_list_add_printf must keep NULL");
+ return true;
+}
+
+static bool test_list_add_printf(struct torture_context *tctx)
+{
+ const char *list2[] = { "foo", "bar=baz", NULL };
+ char **list = str_list_make_empty(tctx);
+ str_list_add_printf(&list, "foo");
+ str_list_add_printf(&list, "bar=%s", "baz");
+ torture_assert(
+ tctx,
+ str_list_equal((const char * const *)list, list2),
+ "str_list_add_printf failed");
+ TALLOC_FREE(list);
+ return true;
+}
+
struct torture_suite *torture_local_util_strlist(TALLOC_CTX *mem_ctx)
{
struct torture_suite *suite = torture_suite_create(mem_ctx, "strlist");
- int i;
+ size_t i;
for (i = 0; i < ARRAY_SIZE(test_lists_shell_strings); i++) {
char *name;
torture_suite_add_simple_test(suite, "list_unique_2", test_list_unique_2);
torture_suite_add_simple_test(suite, "list_append", test_list_append);
torture_suite_add_simple_test(suite, "list_append_const", test_list_append_const);
-
+ torture_suite_add_simple_test(
+ suite, "list_add_printf_NULL", test_list_add_printf_NULL);
+ torture_suite_add_simple_test(
+ suite, "list_add_printf", test_list_add_printf);
return suite;
}
return ret;
}
+/**
+ * @brief Extend a talloc'ed string list with a printf'ed string
+ *
+ * str_list_add_printf() does nothing if *plist is NULL and it sets
+ * *plist to NULL on failure. It is designed to avoid intermediate
+ * NULL checks:
+ *
+ * argv = str_list_make_empty(ctx);
+ * str_list_add_printf(&argv, "smbstatus");
+ * str_list_add_printf(&argv, "--configfile=%s", config);
+ * if (argv == NULL) {
+ * goto nomem;
+ * }
+ *
+ * @param[in,out] plist The talloc'ed list to extend
+ * @param[in] fmt The format string
+ */
+void str_list_add_printf(char ***plist, const char *fmt, ...)
+{
+ char **list = *plist;
+ size_t len;
+ char **tmp = NULL;
+ va_list ap;
+
+ if (list == NULL) {
+ return;
+ }
+ len = str_list_length((const char * const *)list);
+
+ tmp = talloc_realloc(NULL, list, char *, len+2);
+ if (tmp == NULL) {
+ goto fail;
+ }
+ list = tmp;
+ list[len+1] = NULL;
+
+ va_start(ap, fmt);
+ list[len] = talloc_vasprintf(list, fmt, ap);
+ va_end(ap);
+
+ if (list[len] == NULL) {
+ goto fail;
+ }
+ *plist = list;
+
+ return;
+fail:
+ TALLOC_FREE(list);
+ *plist = NULL;
+}
+
/**
remove an entry from a string list
*/
*/
const char **str_list_add(const char **list, const char *s);
+/*
+ * Extend a list with a printf'ed string
+ */
+void str_list_add_printf(char ***plist, const char *fmt, ...)
+ PRINTF_ATTRIBUTE(2,3);
+
/**
remove an entry from a string list
*/