chiark / gitweb /
2ebd0ee53a92199d4cd59b3d960bec9c18751ce3
[elogind.git] / src / strv.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "util.h"
29 #include "strv.h"
30
31 char *strv_find(char **l, const char *name) {
32         char **i;
33
34         assert(name);
35
36         STRV_FOREACH(i, l)
37                 if (streq(*i, name))
38                         return *i;
39
40         return NULL;
41 }
42
43 char *strv_find_prefix(char **l, const char *name) {
44         char **i;
45
46         assert(name);
47
48         STRV_FOREACH(i, l)
49                 if (startswith(*i, name))
50                         return *i;
51
52         return NULL;
53 }
54
55 void strv_free(char **l) {
56         char **k;
57
58         if (!l)
59                 return;
60
61         for (k = l; *k; k++)
62                 free(*k);
63
64         free(l);
65 }
66
67 char **strv_copy(char **l) {
68         char **r, **k;
69
70         if (!(r = new(char*, strv_length(l)+1)))
71                 return NULL;
72
73         for (k = r; *l; k++, l++)
74                 if (!(*k = strdup(*l)))
75                         goto fail;
76
77         *k = NULL;
78         return r;
79
80 fail:
81         for (k--, l--; k >= r; k--, l--)
82                 free(*k);
83
84         return NULL;
85 }
86
87 unsigned strv_length(char **l) {
88         unsigned n = 0;
89
90         if (!l)
91                 return 0;
92
93         for (; *l; l++)
94                 n++;
95
96         return n;
97 }
98
99 char **strv_new_ap(const char *x, va_list ap) {
100         const char *s;
101         char **a;
102         unsigned n = 0, i = 0;
103         va_list aq;
104
105
106         if (x) {
107                 n = 1;
108
109                 va_copy(aq, ap);
110                 while (va_arg(aq, const char*))
111                         n++;
112                 va_end(aq);
113         }
114
115         if (!(a = new(char*, n+1)))
116                 return NULL;
117
118         if (x) {
119                 if (!(a[i] = strdup(x))) {
120                         free(a);
121                         return NULL;
122                 }
123
124                 i++;
125
126                 while ((s = va_arg(ap, const char*))) {
127                         if (!(a[i] = strdup(s)))
128                                 goto fail;
129
130                         i++;
131                 }
132         }
133
134         a[i] = NULL;
135
136         return a;
137
138 fail:
139
140         for (; i > 0; i--)
141                 if (a[i-1])
142                         free(a[i-1]);
143
144         free(a);
145
146         return NULL;
147 }
148
149 char **strv_new(const char *x, ...) {
150         char **r;
151         va_list ap;
152
153         va_start(ap, x);
154         r = strv_new_ap(x, ap);
155         va_end(ap);
156
157         return r;
158 }
159
160 char **strv_merge(char **a, char **b) {
161         char **r, **k;
162
163         if (!a)
164                 return strv_copy(b);
165
166         if (!b)
167                 return strv_copy(a);
168
169         if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
170                 return NULL;
171
172         for (k = r; *a; k++, a++)
173                 if (!(*k = strdup(*a)))
174                         goto fail;
175         for (; *b; k++, b++)
176                 if (!(*k = strdup(*b)))
177                         goto fail;
178
179         *k = NULL;
180         return r;
181
182 fail:
183         for (k--; k >= r; k--)
184                 free(*k);
185
186         free(r);
187
188         return NULL;
189 }
190
191 char **strv_merge_concat(char **a, char **b, const char *suffix) {
192         char **r, **k;
193
194         /* Like strv_merge(), but appends suffix to all strings in b, before adding */
195
196         if (!b)
197                 return strv_copy(a);
198
199         if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
200                 return NULL;
201
202         for (k = r; *a; k++, a++)
203                 if (!(*k = strdup(*a)))
204                         goto fail;
205         for (; *b; k++, b++)
206                 if (!(*k = strappend(*b, suffix)))
207                         goto fail;
208
209         *k = NULL;
210         return r;
211
212 fail:
213         for (k--; k >= r; k--)
214                 free(*k);
215
216         free(r);
217
218         return NULL;
219
220 }
221
222 char **strv_split(const char *s, const char *separator) {
223         char *state;
224         char *w;
225         size_t l;
226         unsigned n, i;
227         char **r;
228
229         assert(s);
230
231         n = 0;
232         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
233                 n++;
234
235         if (!(r = new(char*, n+1)))
236                 return NULL;
237
238         i = 0;
239         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
240                 if (!(r[i++] = strndup(w, l))) {
241                         strv_free(r);
242                         return NULL;
243                 }
244
245         r[i] = NULL;
246         return r;
247 }
248
249 char **strv_split_quoted(const char *s) {
250         char *state;
251         char *w;
252         size_t l;
253         unsigned n, i;
254         char **r;
255
256         assert(s);
257
258         n = 0;
259         FOREACH_WORD_QUOTED(w, l, s, state)
260                 n++;
261
262         if (!(r = new(char*, n+1)))
263                 return NULL;
264
265         i = 0;
266         FOREACH_WORD_QUOTED(w, l, s, state)
267                 if (!(r[i++] = strndup(w, l))) {
268                         strv_free(r);
269                         return NULL;
270                 }
271
272         r[i] = NULL;
273         return r;
274 }
275
276 char *strv_join(char **l, const char *separator) {
277         char *r, *e;
278         char **s;
279         size_t n, k;
280
281         if (!separator)
282                 separator = " ";
283
284         k = strlen(separator);
285
286         n = 0;
287         STRV_FOREACH(s, l) {
288                 if (n != 0)
289                         n += k;
290                 n += strlen(*s);
291         }
292
293         if (!(r = new(char, n+1)))
294                 return NULL;
295
296         e = r;
297         STRV_FOREACH(s, l) {
298                 if (e != r)
299                         e = stpcpy(e, separator);
300
301                 e = stpcpy(e, *s);
302         }
303
304         *e = 0;
305
306         return r;
307 }
308
309 char **strv_append(char **l, const char *s) {
310         char **r, **k;
311
312         if (!l)
313                 return strv_new(s, NULL);
314
315         if (!s)
316                 return strv_copy(l);
317
318         if (!(r = new(char*, strv_length(l)+2)))
319                 return NULL;
320
321         for (k = r; *l; k++, l++)
322                 if (!(*k = strdup(*l)))
323                         goto fail;
324
325         if (!(*(k++) = strdup(s)))
326                 goto fail;
327
328         *k = NULL;
329         return r;
330
331 fail:
332         for (k--; k >= r; k--)
333                 free(*k);
334
335         free(r);
336
337         return NULL;
338 }
339
340 char **strv_uniq(char **l) {
341         char **i;
342
343         /* Drops duplicate entries. The first identical string will be
344          * kept, the others dropped */
345
346         STRV_FOREACH(i, l)
347                 strv_remove(i+1, *i);
348
349         return l;
350 }
351
352 char **strv_remove(char **l, const char *s) {
353         char **f, **t;
354
355         if (!l)
356                 return NULL;
357
358         /* Drops every occurence of s in the string list */
359
360         for (f = t = l; *f; f++) {
361
362                 if (streq(*f, s)) {
363                         free(*f);
364                         continue;
365                 }
366
367                 *(t++) = *f;
368         }
369
370         *t = NULL;
371         return l;
372 }
373
374 static int env_append(char **r, char ***k, char **a) {
375         assert(r);
376         assert(k);
377         assert(a);
378
379         /* Add the entries of a to *k unless they already exist in *r
380          * in which case they are overriden instead. This assumes
381          * there is enough space in the r */
382
383         for (; *a; a++) {
384                 char **j;
385                 size_t n = strcspn(*a, "=") + 1;
386
387                 for (j = r; j < *k; j++)
388                         if (strncmp(*j, *a, n) == 0)
389                                 break;
390
391                 if (j >= *k)
392                         (*k)++;
393                 else
394                         free(*j);
395
396                 if (!(*j = strdup(*a)))
397                         return -ENOMEM;
398         }
399
400         return 0;
401 }
402
403 char **strv_env_merge(char **x, ...) {
404         size_t n = 0;
405         char **l, **k, **r;
406         va_list ap;
407
408         /* Merges an arbitrary number of environment sets */
409
410         if (x) {
411                 n += strv_length(x);
412
413                 va_start(ap, x);
414                 while ((l = va_arg(ap, char**)))
415                         n += strv_length(l);
416                 va_end(ap);
417         }
418
419
420         if (!(r = new(char*, n+1)))
421                 return NULL;
422
423         k = r;
424
425         if (x) {
426                 if (env_append(r, &k, x) < 0)
427                         goto fail;
428
429                 va_start(ap, x);
430                 while ((l = va_arg(ap, char**)))
431                         if (env_append(r, &k, l) < 0)
432                                 goto fail;
433                 va_end(ap);
434         }
435
436         *k = NULL;
437
438         return r;
439
440 fail:
441         for (k--; k >= r; k--)
442                 free(*k);
443
444         free(r);
445
446         return NULL;
447 }
448
449 static bool env_match(const char *t, const char *pattern) {
450         assert(t);
451         assert(pattern);
452
453         /* pattern a matches string a
454          *         a matches a=
455          *         a matches a=b
456          *         a= matches a=
457          *         a=b matches a=b
458          *         a= does not match a
459          *         a=b does not match a=
460          *         a=b does not match a
461          *         a=b does not match a=c */
462
463         if (streq(t, pattern))
464                 return true;
465
466         if (!strchr(pattern, '=')) {
467                 size_t l = strlen(pattern);
468
469                 return strncmp(t, pattern, l) == 0 && t[l] == '=';
470         }
471
472         return false;
473 }
474
475 char **strv_env_delete(char **x, ...) {
476         size_t n = 0, i = 0;
477         char **l, **k, **r, **j;
478         va_list ap;
479
480         /* Deletes every entry fromx that is mentioned in the other
481          * string lists */
482
483         n = strv_length(x);
484
485         if (!(r = new(char*, n+1)))
486                 return NULL;
487
488         STRV_FOREACH(k, x) {
489                 va_start(ap, x);
490
491                 while ((l = va_arg(ap, char**)))
492                         STRV_FOREACH(j, l)
493                                 if (env_match(*k, *j))
494                                         goto delete;
495
496                 va_end(ap);
497
498                 if (!(r[i++] = strdup(*k))) {
499                         strv_free(r);
500                         return NULL;
501                 }
502
503                 continue;
504
505         delete:
506                 va_end(ap);
507         }
508
509         r[i] = NULL;
510
511         assert(i <= n);
512
513         return r;
514 }