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