]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/libudev-list.c
udev: switch to systemd logging functions
[thirdparty/systemd.git] / src / udev / libudev-list.c
CommitLineData
e345e267
KS
1/*
2 * libudev - interface to udev device information
3 *
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5 *
4061ab9f
KS
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
e345e267
KS
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <stddef.h>
15#include <unistd.h>
16#include <errno.h>
17#include <string.h>
18
19#include "libudev.h"
20#include "libudev-private.h"
21
ce1d6d7f
KS
22/**
23 * SECTION:libudev-list
24 * @short_description: list operation
25 *
26 * Libudev list operations.
27 */
28
1e511322
KS
29/**
30 * udev_list_entry:
31 *
ce1d6d7f
KS
32 * Opaque object representing one entry in a list. An entry contains
33 * contains a name, and optionally a value.
1e511322 34 */
0de33a61 35struct udev_list_entry {
912541b0
KS
36 struct udev_list_node node;
37 struct udev_list *list;
38 char *name;
39 char *value;
40 int num;
e345e267
KS
41};
42
8958da13 43/* the list's head points to itself if empty */
869c9031 44void udev_list_node_init(struct udev_list_node *list)
e345e267 45{
912541b0
KS
46 list->next = list;
47 list->prev = list;
e345e267
KS
48}
49
869c9031 50int udev_list_node_is_empty(struct udev_list_node *list)
e345e267 51{
912541b0 52 return list->next == list;
e345e267
KS
53}
54
3feeb77c 55static void udev_list_node_insert_between(struct udev_list_node *new,
912541b0
KS
56 struct udev_list_node *prev,
57 struct udev_list_node *next)
e345e267 58{
912541b0
KS
59 next->prev = new;
60 new->next = next;
61 new->prev = prev;
62 prev->next = new;
e345e267
KS
63}
64
9dcf7ec8
KS
65void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
66{
912541b0 67 udev_list_node_insert_between(new, list->prev, list);
9dcf7ec8
KS
68}
69
70void udev_list_node_remove(struct udev_list_node *entry)
e345e267 71{
912541b0
KS
72 struct udev_list_node *prev = entry->prev;
73 struct udev_list_node *next = entry->next;
e345e267 74
912541b0
KS
75 next->prev = prev;
76 prev->next = next;
0de33a61 77
912541b0
KS
78 entry->prev = NULL;
79 entry->next = NULL;
e345e267
KS
80}
81
0de33a61 82/* return list entry which embeds this node */
8cd2e972 83static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
0de33a61 84{
912541b0 85 char *list;
e345e267 86
912541b0
KS
87 list = (char *)node;
88 list -= offsetof(struct udev_list_entry, node);
89 return (struct udev_list_entry *)list;
0de33a61
KS
90}
91
869c9031 92void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
0de33a61 93{
912541b0
KS
94 memset(list, 0x00, sizeof(struct udev_list));
95 list->udev = udev;
96 list->unique = unique;
97 udev_list_node_init(&list->node);
0de33a61
KS
98}
99
869c9031
KS
100/* insert entry into a list as the last element */
101void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
1e78dcbe 102{
912541b0
KS
103 /* inserting before the list head make the node the last node in the list */
104 udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
105 new->list = list;
1e78dcbe
KS
106}
107
0de33a61 108/* insert entry into a list, before a given existing entry */
1e78dcbe 109void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
0de33a61 110{
912541b0
KS
111 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
112 new->list = entry->list;
0de33a61 113}
e345e267 114
869c9031
KS
115/* binary search in sorted array */
116static int list_search(struct udev_list *list, const char *name)
0de33a61 117{
912541b0
KS
118 unsigned int first, last;
119
120 first = 0;
121 last = list->entries_cur;
122 while (first < last) {
123 unsigned int i;
124 int cmp;
125
126 i = (first + last)/2;
127 cmp = strcmp(name, list->entries[i]->name);
128 if (cmp < 0)
129 last = i;
130 else if (cmp > 0)
131 first = i+1;
132 else
133 return i;
134 }
135
136 /* not found, return negative insertion-index+1 */
137 return -(first+1);
869c9031
KS
138}
139
140struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
141{
912541b0
KS
142 struct udev_list_entry *entry;
143 int i = 0;
144
145 if (list->unique) {
146 /* lookup existing name or insertion-index */
147 i = list_search(list, name);
148 if (i >= 0) {
149 entry = list->entries[i];
150
912541b0
KS
151 free(entry->value);
152 if (value == NULL) {
153 entry->value = NULL;
912541b0
KS
154 return entry;
155 }
156 entry->value = strdup(value);
157 if (entry->value == NULL)
158 return NULL;
912541b0
KS
159 return entry;
160 }
161 }
162
163 /* add new name */
164 entry = calloc(1, sizeof(struct udev_list_entry));
165 if (entry == NULL)
166 return NULL;
167 entry->name = strdup(name);
168 if (entry->name == NULL) {
169 free(entry);
170 return NULL;
171 }
172 if (value != NULL) {
173 entry->value = strdup(value);
174 if (entry->value == NULL) {
175 free(entry->name);
176 free(entry);
177 return NULL;
178 }
179 }
180
181 if (list->unique) {
182 /* allocate or enlarge sorted array if needed */
183 if (list->entries_cur >= list->entries_max) {
184 unsigned int add;
185
186 add = list->entries_max;
187 if (add < 1)
188 add = 64;
189 list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
190 if (list->entries == NULL) {
191 free(entry->name);
192 free(entry->value);
193 return NULL;
194 }
195 list->entries_max += add;
196 }
197
198 /* the negative i returned the insertion index */
199 i = (-i)-1;
200
201 /* insert into sorted list */
202 if ((unsigned int)i < list->entries_cur)
203 udev_list_entry_insert_before(entry, list->entries[i]);
204 else
205 udev_list_entry_append(entry, list);
206
207 /* insert into sorted array */
208 memmove(&list->entries[i+1], &list->entries[i],
209 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
210 list->entries[i] = entry;
211 list->entries_cur++;
212 } else {
213 udev_list_entry_append(entry, list);
214 }
215
912541b0 216 return entry;
e345e267
KS
217}
218
1e78dcbe 219void udev_list_entry_delete(struct udev_list_entry *entry)
e345e267 220{
912541b0
KS
221 if (entry->list->entries != NULL) {
222 int i;
223 struct udev_list *list = entry->list;
224
225 /* remove entry from sorted array */
226 i = list_search(list, entry->name);
227 if (i >= 0) {
228 memmove(&list->entries[i], &list->entries[i+1],
229 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
230 list->entries_cur--;
231 }
232 }
233
234 udev_list_node_remove(&entry->node);
235 free(entry->name);
236 free(entry->value);
237 free(entry);
e345e267
KS
238}
239
869c9031 240void udev_list_cleanup(struct udev_list *list)
e345e267 241{
912541b0
KS
242 struct udev_list_entry *entry_loop;
243 struct udev_list_entry *entry_tmp;
244
245 free(list->entries);
246 list->entries = NULL;
247 list->entries_cur = 0;
248 list->entries_max = 0;
249 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
250 udev_list_entry_delete(entry_loop);
be7f7f57
KS
251}
252
869c9031 253struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
e345e267 254{
912541b0
KS
255 if (udev_list_node_is_empty(&list->node))
256 return NULL;
257 return list_node_to_entry(list->node.next);
e345e267
KS
258}
259
1e511322
KS
260/**
261 * udev_list_entry_get_next:
262 * @list_entry: current entry
263 *
264 * Returns: the next entry from the list, #NULL is no more entries are found.
265 */
54cf0b7f 266_public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
e345e267 267{
912541b0
KS
268 struct udev_list_node *next;
269
270 if (list_entry == NULL)
271 return NULL;
272 next = list_entry->node.next;
273 /* empty list or no more entries */
274 if (next == &list_entry->list->node)
275 return NULL;
276 return list_node_to_entry(next);
e345e267
KS
277}
278
1e511322
KS
279/**
280 * udev_list_entry_get_by_name:
281 * @list_entry: current entry
282 * @name: name string to match
283 *
284 * Returns: the entry where @name matched, #NULL if no matching entry is found.
285 */
54cf0b7f 286_public_ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
bc8184ed 287{
912541b0 288 int i;
bc8184ed 289
912541b0
KS
290 if (list_entry == NULL)
291 return NULL;
869c9031 292
912541b0
KS
293 if (!list_entry->list->unique)
294 return NULL;
869c9031 295
912541b0
KS
296 i = list_search(list_entry->list, name);
297 if (i < 0)
298 return NULL;
299 return list_entry->list->entries[i];
bc8184ed
KS
300}
301
1e511322
KS
302/**
303 * udev_list_entry_get_name:
304 * @list_entry: current entry
305 *
306 * Returns: the name string of this entry.
307 */
54cf0b7f 308_public_ const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
e345e267 309{
912541b0
KS
310 if (list_entry == NULL)
311 return NULL;
312 return list_entry->name;
e345e267
KS
313}
314
1e511322
KS
315/**
316 * udev_list_entry_get_value:
317 * @list_entry: current entry
318 *
319 * Returns: the value string of this entry.
320 */
54cf0b7f 321_public_ const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
e345e267 322{
912541b0
KS
323 if (list_entry == NULL)
324 return NULL;
325 return list_entry->value;
e345e267 326}
df1dcc09 327
8958da13 328int udev_list_entry_get_num(struct udev_list_entry *list_entry)
df1dcc09 329{
912541b0
KS
330 if (list_entry == NULL)
331 return -EINVAL;
332 return list_entry->num;
df1dcc09
KS
333}
334
8958da13 335void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
df1dcc09 336{
912541b0
KS
337 if (list_entry == NULL)
338 return;
339 list_entry->num = num;
df1dcc09 340}