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 struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
88 list -= offsetof(struct udev_list_entry, node);
89 return (struct udev_list_entry *)list;
92 void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
94 memset(list, 0x00, sizeof(struct udev_list));
96 list->unique = unique;
97 udev_list_node_init(&list->node);
100 /* insert entry into a list as the last element */
101 void udev_list_entry_append(struct udev_list_entry *new, struct udev_list *list)
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);
108 /* insert entry into a list, before a given existing entry */
109 void udev_list_entry_insert_before(struct udev_list_entry *new, struct udev_list_entry *entry)
111 udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
112 new->list = entry->list;
115 /* binary search in sorted array */
116 static int list_search(struct udev_list *list, const char *name)
118 unsigned int first, last;
121 last = list->entries_cur;
122 while (first < last) {
126 i = (first + last)/2;
127 cmp = strcmp(name, list->entries[i]->name);
136 /* not found, return negative insertion-index+1 */
140 struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
142 struct udev_list_entry *entry;
146 /* lookup existing name or insertion-index */
147 i = list_search(list, name);
149 entry = list->entries[i];
151 dbg(list->udev, "'%s' is already in the list\n", name);
155 dbg(list->udev, "'%s' value unset\n", name);
158 entry->value = strdup(value);
159 if (entry->value == NULL)
161 dbg(list->udev, "'%s' value replaced with '%s'\n", name, value);
167 entry = calloc(1, sizeof(struct udev_list_entry));
170 entry->name = strdup(name);
171 if (entry->name == NULL) {
176 entry->value = strdup(value);
177 if (entry->value == NULL) {
183 udev_list_entry_append(entry, list);
186 /* allocate or enlarge sorted array if needed */
187 if (list->entries_cur >= list->entries_max) {
190 add = list->entries_max;
193 list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
194 if (list->entries == NULL) {
199 list->entries_max += add;
202 /* insert into sorted array */
204 memmove(&list->entries[i+1], &list->entries[i],
205 (list->entries_cur - i) * sizeof(struct udev_list_entry *));
206 list->entries[i] = entry;
210 dbg(list->udev, "'%s=%s' added\n", entry->name, entry->value);
214 void udev_list_entry_delete(struct udev_list_entry *entry)
216 if (entry->list->entries != NULL) {
218 struct udev_list *list = entry->list;
220 /* remove entry from sorted array */
221 i = list_search(list, entry->name);
223 memmove(&list->entries[i], &list->entries[i+1],
224 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
229 udev_list_node_remove(&entry->node);
235 void udev_list_cleanup(struct udev_list *list)
237 struct udev_list_entry *entry_loop;
238 struct udev_list_entry *entry_tmp;
241 list->entries = NULL;
242 list->entries_cur = 0;
243 list->entries_max = 0;
244 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
245 udev_list_entry_delete(entry_loop);
248 struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
250 if (udev_list_node_is_empty(&list->node))
252 return list_node_to_entry(list->node.next);
256 * udev_list_entry_get_next:
257 * @list_entry: current entry
259 * Returns: the next entry from the list, #NULL is no more entries are found.
261 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
263 struct udev_list_node *next;
265 if (list_entry == NULL)
267 next = list_entry->node.next;
268 /* empty list or no more entries */
269 if (next == &list_entry->list->node)
271 return list_node_to_entry(next);
275 * udev_list_entry_get_by_name:
276 * @list_entry: current entry
277 * @name: name string to match
279 * Returns: the entry where @name matched, #NULL if no matching entry is found.
281 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
285 if (list_entry == NULL)
288 if (!list_entry->list->unique)
291 i = list_search(list_entry->list, name);
294 return list_entry->list->entries[i];
298 * udev_list_entry_get_name:
299 * @list_entry: current entry
301 * Returns: the name string of this entry.
303 UDEV_EXPORT const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
305 if (list_entry == NULL)
307 return list_entry->name;
311 * udev_list_entry_get_value:
312 * @list_entry: current entry
314 * Returns: the value string of this entry.
316 UDEV_EXPORT const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
318 if (list_entry == NULL)
320 return list_entry->value;
323 int udev_list_entry_get_num(struct udev_list_entry *list_entry)
325 if (list_entry == NULL)
327 return list_entry->num;
330 void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
332 if (list_entry == NULL)
334 list_entry->num = num;