]>
git.ipfire.org Git - people/ms/systemd.git/blob - strv.c
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
--)
169 char **strv_merge_concat(char **a
, char **b
, const char *suffix
) {
172 /* Like strv_merge(), but appends suffix to all strings in b, before adding */
177 if (!(r
= new(char*, strv_length(a
)+strv_length(b
)+1)))
180 for (k
= r
; *a
; k
++, a
++)
181 if (!(*k
= strdup(*a
)))
184 if (!(*k
= strappend(*b
, suffix
)))
191 for (k
--; k
>= r
; k
--)
198 char **strv_split(const char *s
, const char *separator
) {
208 FOREACH_WORD_SEPARATOR(w
, l
, s
, separator
, state
)
211 if (!(r
= new(char*, n
+1)))
215 FOREACH_WORD_SEPARATOR(w
, l
, s
, separator
, state
)
216 if (!(r
[i
++] = strndup(w
, l
))) {
225 char **strv_split_quoted(const char *s
) {
235 FOREACH_WORD_QUOTED(w
, l
, s
, state
)
238 if (!(r
= new(char*, n
+1)))
242 FOREACH_WORD_QUOTED(w
, l
, s
, state
)
243 if (!(r
[i
++] = strndup(w
, l
))) {
252 char *strv_join(char **l
, const char *separator
) {
260 k
= strlen(separator
);
269 if (!(r
= new(char, n
+1)))
275 e
= stpcpy(e
, separator
);
283 char **strv_append(char **l
, const char *s
) {
287 return strv_new(s
, NULL
);
292 if (!(r
= new(char*, strv_length(l
)+2)))
295 for (k
= r
; *l
; k
++, l
++)
296 if (!(*k
= strdup(*l
)))
298 if (!(*(k
++) = strdup(s
)))
305 for (k
--; k
>= r
; k
--)
311 char **strv_uniq(char **l
) {
314 /* Drops duplicate entries. The first identical string will be
315 * kept, the others dropped */
318 strv_remove(i
+1, *i
);
323 char **strv_remove(char **l
, const char *s
) {
329 /* Drops every occurence of s in the string list */
331 for (f
= t
= l
; *f
; f
++) {