1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 char *strv_find(char **l, const char *name) {
43 char *strv_find_prefix(char **l, const char *name) {
49 if (startswith(*i, name))
55 void strv_free(char **l) {
67 char **strv_copy(char **l) {
70 if (!(r = new(char*, strv_length(l)+1)))
73 for (k = r; *l; k++, l++)
74 if (!(*k = strdup(*l)))
81 for (k--, l--; k >= r; k--, l--)
87 unsigned strv_length(char **l) {
99 char **strv_new_ap(const char *x, va_list ap) {
102 unsigned n = 0, i = 0;
110 while (va_arg(aq, const char*))
115 if (!(a = new(char*, n+1)))
119 if (!(a[i] = strdup(x))) {
126 while ((s = va_arg(ap, const char*))) {
127 if (!(a[i] = strdup(s)))
149 char **strv_new(const char *x, ...) {
154 r = strv_new_ap(x, ap);
160 char **strv_merge(char **a, char **b) {
169 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
172 for (k = r; *a; k++, a++)
173 if (!(*k = strdup(*a)))
176 if (!(*k = strdup(*b)))
183 for (k--; k >= r; k--)
191 char **strv_merge_concat(char **a, char **b, const char *suffix) {
194 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
199 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
202 for (k = r; *a; k++, a++)
203 if (!(*k = strdup(*a)))
206 if (!(*k = strappend(*b, suffix)))
213 for (k--; k >= r; k--)
222 char **strv_split(const char *s, const char *separator) {
232 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
235 if (!(r = new(char*, n+1)))
239 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
240 if (!(r[i++] = strndup(w, l))) {
249 char **strv_split_quoted(const char *s) {
259 FOREACH_WORD_QUOTED(w, l, s, state)
262 if (!(r = new(char*, n+1)))
266 FOREACH_WORD_QUOTED(w, l, s, state)
267 if (!(r[i++] = strndup(w, l))) {
276 char *strv_join(char **l, const char *separator) {
284 k = strlen(separator);
293 if (!(r = new(char, n+1)))
299 e = stpcpy(e, separator);
309 char **strv_append(char **l, const char *s) {
313 return strv_new(s, NULL);
318 if (!(r = new(char*, strv_length(l)+2)))
321 for (k = r; *l; k++, l++)
322 if (!(*k = strdup(*l)))
325 if (!(*(k++) = strdup(s)))
332 for (k--; k >= r; k--)
340 char **strv_uniq(char **l) {
343 /* Drops duplicate entries. The first identical string will be
344 * kept, the others dropped */
347 strv_remove(i+1, *i);
352 char **strv_remove(char **l, const char *s) {
358 /* Drops every occurence of s in the string list */
360 for (f = t = l; *f; f++) {
374 static int env_append(char **r, char ***k, char **a) {
381 /* Add the entries of a to *k unless they already exist in *r
382 * in which case they are overriden instead. This assumes
383 * there is enough space in the r */
387 size_t n = strcspn(*a, "=") + 1;
389 for (j = r; j < *k; j++)
390 if (strncmp(*j, *a, n) == 0)
398 if (!(*j = strdup(*a)))
405 char **strv_env_merge(unsigned n_lists, ...) {
411 /* Merges an arbitrary number of environment sets */
413 va_start(ap, n_lists);
414 for (i = 0; i < n_lists; i++) {
415 l = va_arg(ap, char**);
420 if (!(r = new(char*, n+1)))
425 va_start(ap, n_lists);
426 for (i = 0; i < n_lists; i++) {
427 l = va_arg(ap, char**);
428 if (env_append(r, &k, l) < 0)
438 for (k--; k >= r; k--)
446 static bool env_match(const char *t, const char *pattern) {
450 /* pattern a matches string a
455 * a= does not match a
456 * a=b does not match a=
457 * a=b does not match a
458 * a=b does not match a=c */
460 if (streq(t, pattern))
463 if (!strchr(pattern, '=')) {
464 size_t l = strlen(pattern);
466 return strncmp(t, pattern, l) == 0 && t[l] == '=';
472 char **strv_env_delete(char **x, unsigned n_lists, ...) {
474 char **l, **k, **r, **j;
477 /* Deletes every entry fromx that is mentioned in the other
482 if (!(r = new(char*, n+1)))
486 va_start(ap, n_lists);
488 for (i = 0; i < n_lists; i++) {
489 l = va_arg(ap, char**);
491 if (env_match(*k, *j))
497 if (!(r[i++] = strdup(*k))) {
515 char **strv_env_set(char **x, const char *p) {
519 if (!(r = new(char*, strv_length(x)+2)))
523 if (env_append(r, &k, x) < 0)
526 if (!(*(k++) = strdup(p)))
534 for (k--; k >= r; k--)