chiark / gitweb /
systemctl: make -f short for both --follow and --force
[elogind.git] / src / strv.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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         k = r = new(char*, strv_length(l)+1);
71         if (!k)
72                 return NULL;
73
74         if (l)
75                 for (; *l; k++, l++)
76                         if (!(*k = strdup(*l)))
77                                 goto fail;
78
79         *k = NULL;
80         return r;
81
82 fail:
83         for (k--; k >= r; k--)
84                 free(*k);
85
86         free(r);
87
88         return NULL;
89 }
90
91 unsigned strv_length(char **l) {
92         unsigned n = 0;
93
94         if (!l)
95                 return 0;
96
97         for (; *l; l++)
98                 n++;
99
100         return n;
101 }
102
103 char **strv_new_ap(const char *x, va_list ap) {
104         const char *s;
105         char **a;
106         unsigned n = 0, i = 0;
107         va_list aq;
108
109         if (x) {
110                 n = 1;
111
112                 va_copy(aq, ap);
113                 while (va_arg(aq, const char*))
114                         n++;
115                 va_end(aq);
116         }
117
118         if (!(a = new(char*, n+1)))
119                 return NULL;
120
121         if (x) {
122                 if (!(a[i] = strdup(x))) {
123                         free(a);
124                         return NULL;
125                 }
126
127                 i++;
128
129                 while ((s = va_arg(ap, const char*))) {
130                         if (!(a[i] = strdup(s)))
131                                 goto fail;
132
133                         i++;
134                 }
135         }
136
137         a[i] = NULL;
138
139         return a;
140
141 fail:
142
143         for (; i > 0; i--)
144                 if (a[i-1])
145                         free(a[i-1]);
146
147         free(a);
148
149         return NULL;
150 }
151
152 char **strv_new(const char *x, ...) {
153         char **r;
154         va_list ap;
155
156         va_start(ap, x);
157         r = strv_new_ap(x, ap);
158         va_end(ap);
159
160         return r;
161 }
162
163 char **strv_merge(char **a, char **b) {
164         char **r, **k;
165
166         if (!a)
167                 return strv_copy(b);
168
169         if (!b)
170                 return strv_copy(a);
171
172         if (!(r = new(char*, strv_length(a)+strv_length(b)+1)))
173                 return NULL;
174
175         for (k = r; *a; k++, a++)
176                 if (!(*k = strdup(*a)))
177                         goto fail;
178         for (; *b; k++, b++)
179                 if (!(*k = strdup(*b)))
180                         goto fail;
181
182         *k = NULL;
183         return r;
184
185 fail:
186         for (k--; k >= r; k--)
187                 free(*k);
188
189         free(r);
190
191         return NULL;
192 }
193
194 char **strv_merge_concat(char **a, char **b, const char *suffix) {
195         char **r, **k;
196
197         /* Like strv_merge(), but appends suffix to all strings in b, before adding */
198
199         if (!b)
200                 return strv_copy(a);
201
202         r = new(char*, strv_length(a) + strv_length(b) + 1);
203         if (!r)
204                 return NULL;
205
206         k = r;
207         if (a)
208                 for (; *a; k++, a++) {
209                         *k = strdup(*a);
210                         if (!*k)
211                                 goto fail;
212                 }
213
214         for (; *b; k++, b++) {
215                 *k = strappend(*b, suffix);
216                 if (!*k)
217                         goto fail;
218         }
219
220         *k = NULL;
221         return r;
222
223 fail:
224         for (k--; k >= r; k--)
225                 free(*k);
226
227         free(r);
228
229         return NULL;
230
231 }
232
233 char **strv_split(const char *s, const char *separator) {
234         char *state;
235         char *w;
236         size_t l;
237         unsigned n, i;
238         char **r;
239
240         assert(s);
241
242         n = 0;
243         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
244                 n++;
245
246         if (!(r = new(char*, n+1)))
247                 return NULL;
248
249         i = 0;
250         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
251                 if (!(r[i++] = strndup(w, l))) {
252                         strv_free(r);
253                         return NULL;
254                 }
255
256         r[i] = NULL;
257         return r;
258 }
259
260 char **strv_split_quoted(const char *s) {
261         char *state;
262         char *w;
263         size_t l;
264         unsigned n, i;
265         char **r;
266
267         assert(s);
268
269         n = 0;
270         FOREACH_WORD_QUOTED(w, l, s, state)
271                 n++;
272
273         if (!(r = new(char*, n+1)))
274                 return NULL;
275
276         i = 0;
277         FOREACH_WORD_QUOTED(w, l, s, state)
278                 if (!(r[i++] = cunescape_length(w, l))) {
279                         strv_free(r);
280                         return NULL;
281                 }
282
283         r[i] = NULL;
284         return r;
285 }
286
287 char *strv_join(char **l, const char *separator) {
288         char *r, *e;
289         char **s;
290         size_t n, k;
291
292         if (!separator)
293                 separator = " ";
294
295         k = strlen(separator);
296
297         n = 0;
298         STRV_FOREACH(s, l) {
299                 if (n != 0)
300                         n += k;
301                 n += strlen(*s);
302         }
303
304         if (!(r = new(char, n+1)))
305                 return NULL;
306
307         e = r;
308         STRV_FOREACH(s, l) {
309                 if (e != r)
310                         e = stpcpy(e, separator);
311
312                 e = stpcpy(e, *s);
313         }
314
315         *e = 0;
316
317         return r;
318 }
319
320 char **strv_append(char **l, const char *s) {
321         char **r, **k;
322
323         if (!l)
324                 return strv_new(s, NULL);
325
326         if (!s)
327                 return strv_copy(l);
328
329         r = new(char*, strv_length(l)+2);
330         if (!r)
331                 return NULL;
332
333         for (k = r; *l; k++, l++)
334                 if (!(*k = strdup(*l)))
335                         goto fail;
336
337         if (!(*(k++) = strdup(s)))
338                 goto fail;
339
340         *k = NULL;
341         return r;
342
343 fail:
344         for (k--; k >= r; k--)
345                 free(*k);
346
347         free(r);
348
349         return NULL;
350 }
351
352 char **strv_uniq(char **l) {
353         char **i;
354
355         /* Drops duplicate entries. The first identical string will be
356          * kept, the others dropped */
357
358         STRV_FOREACH(i, l)
359                 strv_remove(i+1, *i);
360
361         return l;
362 }
363
364 char **strv_remove(char **l, const char *s) {
365         char **f, **t;
366
367         if (!l)
368                 return NULL;
369
370         assert(s);
371
372         /* Drops every occurrence of s in the string list, edits
373          * in-place. */
374
375         for (f = t = l; *f; f++) {
376
377                 if (streq(*f, s)) {
378                         free(*f);
379                         continue;
380                 }
381
382                 *(t++) = *f;
383         }
384
385         *t = NULL;
386         return l;
387 }
388
389 static int env_append(char **r, char ***k, char **a) {
390         assert(r);
391         assert(k);
392
393         if (!a)
394                 return 0;
395
396         /* Add the entries of a to *k unless they already exist in *r
397          * in which case they are overridden instead. This assumes
398          * there is enough space in the r array. */
399
400         for (; *a; a++) {
401                 char **j;
402                 size_t n;
403
404                 n = strcspn(*a, "=");
405
406                 if ((*a)[n] == '=')
407                         n++;
408
409                 for (j = r; j < *k; j++)
410                         if (strncmp(*j, *a, n) == 0)
411                                 break;
412
413                 if (j >= *k)
414                         (*k)++;
415                 else
416                         free(*j);
417
418                 if (!(*j = strdup(*a)))
419                         return -ENOMEM;
420         }
421
422         return 0;
423 }
424
425 char **strv_env_merge(unsigned n_lists, ...) {
426         size_t n = 0;
427         char **l, **k, **r;
428         va_list ap;
429         unsigned i;
430
431         /* Merges an arbitrary number of environment sets */
432
433         va_start(ap, n_lists);
434         for (i = 0; i < n_lists; i++) {
435                 l = va_arg(ap, char**);
436                 n += strv_length(l);
437         }
438         va_end(ap);
439
440         if (!(r = new(char*, n+1)))
441                 return NULL;
442
443         k = r;
444
445         va_start(ap, n_lists);
446         for (i = 0; i < n_lists; i++) {
447                 l = va_arg(ap, char**);
448                 if (env_append(r, &k, l) < 0)
449                         goto fail;
450         }
451         va_end(ap);
452
453         *k = NULL;
454
455         return r;
456
457 fail:
458         va_end(ap);
459
460         for (k--; k >= r; k--)
461                 free(*k);
462
463         free(r);
464
465         return NULL;
466 }
467
468 static bool env_match(const char *t, const char *pattern) {
469         assert(t);
470         assert(pattern);
471
472         /* pattern a matches string a
473          *         a matches a=
474          *         a matches a=b
475          *         a= matches a=
476          *         a=b matches a=b
477          *         a= does not match a
478          *         a=b does not match a=
479          *         a=b does not match a
480          *         a=b does not match a=c */
481
482         if (streq(t, pattern))
483                 return true;
484
485         if (!strchr(pattern, '=')) {
486                 size_t l = strlen(pattern);
487
488                 return strncmp(t, pattern, l) == 0 && t[l] == '=';
489         }
490
491         return false;
492 }
493
494 char **strv_env_delete(char **x, unsigned n_lists, ...) {
495         size_t n, i = 0;
496         char **k, **r;
497         va_list ap;
498
499         /* Deletes every entry from x that is mentioned in the other
500          * string lists */
501
502         n = strv_length(x);
503
504         r = new(char*, n+1);
505         if (!r)
506                 return NULL;
507
508         STRV_FOREACH(k, x) {
509                 unsigned v;
510
511                 va_start(ap, n_lists);
512                 for (v = 0; v < n_lists; v++) {
513                         char **l, **j;
514
515                         l = va_arg(ap, char**);
516                         STRV_FOREACH(j, l)
517                                 if (env_match(*k, *j))
518                                         goto skip;
519                 }
520                 va_end(ap);
521
522                 r[i] = strdup(*k);
523                 if (!r[i]) {
524                         strv_free(r);
525                         return NULL;
526                 }
527
528                 i++;
529                 continue;
530
531         skip:
532                 va_end(ap);
533         }
534
535         r[i] = NULL;
536
537         assert(i <= n);
538
539         return r;
540 }
541
542 char **strv_env_unset(char **l, const char *p) {
543
544         char **f, **t;
545
546         if (!l)
547                 return NULL;
548
549         assert(p);
550
551         /* Drops every occurrence of the env var setting p in the
552          * string list. edits in-place. */
553
554         for (f = t = l; *f; f++) {
555
556                 if (env_match(*f, p)) {
557                         free(*f);
558                         continue;
559                 }
560
561                 *(t++) = *f;
562         }
563
564         *t = NULL;
565         return l;
566 }
567
568 char **strv_env_set(char **x, const char *p) {
569
570         char **k, **r;
571         char* m[2] = { (char*) p, NULL };
572
573         /* Overrides the env var setting of p, returns a new copy */
574
575         if (!(r = new(char*, strv_length(x)+2)))
576                 return NULL;
577
578         k = r;
579         if (env_append(r, &k, x) < 0)
580                 goto fail;
581
582         if (env_append(r, &k, m) < 0)
583                 goto fail;
584
585         *k = NULL;
586
587         return r;
588
589 fail:
590         for (k--; k >= r; k--)
591                 free(*k);
592
593         free(r);
594
595         return NULL;
596
597 }
598
599 char *strv_env_get_with_length(char **l, const char *name, size_t k) {
600         char **i;
601
602         assert(name);
603
604         STRV_FOREACH(i, l)
605                 if (strncmp(*i, name, k) == 0 &&
606                     (*i)[k] == '=')
607                         return *i + k + 1;
608
609         return NULL;
610 }
611
612 char *strv_env_get(char **l, const char *name) {
613         return strv_env_get_with_length(l, name, strlen(name));
614 }
615
616 char **strv_env_clean(char **l) {
617         char **r, **ret;
618
619         for (r = ret = l; *l; l++) {
620                 const char *equal;
621
622                 equal = strchr(*l, '=');
623
624                 if (equal && equal[1] == 0) {
625                         free(*l);
626                         continue;
627                 }
628
629                 *(r++) = *l;
630         }
631
632         *r = NULL;
633
634         return ret;
635 }
636
637 char **strv_parse_nulstr(const char *s, size_t l) {
638         const char *p;
639         unsigned c = 0, i = 0;
640         char **v;
641
642         assert(s || l <= 0);
643
644         if (l <= 0)
645                 return strv_new(NULL, NULL);
646
647         for (p = s; p < s + l; p++)
648                 if (*p == 0)
649                         c++;
650
651         if (s[l-1] != 0)
652                 c++;
653
654         if (!(v = new0(char*, c+1)))
655                 return NULL;
656
657         p = s;
658         while (p < s + l) {
659                 const char *e;
660
661                 e = memchr(p, 0, s + l - p);
662
663                 if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
664                         strv_free(v);
665                         return NULL;
666                 }
667
668                 if (!e)
669                         break;
670
671                 p = e + 1;
672         }
673
674         assert(i == c);
675
676         return v;
677 }
678
679 bool strv_overlap(char **a, char **b) {
680         char **i, **j;
681
682         STRV_FOREACH(i, a) {
683                 STRV_FOREACH(j, b) {
684                         if (streq(*i, *j))
685                                 return true;
686                 }
687         }
688
689         return false;
690 }