strv: introduce new strv_from_stdarg_alloca() macro to generate a string array from stdarg function parameters

This allows us to turn lists of strings passed in easily into string
arrays without having to allocate memory.
This commit is contained in:
Lennart Poettering
2013-10-29 19:53:43 +01:00
parent 7b1132f60d
commit 250a918dc4
5 changed files with 76 additions and 49 deletions
+2 -8
View File
@@ -148,18 +148,12 @@ int conf_files_list_strv(char ***strv, const char *suffix, const char *root, con
}
int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
_cleanup_strv_free_ char **dirs = NULL;
va_list ap;
char **dirs;
assert(strv);
assert(suffix);
va_start(ap, dir);
dirs = strv_new_ap(dir, ap);
va_end(ap);
if (!dirs)
return -ENOMEM;
dirs = strv_from_stdarg_alloca(dir);
return conf_files_list_strv_internal(strv, suffix, root, dirs);
}
+29
View File
@@ -85,3 +85,32 @@ bool strv_overlap(char **a, char **b) _pure_;
char **strv_sort(char **l);
void strv_print(char **l);
#define strv_from_stdarg_alloca(first) \
({ \
char **_l; \
\
if (!first) \
_l = ((char*[1]) { NULL }); \
else { \
unsigned _n; \
va_list _ap; \
\
_n = 1; \
va_start(_ap, first); \
while (va_arg(_ap, char*)) \
_n++; \
va_end(_ap); \
\
_l = newa(char*, _n+1); \
_l[_n = 0] = (char*) first; \
va_start(_ap, first); \
for (;;) { \
_l[++_n] = va_arg(_ap, char*); \
if (!_l[_n]) \
break; \
} \
va_end(_ap); \
} \
_l; \
})