X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fstrv.c;h=2df478f30bbce900cb3e2b0673b61a1e4eada0c2;hb=8b179a830a789746cce0be6671e2de235e3b0ea9;hp=a5ce7e9593652f9f095b1801454fd061caf69034;hpb=2fd9ae2e9bd585312e15f8383036caee704822fb;p=elogind.git diff --git a/src/shared/strv.c b/src/shared/strv.c index a5ce7e959..2df478f30 100644 --- a/src/shared/strv.c +++ b/src/shared/strv.c @@ -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; @@ -387,6 +424,21 @@ fail: return NULL; } +char **strv_appendf(char **l, const char *format, ...) { + va_list ap; + _cleanup_free_ char *s = NULL; + int r; + + va_start(ap, format); + r = vasprintf(&s, format, ap); + va_end(ap); + + if (r < 0) + return NULL; + + return strv_append(l, s); +} + int strv_push(char ***l, char *value) { char **c; unsigned n;