From 250a918dc4c8a15d927deecc3b3f6a0604657ae4 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 29 Oct 2013 19:53:43 +0100 Subject: 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. --- src/shared/strv.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/shared/strv.h') diff --git a/src/shared/strv.h b/src/shared/strv.h index cccf2e6a92..737728a3c6 100644 --- a/src/shared/strv.h +++ b/src/shared/strv.h @@ -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; \ + }) -- cgit v1.2.3-54-g00ecf