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/>.
30 char *strv_find(char **l, const char *name) {
43 void strv_free(char **l) {
55 char **strv_copy(char **l) {
58 if (!(r = new(char*, strv_length(l)+1)))
61 for (k = r; *l; k++, l++)
62 if (!(*k = strdup(*l)))
69 for (k--, l--; k >= r; k--, l--)
75 unsigned strv_length(char **l) {
87 char **strv_new(const char *x, ...) {
90 unsigned n = 0, i = 0;
98 while (va_arg(ap, const char*))
104 if (!(a = new(char*, n+1)))
108 if (!(a[i] = strdup(x))) {
117 while ((s = va_arg(ap, const char*))) {
118 if (!(a[i] = strdup(s)))
140 char **strv_merge(char **a, char **b) {
149 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
152 for (k = r; *a; k++, a++)
153 if (!(*k = strdup(*a)))
156 if (!(*k = strdup(*b)))
163 for (k--; k >= r; k--)
171 char **strv_merge_concat(char **a, char **b, const char *suffix) {
174 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
179 if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
182 for (k = r; *a; k++, a++)
183 if (!(*k = strdup(*a)))
186 if (!(*k = strappend(*b, suffix)))
193 for (k--; k >= r; k--)
202 char **strv_split(const char *s, const char *separator) {
212 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
215 if (!(r = new(char*, n+1)))
219 FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
220 if (!(r[i++] = strndup(w, l))) {
229 char **strv_split_quoted(const char *s) {
239 FOREACH_WORD_QUOTED(w, l, s, state)
242 if (!(r = new(char*, n+1)))
246 FOREACH_WORD_QUOTED(w, l, s, state)
247 if (!(r[i++] = strndup(w, l))) {
256 char *strv_join(char **l, const char *separator) {
264 k = strlen(separator);
273 if (!(r = new(char, n+1)))
279 e = stpcpy(e, separator);
289 char **strv_append(char **l, const char *s) {
293 return strv_new(s, NULL);
298 if (!(r = new(char*, strv_length(l)+2)))
301 for (k = r; *l; k++, l++)
302 if (!(*k = strdup(*l)))
304 if (!(*(k++) = strdup(s)))
311 for (k--; k >= r; k--)
319 char **strv_uniq(char **l) {
322 /* Drops duplicate entries. The first identical string will be
323 * kept, the others dropped */
326 strv_remove(i+1, *i);
331 char **strv_remove(char **l, const char *s) {
337 /* Drops every occurence of s in the string list */
339 for (f = t = l; *f; f++) {