]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-list.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / libudev / libudev-list.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e345e267 2
e345e267 3#include <errno.h>
b5efdb8a
LP
4#include <stddef.h>
5#include <stdlib.h>
e345e267
KS
6#include <string.h>
7
b5efdb8a 8#include "alloc-util.h"
5ea78a39
YW
9#include "libudev-list-internal.h"
10#include "util.h"
e345e267 11
ce1d6d7f
KS
12/**
13 * SECTION:libudev-list
14 * @short_description: list operation
15 *
16 * Libudev list operations.
17 */
18
1e511322
KS
19/**
20 * udev_list_entry:
21 *
ce1d6d7f
KS
22 * Opaque object representing one entry in a list. An entry contains
23 * contains a name, and optionally a value.
1e511322 24 */
0de33a61 25struct udev_list_entry {
912541b0
KS
26 struct udev_list_node node;
27 struct udev_list *list;
28 char *name;
29 char *value;
30 int num;
e345e267
KS
31};
32
8958da13 33/* the list's head points to itself if empty */
5ea78a39 34static void udev_list_node_init(struct udev_list_node *list) {
912541b0
KS
35 list->next = list;
36 list->prev = list;
e345e267
KS
37}
38
5ea78a39 39static int udev_list_node_is_empty(struct udev_list_node *list) {
912541b0 40 return list->next == list;
e345e267
KS
41}
42
3feeb77c 43static void udev_list_node_insert_between(struct udev_list_node *new,
912541b0 44 struct udev_list_node *prev,
5ea78a39 45 struct udev_list_node *next) {
912541b0
KS
46 next->prev = new;
47 new->next = next;
48 new->prev = prev;
49 prev->next = new;
e345e267
KS
50}
51
5ea78a39 52static void udev_list_node_remove(struct udev_list_node *entry) {
912541b0
KS
53 struct udev_list_node *prev = entry->prev;
54 struct udev_list_node *next = entry->next;
e345e267 55
912541b0
KS
56 next->prev = prev;
57 prev->next = next;
0de33a61 58
912541b0
KS
59 entry->prev = NULL;
60 entry->next = NULL;
e345e267
KS
61}
62
0de33a61 63/* return list entry which embeds this node */
a1e92eee 64static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node) {
b27ee00b 65 return container_of(node, struct udev_list_entry, node);
0de33a61
KS
66}
67
f349626b 68void udev_list_init(struct udev_list *list, bool unique) {
29804cc1 69 memzero(list, sizeof(struct udev_list));
912541b0
KS
70 list->unique = unique;
71 udev_list_node_init(&list->node);
0de33a61
KS
72}
73
869c9031 74/* insert entry into a list as the last element */
5ea78a39 75static void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list) {
912541b0
KS
76 /* inserting before the list head make the node the last node in the list */
77 udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
78 new->list = list;
1e78dcbe
KS
79}
80
0de33a61 81/* insert entry into a list, before a given existing entry */
5ea78a39 82static void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry) {
912541b0
KS
83 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
84 new->list = entry->list;
0de33a61 85}
e345e267 86
869c9031 87/* binary search in sorted array */
5ea78a39 88static int list_search(struct udev_list *list, const char *name) {
14cb109d 89 unsigned first, last;
912541b0
KS
90
91 first = 0;
92 last = list->entries_cur;
93 while (first < last) {
14cb109d 94 unsigned i;
912541b0
KS
95 int cmp;
96
97 i = (first + last)/2;
98 cmp = strcmp(name, list->entries[i]->name);
99 if (cmp < 0)
100 last = i;
101 else if (cmp > 0)
102 first = i+1;
103 else
104 return i;
105 }
106
107 /* not found, return negative insertion-index+1 */
108 return -(first+1);
869c9031
KS
109}
110
62d74c78 111struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value) {
912541b0
KS
112 struct udev_list_entry *entry;
113 int i = 0;
114
115 if (list->unique) {
116 /* lookup existing name or insertion-index */
117 i = list_search(list, name);
118 if (i >= 0) {
119 entry = list->entries[i];
120
912541b0 121 free(entry->value);
62d74c78 122 if (!value) {
912541b0 123 entry->value = NULL;
912541b0
KS
124 return entry;
125 }
126 entry->value = strdup(value);
62d74c78 127 if (!entry->value)
912541b0 128 return NULL;
912541b0
KS
129 return entry;
130 }
131 }
132
133 /* add new name */
955d98c9 134 entry = new0(struct udev_list_entry, 1);
62d74c78 135 if (!entry)
912541b0 136 return NULL;
6b430fdb 137
912541b0 138 entry->name = strdup(name);
62d74c78 139 if (!entry->name)
6b430fdb
ZJS
140 return mfree(entry);
141
62d74c78 142 if (value) {
912541b0 143 entry->value = strdup(value);
62d74c78 144 if (!entry->value) {
912541b0 145 free(entry->name);
6b430fdb 146 return mfree(entry);
912541b0
KS
147 }
148 }
149
150 if (list->unique) {
151 /* allocate or enlarge sorted array if needed */
152 if (list->entries_cur >= list->entries_max) {
cf2292f5 153 struct udev_list_entry **entries;
14cb109d 154 unsigned add;
912541b0
KS
155
156 add = list->entries_max;
157 if (add < 1)
158 add = 64;
62d74c78
LP
159 entries = reallocarray(list->entries, list->entries_max + add, sizeof(struct udev_list_entry *));
160 if (!entries) {
912541b0
KS
161 free(entry->name);
162 free(entry->value);
6b430fdb 163 return mfree(entry);
912541b0 164 }
cf2292f5 165 list->entries = entries;
912541b0
KS
166 list->entries_max += add;
167 }
168
169 /* the negative i returned the insertion index */
170 i = (-i)-1;
171
172 /* insert into sorted list */
14cb109d 173 if ((unsigned)i < list->entries_cur)
912541b0
KS
174 udev_list_entry_insert_before(entry, list->entries[i]);
175 else
176 udev_list_entry_append(entry, list);
177
178 /* insert into sorted array */
179 memmove(&list->entries[i+1], &list->entries[i],
180 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
181 list->entries[i] = entry;
182 list->entries_cur++;
62d74c78 183 } else
912541b0 184 udev_list_entry_append(entry, list);
912541b0 185
912541b0 186 return entry;
e345e267
KS
187}
188
5ea78a39 189static void udev_list_entry_delete(struct udev_list_entry *entry) {
f921b457 190 if (entry->list->entries) {
912541b0
KS
191 int i;
192 struct udev_list *list = entry->list;
193
194 /* remove entry from sorted array */
195 i = list_search(list, entry->name);
196 if (i >= 0) {
197 memmove(&list->entries[i], &list->entries[i+1],
198 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
199 list->entries_cur--;
200 }
201 }
202
203 udev_list_node_remove(&entry->node);
204 free(entry->name);
205 free(entry->value);
206 free(entry);
e345e267
KS
207}
208
5ea78a39
YW
209#define udev_list_entry_foreach_safe(entry, tmp, first) \
210 for (entry = first, tmp = udev_list_entry_get_next(entry); \
211 entry; \
212 entry = tmp, tmp = udev_list_entry_get_next(tmp))
213
214void udev_list_cleanup(struct udev_list *list) {
912541b0
KS
215 struct udev_list_entry *entry_loop;
216 struct udev_list_entry *entry_tmp;
217
a1e58e8e 218 list->entries = mfree(list->entries);
912541b0
KS
219 list->entries_cur = 0;
220 list->entries_max = 0;
221 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
222 udev_list_entry_delete(entry_loop);
be7f7f57
KS
223}
224
5ea78a39 225struct udev_list_entry *udev_list_get_entry(struct udev_list *list) {
912541b0
KS
226 if (udev_list_node_is_empty(&list->node))
227 return NULL;
228 return list_node_to_entry(list->node.next);
e345e267
KS
229}
230
1e511322
KS
231/**
232 * udev_list_entry_get_next:
233 * @list_entry: current entry
234 *
21dbe43a
KS
235 * Get the next entry from the list.
236 *
237 * Returns: udev_list_entry, #NULL if no more entries are available.
1e511322 238 */
5ea78a39 239_public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry) {
912541b0
KS
240 struct udev_list_node *next;
241
f921b457 242 if (!list_entry)
912541b0
KS
243 return NULL;
244 next = list_entry->node.next;
245 /* empty list or no more entries */
246 if (next == &list_entry->list->node)
247 return NULL;
248 return list_node_to_entry(next);
e345e267
KS
249}
250
1e511322
KS
251/**
252 * udev_list_entry_get_by_name:
253 * @list_entry: current entry
254 * @name: name string to match
255 *
21dbe43a
KS
256 * Lookup an entry in the list with a certain name.
257 *
258 * Returns: udev_list_entry, #NULL if no matching entry is found.
1e511322 259 */
5ea78a39 260_public_ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name) {
912541b0 261 int i;
bc8184ed 262
f921b457 263 if (!list_entry)
912541b0 264 return NULL;
869c9031 265
912541b0
KS
266 if (!list_entry->list->unique)
267 return NULL;
869c9031 268
912541b0
KS
269 i = list_search(list_entry->list, name);
270 if (i < 0)
271 return NULL;
272 return list_entry->list->entries[i];
bc8184ed
KS
273}
274
1e511322
KS
275/**
276 * udev_list_entry_get_name:
277 * @list_entry: current entry
278 *
21dbe43a
KS
279 * Get the name of a list entry.
280 *
1e511322
KS
281 * Returns: the name string of this entry.
282 */
5ea78a39 283_public_ const char *udev_list_entry_get_name(struct udev_list_entry *list_entry) {
f921b457 284 if (!list_entry)
912541b0
KS
285 return NULL;
286 return list_entry->name;
e345e267
KS
287}
288
1e511322
KS
289/**
290 * udev_list_entry_get_value:
291 * @list_entry: current entry
292 *
21dbe43a
KS
293 * Get the value of list entry.
294 *
1e511322
KS
295 * Returns: the value string of this entry.
296 */
5ea78a39 297_public_ const char *udev_list_entry_get_value(struct udev_list_entry *list_entry) {
f921b457 298 if (!list_entry)
912541b0
KS
299 return NULL;
300 return list_entry->value;
e345e267 301}