]>
Commit | Line | Data |
---|---|---|
00616955 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2016 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include "ordered-set.h" | |
21 | #include "strv.h" | |
22 | ||
23 | int ordered_set_consume(OrderedSet *s, void *p) { | |
24 | int r; | |
25 | ||
26 | r = ordered_set_put(s, p); | |
27 | if (r <= 0) | |
28 | free(p); | |
29 | ||
30 | return r; | |
31 | } | |
32 | ||
33 | int ordered_set_put_strdup(OrderedSet *s, const char *p) { | |
34 | char *c; | |
35 | int r; | |
36 | ||
37 | assert(s); | |
38 | assert(p); | |
39 | ||
40 | c = strdup(p); | |
41 | if (!c) | |
42 | return -ENOMEM; | |
43 | ||
44 | r = ordered_set_consume(s, c); | |
45 | if (r == -EEXIST) | |
46 | return 0; | |
47 | ||
48 | return r; | |
49 | } | |
50 | ||
51 | int ordered_set_put_strdupv(OrderedSet *s, char **l) { | |
52 | int n = 0, r; | |
53 | char **i; | |
54 | ||
55 | STRV_FOREACH(i, l) { | |
56 | r = ordered_set_put_strdup(s, *i); | |
57 | if (r < 0) | |
58 | return r; | |
59 | ||
60 | n += r; | |
61 | } | |
62 | ||
63 | return n; | |
64 | } |