X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fstrv.c;h=607c221ae66790b86311bdc36625039e2f401c20;hb=0371ca0dac1d70b2e5060a3c4e6fbbc2bdbd8671;hp=e57e0ee7bfad9919a09893453396354d9eaf355c;hpb=4468addca6d01a0d2d154371dd72f54307a9c786;p=elogind.git diff --git a/src/shared/strv.c b/src/shared/strv.c index e57e0ee7b..607c221ae 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -64,7 +64,7 @@ void strv_free(char **l) { free(l); } -char **strv_copy(char **l) { +char **strv_copy(char * const *l) { char **r, **k; k = r = new(char*, strv_length(l) + 1); @@ -84,7 +84,7 @@ char **strv_copy(char **l) { return r; } -unsigned strv_length(char **l) { +unsigned strv_length(char * const *l) { unsigned n = 0; if (!l) @@ -356,6 +356,43 @@ char *strv_join(char **l, const char *separator) { return r; } +char *strv_join_quoted(char **l) { + char *buf = NULL; + char **s; + size_t allocated = 0, len = 0; + + STRV_FOREACH(s, l) { + /* assuming here that escaped string cannot be more + * than twice as long, and reserving space for the + * separator and quotes. + */ + _cleanup_free_ char *esc = NULL; + size_t needed; + + if (!GREEDY_REALLOC(buf, allocated, + len + strlen(*s) * 2 + 3)) + goto oom; + + esc = cescape(*s); + if (!esc) + goto oom; + + needed = snprintf(buf + len, allocated - len, "%s\"%s\"", + len > 0 ? " " : "", esc); + assert(needed < allocated - len); + len += needed; + } + + if (!buf) + buf = malloc0(1); + + return buf; + + oom: + free(buf); + return NULL; +} + char **strv_append(char **l, const char *s) { char **r, **k; @@ -494,7 +531,7 @@ char **strv_parse_nulstr(const char *s, size_t l) { assert(s || l <= 0); if (l <= 0) - return strv_new(NULL, NULL); + return new0(char*, 1); for (p = s; p < s + l; p++) if (*p == 0)