chiark / gitweb /
strv: introduce new STRV_MAKE and STRV_MAKE_EMPTY macros to create string arrays...
authorLennart Poettering <lennart@poettering.net>
Tue, 29 Oct 2013 19:09:16 +0000 (20:09 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 29 Oct 2013 19:09:16 +0000 (20:09 +0100)
src/shared/strv.h
src/test/test-strv.c

index 737728a3c6bde296b262a9d0a09177b4227a9eed..6ce21acdbdeaa05809bc56452a34405b65ed054c 100644 (file)
@@ -86,12 +86,16 @@ bool strv_overlap(char **a, char **b) _pure_;
 char **strv_sort(char **l);
 void strv_print(char **l);
 
+#define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
+
+#define STRV_MAKE_EMPTY ((char*[1]) { NULL })
+
 #define strv_from_stdarg_alloca(first)                          \
         ({                                                      \
                 char **_l;                                      \
                                                                 \
                 if (!first)                                     \
-                        _l = ((char*[1]) { NULL });             \
+                        _l = (char**) &first;                   \
                 else {                                          \
                         unsigned _n;                            \
                         va_list _ap;                            \
index 7002b8b1c0dea144835a83c768f8c5e7afa7c2a1..f32d02ed8574cebd50a39882028d62e9b0872e36 100644 (file)
@@ -324,7 +324,7 @@ static void test_strv_foreach_pair(void) {
         }
 }
 
-static void test_strv_from_stdarg_alloca_one(const char **l, const char *first, ...) {
+static void test_strv_from_stdarg_alloca_one(char **l, const char *first, ...) {
         char **j;
         unsigned i;
 
@@ -339,9 +339,9 @@ static void test_strv_from_stdarg_alloca_one(const char **l, const char *first,
 }
 
 static void test_strv_from_stdarg_alloca(void) {
-        test_strv_from_stdarg_alloca_one((const char*[]) { "foo", "bar", NULL }, "foo", "bar", NULL);
-        test_strv_from_stdarg_alloca_one((const char*[]) { "foo", "bar", NULL }, "foo", "bar", NULL);
-        test_strv_from_stdarg_alloca_one((const char*[]) { "foo", NULL }, "foo", NULL);
+        test_strv_from_stdarg_alloca_one(STRV_MAKE("foo", "bar"), "foo", "bar", NULL);
+        test_strv_from_stdarg_alloca_one(STRV_MAKE("foo"), "foo", NULL);
+        test_strv_from_stdarg_alloca_one(STRV_MAKE_EMPTY, NULL);
 }
 
 int main(int argc, char *argv[]) {