2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
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.
20 #include "libudev-private.h"
23 * SECTION:libudev-list
24 * @short_description: list operation
26 * Libudev list operations.
32 * Opaque object representing one entry in a list. An entry contains
33 * contains a name, and optionally a value.
35 struct udev_list_entry {
36 struct udev_list_node node;
37 struct udev_list *list;
43 /* the list's head points to itself if empty */
44 void udev_list_node_init(struct udev_list_node *list)
50 int udev_list_node_is_empty(struct udev_list_node *list)
52 return list->next == list;
55 static void udev_list_node_insert_between(struct udev_list_node *new,
56 struct udev_list_node *prev,
57 struct udev_list_node *next)
65 void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
67 udev_list_node_insert_between(new, list->prev, list);
70 void udev_list_node_remove(struct udev_list_node *entry)
72 struct udev_list_node *prev = entry->prev;
73 struct udev_list_node *next = entry->next;
82 /* return list entry which embeds this node */
83 static inline struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
85 return container_of(node, struct udev_list_entry, node);
88 void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
90 memset(list, 0x00, sizeof(struct udev_list));
92 list->unique = unique;
93 udev_list_node_init(&list->node);
96 /* insert entry into a list as the last element */
97 void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
99 /* inserting before the list head make the node the last node in the list */
100 udev_list_node_insert_between(&new->node, list->node.prev, &list->node);
104 /* insert entry into a list, before a given existing entry */
105 void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
107 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
108 new->list = entry->list;
111 /* binary search in sorted array */
112 static int list_search(struct udev_list *list, const char *name)
114 unsigned int first, last;
117 last = list->entries_cur;
118 while (first < last) {
122 i = (first + last)/2;
123 cmp = strcmp(name, list->entries[i]->name);
132 /* not found, return negative insertion-index+1 */
136 struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
138 struct udev_list_entry *entry;
142 /* lookup existing name or insertion-index */
143 i = list_search(list, name);
145 entry = list->entries[i];
152 entry->value = strdup(value);
153 if (entry->value == NULL)
160 entry = calloc(1, sizeof(struct udev_list_entry));
163 entry->name = strdup(name);
164 if (entry->name == NULL) {
169 entry->value = strdup(value);
170 if (entry->value == NULL) {
178 /* allocate or enlarge sorted array if needed */
179 if (list->entries_cur >= list->entries_max) {
182 add = list->entries_max;
185 list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
186 if (list->entries == NULL) {
192 list->entries_max += add;
195 /* the negative i returned the insertion index */
198 /* insert into sorted list */
199 if ((unsigned int)i < list->entries_cur)
200 udev_list_entry_insert_before(entry, list->entries[i]);
202 udev_list_entry_append(entry, list);
204 /* insert into sorted array */
205 memmove(&list->entries[i+1], &list->entries[i],
206 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
207 list->entries[i] = entry;
210 udev_list_entry_append(entry, list);
216 void udev_list_entry_delete(struct udev_list_entry *entry)
218 if (entry->list->entries != NULL) {
220 struct udev_list *list = entry->list;
222 /* remove entry from sorted array */
223 i = list_search(list, entry->name);
225 memmove(&list->entries[i], &list->entries[i+1],
226 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
231 udev_list_node_remove(&entry->node);
237 void udev_list_cleanup(struct udev_list *list)
239 struct udev_list_entry *entry_loop;
240 struct udev_list_entry *entry_tmp;
243 list->entries = NULL;
244 list->entries_cur = 0;
245 list->entries_max = 0;
246 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
247 udev_list_entry_delete(entry_loop);
250 struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
252 if (udev_list_node_is_empty(&list->node))
254 return list_node_to_entry(list->node.next);
258 * udev_list_entry_get_next:
259 * @list_entry: current entry
261 * Get the next entry from the list.
263 * Returns: udev_list_entry, #NULL if no more entries are available.
265 _public_ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
267 struct udev_list_node *next;
269 if (list_entry == NULL)
271 next = list_entry->node.next;
272 /* empty list or no more entries */
273 if (next == &list_entry->list->node)
275 return list_node_to_entry(next);
279 * udev_list_entry_get_by_name:
280 * @list_entry: current entry
281 * @name: name string to match
283 * Lookup an entry in the list with a certain name.
285 * Returns: udev_list_entry, #NULL if no matching entry is found.
287 _public_ struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
291 if (list_entry == NULL)
294 if (!list_entry->list->unique)
297 i = list_search(list_entry->list, name);
300 return list_entry->list->entries[i];
304 * udev_list_entry_get_name:
305 * @list_entry: current entry
307 * Get the name of a list entry.
309 * Returns: the name string of this entry.
311 _public_ const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
313 if (list_entry == NULL)
315 return list_entry->name;
319 * udev_list_entry_get_value:
320 * @list_entry: current entry
322 * Get the value of list entry.
324 * Returns: the value string of this entry.
326 _public_ const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
328 if (list_entry == NULL)
330 return list_entry->value;
333 int udev_list_entry_get_num(struct udev_list_entry *list_entry)
335 if (list_entry == NULL)
337 return list_entry->num;
340 void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
342 if (list_entry == NULL)
344 list_entry->num = num;