chiark / gitweb /
coverity: fix a couple of bugs found by coverity
[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         k = r;
205         if (a)
206                 for (; *a; k++, a++) {
207                         *k = strdup(*a);
208                         if (!*k)
209                                 goto fail;
210                 }
211
212         for (; *b; k++, b++) {
213                 *k = strappend(*b, suffix);
214                 if (!*k)
215                         goto fail;
216         }
217
218         *k = NULL;
219         return r;
220
221 fail:
222         for (k--; k >= r; k--)
223                 free(*k);
224
225         free(r);
226
227         return NULL;
228
229 }
230
231 char **strv_split(const char *s, const char *separator) {
232         char *state;
233         char *w;
234         size_t l;
235         unsigned n, i;
236         char **r;
237
238         assert(s);
239
240         n = 0;
241         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
242                 n++;
243
244         if (!(r = new(char*, n+1)))
245                 return NULL;
246
247         i = 0;
248         FOREACH_WORD_SEPARATOR(w, l, s, separator, state)
249                 if (!(r[i++] = strndup(w, l))) {
250                         strv_free(r);
251                         return NULL;
252                 }
253
254         r[i] = NULL;
255         return r;
256 }
257
258 char **strv_split_quoted(const char *s) {
259         char *state;
260         char *w;
261         size_t l;
262         unsigned n, i;
263         char **r;
264
265         assert(s);
266
267         n = 0;
268         FOREACH_WORD_QUOTED(w, l, s, state)
269                 n++;
270
271         if (!(r = new(char*, n+1)))
272                 return NULL;
273
274         i = 0;
275         FOREACH_WORD_QUOTED(w, l, s, state)
276                 if (!(r[i++] = cunescape_length(w, l))) {
277                         strv_free(r);
278                         return NULL;
279                 }
280
281         r[i] = NULL;
282         return r;
283 }
284
285 char *strv_join(char **l, const char *separator) {
286         char *r, *e;
287         char **s;
288         size_t n, k;
289
290         if (!separator)
291                 separator = " ";
292
293         k = strlen(separator);
294
295         n = 0;
296         STRV_FOREACH(s, l) {
297                 if (n != 0)
298                         n += k;
299                 n += strlen(*s);
300         }
301
302         if (!(r = new(char, n+1)))
303                 return NULL;
304
305         e = r;
306         STRV_FOREACH(s, l) {
307                 if (e != r)
308                         e = stpcpy(e, separator);
309
310                 e = stpcpy(e, *s);
311         }
312
313         *e = 0;
314
315         return r;
316 }
317
318 char **strv_append(char **l, const char *s) {
319         char **r, **k;
320
321         if (!l)
322                 return strv_new(s, NULL);
323
324         if (!s)
325                 return strv_copy(l);
326
327         if (!(r = new(char*, strv_length(l)+2)))
328                 return NULL;
329
330         for (k = r; *l; k++, l++)
331                 if (!(*k = strdup(*l)))
332                         goto fail;
333
334         if (!(*(k++) = strdup(s)))
335                 goto fail;
336
337         *k = NULL;
338         return r;
339
340 fail:
341         for (k--; k >= r; k--)
342                 free(*k);
343
344         free(r);
345
346         return NULL;
347 }
348
349 char **strv_uniq(char **l) {
350         char **i;
351
352         /* Drops duplicate entries. The first identical string will be
353          * kept, the others dropped */
354
355         STRV_FOREACH(i, l)
356                 strv_remove(i+1, *i);
357
358         return l;
359 }
360
361 char **strv_remove(char **l, const char *s) {
362         char **f, **t;
363
364         if (!l)
365                 return NULL;
366
367         assert(s);
368
369         /* Drops every occurrence of s in the string list, edits
370          * in-place. */
371
372         for (f = t = l; *f; f++) {
373
374                 if (streq(*f, s)) {
375                         free(*f);
376                         continue;
377                 }
378
379                 *(t++) = *f;
380         }
381
382         *t = NULL;
383         return l;
384 }
385
386 static int env_append(char **r, char ***k, char **a) {
387         assert(r);
388         assert(k);
389
390         if (!a)
391                 return 0;
392
393         /* Add the entries of a to *k unless they already exist in *r
394          * in which case they are overridden instead. This assumes
395          * there is enough space in the r array. */
396
397         for (; *a; a++) {
398                 char **j;
399                 size_t n;
400
401                 n = strcspn(*a, "=");
402
403                 if ((*a)[n] == '=')
404                         n++;
405
406                 for (j = r; j < *k; j++)
407                         if (strncmp(*j, *a, n) == 0)
408                                 break;
409
410                 if (j >= *k)
411                         (*k)++;
412                 else
413                         free(*j);
414
415                 if (!(*j = strdup(*a)))
416                         return -ENOMEM;
417         }
418
419         return 0;
420 }
421
422 char **strv_env_merge(unsigned n_lists, ...) {
423         size_t n = 0;
424         char **l, **k, **r;
425         va_list ap;
426         unsigned i;
427
428         /* Merges an arbitrary number of environment sets */
429
430         va_start(ap, n_lists);
431         for (i = 0; i < n_lists; i++) {
432                 l = va_arg(ap, char**);
433                 n += strv_length(l);
434         }
435         va_end(ap);
436
437         if (!(r = new(char*, n+1)))
438                 return NULL;
439
440         k = r;
441
442         va_start(ap, n_lists);
443         for (i = 0; i < n_lists; i++) {
444                 l = va_arg(ap, char**);
445                 if (env_append(r, &k, l) < 0)
446                         goto fail;
447         }
448         va_end(ap);
449
450         *k = NULL;
451
452         return r;
453
454 fail:
455         va_end(ap);
456
457         for (k--; k >= r; k--)
458                 free(*k);
459
460         free(r);
461
462         return NULL;
463 }
464
465 static bool env_match(const char *t, const char *pattern) {
466         assert(t);
467         assert(pattern);
468
469         /* pattern a matches string a
470          *         a matches a=
471          *         a matches a=b
472          *         a= matches a=
473          *         a=b matches a=b
474          *         a= does not match a
475          *         a=b does not match a=
476          *         a=b does not match a
477          *         a=b does not match a=c */
478
479         if (streq(t, pattern))
480                 return true;
481
482         if (!strchr(pattern, '=')) {
483                 size_t l = strlen(pattern);
484
485                 return strncmp(t, pattern, l) == 0 && t[l] == '=';
486         }
487
488         return false;
489 }
490
491 char **strv_env_delete(char **x, unsigned n_lists, ...) {
492         size_t n, i = 0;
493         char **k, **r;
494         va_list ap;
495
496         /* Deletes every entry from x that is mentioned in the other
497          * string lists */
498
499         n = strv_length(x);
500
501         r = new(char*, n+1);
502         if (!r)
503                 return NULL;
504
505         STRV_FOREACH(k, x) {
506                 unsigned v;
507
508                 va_start(ap, n_lists);
509                 for (v = 0; v < n_lists; v++) {
510                         char **l, **j;
511
512                         l = va_arg(ap, char**);
513                         STRV_FOREACH(j, l)
514                                 if (env_match(*k, *j))
515                                         goto skip;
516                 }
517                 va_end(ap);
518
519                 r[i] = strdup(*k);
520                 if (!r[i]) {
521                         strv_free(r);
522                         return NULL;
523                 }
524
525                 i++;
526                 continue;
527
528         skip:
529                 va_end(ap);
530         }
531
532         r[i] = NULL;
533
534         assert(i <= n);
535
536         return r;
537 }
538
539 char **strv_env_unset(char **l, const char *p) {
540
541         char **f, **t;
542
543         if (!l)
544                 return NULL;
545
546         assert(p);
547
548         /* Drops every occurrence of the env var setting p in the
549          * string list. edits in-place. */
550
551         for (f = t = l; *f; f++) {
552
553                 if (env_match(*f, p)) {
554                         free(*f);
555                         continue;
556                 }
557
558                 *(t++) = *f;
559         }
560
561         *t = NULL;
562         return l;
563 }
564
565 char **strv_env_set(char **x, const char *p) {
566
567         char **k, **r;
568         char* m[2] = { (char*) p, NULL };
569
570         /* Overrides the env var setting of p, returns a new copy */
571
572         if (!(r = new(char*, strv_length(x)+2)))
573                 return NULL;
574
575         k = r;
576         if (env_append(r, &k, x) < 0)
577                 goto fail;
578
579         if (env_append(r, &k, m) < 0)
580                 goto fail;
581
582         *k = NULL;
583
584         return r;
585
586 fail:
587         for (k--; k >= r; k--)
588                 free(*k);
589
590         free(r);
591
592         return NULL;
593
594 }
595
596 char *strv_env_get_with_length(char **l, const char *name, size_t k) {
597         char **i;
598
599         assert(name);
600
601         STRV_FOREACH(i, l)
602                 if (strncmp(*i, name, k) == 0 &&
603                     (*i)[k] == '=')
604                         return *i + k + 1;
605
606         return NULL;
607 }
608
609 char *strv_env_get(char **l, const char *name) {
610         return strv_env_get_with_length(l, name, strlen(name));
611 }
612
613 char **strv_env_clean(char **l) {
614         char **r, **ret;
615
616         for (r = ret = l; *l; l++) {
617                 const char *equal;
618
619                 equal = strchr(*l, '=');
620
621                 if (equal && equal[1] == 0) {
622                         free(*l);
623                         continue;
624                 }
625
626                 *(r++) = *l;
627         }
628
629         *r = NULL;
630
631         return ret;
632 }
633
634 char **strv_parse_nulstr(const char *s, size_t l) {
635         const char *p;
636         unsigned c = 0, i = 0;
637         char **v;
638
639         assert(s || l <= 0);
640
641         if (l <= 0)
642                 return strv_new(NULL, NULL);
643
644         for (p = s; p < s + l; p++)
645                 if (*p == 0)
646                         c++;
647
648         if (s[l-1] != 0)
649                 c++;
650
651         if (!(v = new0(char*, c+1)))
652                 return NULL;
653
654         p = s;
655         while (p < s + l) {
656                 const char *e;
657
658                 e = memchr(p, 0, s + l - p);
659
660                 if (!(v[i++] = strndup(p, e ? e - p : s + l - p))) {
661                         strv_free(v);
662                         return NULL;
663                 }
664
665                 if (!e)
666                         break;
667
668                 p = e + 1;
669         }
670
671         assert(i == c);
672
673         return v;
674 }
675
676 bool strv_overlap(char **a, char **b) {
677         char **i, **j;
678
679         STRV_FOREACH(i, a) {
680                 STRV_FOREACH(j, b) {
681                         if (streq(*i, *j))
682                                 return true;
683                 }
684         }
685
686         return false;
687 }