2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
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.
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.
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/>.
28 #include "libudev-private.h"
31 * SECTION:libudev-list
32 * @short_description: list operation
34 * Libudev list operations.
40 * Opaque object representing one entry in a list. An entry contains
41 * contains a name, and optionally a value.
43 struct udev_list_entry {
44 struct udev_list_node node;
45 struct udev_list *list;
51 /* the list's head points to itself if empty */
52 void udev_list_node_init(struct udev_list_node *list)
58 int udev_list_node_is_empty(struct udev_list_node *list)
60 return list->next == list;
63 static void udev_list_node_insert_between(struct udev_list_node *new,
64 struct udev_list_node *prev,
65 struct udev_list_node *next)
73 void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
75 udev_list_node_insert_between(new, list->prev, list);
78 void udev_list_node_remove(struct udev_list_node *entry)
80 struct udev_list_node *prev = entry->prev;
81 struct udev_list_node *next = entry->next;
90 /* return list entry which embeds this node */
91 static inline struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
93 return container_of(node, struct udev_list_entry, node);
96 void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
98 memzero(list, sizeof(struct udev_list));
100 list->unique = unique;
101 udev_list_node_init(&list->node);
104 /* insert entry into a list as the last element */
105 static void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
107 /* inserting before the list head make the node the last node in the list */
108 udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
112 /* insert entry into a list, before a given existing entry */
113 static void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
115 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
116 new->list = entry->list;
119 /* binary search in sorted array */
120 static int list_search(struct udev_list *list, const char *name)
122 unsigned int first, last;
125 last = list->entries_cur;
126 while (first < last) {
130 i = (first + last)/2;
131 cmp = strcmp(name, list->entries[i]->name);
140 /* not found, return negative insertion-index+1 */
144 struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
146 struct udev_list_entry *entry;
150 /* lookup existing name or insertion-index */
151 i = list_search(list, name);
153 entry = list->entries[i];
160 entry->value = strdup(value);
161 if (entry->value == NULL)
168 entry = new0(struct udev_list_entry, 1);
171 entry->name = strdup(name);
172 if (entry->name == NULL) {
177 entry->value = strdup(value);
178 if (entry->value == NULL) {
186 /* allocate or enlarge sorted array if needed */
187 if (list->entries_cur >= list->entries_max) {
188 struct udev_list_entry **entries;
191 add = list->entries_max;
194 entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
195 if (entries == NULL) {
201 list->entries = entries;
202 list->entries_max += add;
205 /* the negative i returned the insertion index */
208 /* insert into sorted list */
209 if ((unsigned int)i < list->entries_cur)
210 udev_list_entry_insert_before(entry, list->entries[i]);
212 udev_list_entry_append(entry, list);
214 /* insert into sorted array */
215 memmove(&list->entries[i+1], &list->entries[i],
216 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
217 list->entries[i] = entry;
220 udev_list_entry_append(entry, list);
226 void udev_list_entry_delete(struct udev_list_entry *entry)
228 if (entry->list->entries != NULL) {
230 struct udev_list *list = entry->list;
232 /* remove entry from sorted array */
233 i = list_search(list, entry->name);
235 memmove(&list->entries[i], &list->entries[i+1],
236 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
241 udev_list_node_remove(&entry->node);
247 void udev_list_cleanup(struct udev_list *list)
249 struct udev_list_entry *entry_loop;
250 struct udev_list_entry *entry_tmp;
253 list->entries = NULL;
254 list->entries_cur = 0;
255 list->entries_max = 0;
256 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
257 udev_list_entry_delete(entry_loop);
260 struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
262 if (udev_list_node_is_empty(&list->node))
264 return list_node_to_entry(list->node.next);
268 * udev_list_entry_get_next:
269 * @list_entry: current entry
271 * Get the next entry from the list.
273 * Returns: udev_list_entry, #NULL if no more entries are available.
275 _public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
277 struct udev_list_node *next;
279 if (list_entry == NULL)
281 next = list_entry->node.next;
282 /* empty list or no more entries */
283 if (next == &list_entry->list->node)
285 return list_node_to_entry(next);
289 * udev_list_entry_get_by_name:
290 * @list_entry: current entry
291 * @name: name string to match
293 * Lookup an entry in the list with a certain name.
295 * Returns: udev_list_entry, #NULL if no matching entry is found.
297 _public_ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
301 if (list_entry == NULL)
304 if (!list_entry->list->unique)
307 i = list_search(list_entry->list, name);
310 return list_entry->list->entries[i];
314 * udev_list_entry_get_name:
315 * @list_entry: current entry
317 * Get the name of a list entry.
319 * Returns: the name string of this entry.
321 _public_ const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
323 if (list_entry == NULL)
325 return list_entry->name;
329 * udev_list_entry_get_value:
330 * @list_entry: current entry
332 * Get the value of list entry.
334 * Returns: the value string of this entry.
336 _public_ const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
338 if (list_entry == NULL)
340 return list_entry->value;
343 int udev_list_entry_get_num(struct udev_list_entry *list_entry)
345 if (list_entry == NULL)
347 return list_entry->num;
350 void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
352 if (list_entry == NULL)
354 list_entry->num = num;