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;
38 struct udev_list_node *list;
44 /* the list's head points to itself if empty */
45 void udev_list_init(struct udev_list_node *list)
51 int udev_list_is_empty(struct udev_list_node *list)
53 return list->next == list;
56 static void udev_list_node_insert_between(struct udev_list_node *new,
57 struct udev_list_node *prev,
58 struct udev_list_node *next)
66 void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
68 udev_list_node_insert_between(new, list->prev, list);
71 void udev_list_node_remove(struct udev_list_node *entry)
73 struct udev_list_node *prev = entry->prev;
74 struct udev_list_node *next = entry->next;
83 /* return list entry which embeds this node */
84 static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
89 list -= offsetof(struct udev_list_entry, node);
90 return (struct udev_list_entry *)list;
93 /* insert entry into a list as the last element */
94 void udev_list_entry_append(struct udev_list_entry *new, struct udev_list_node *list)
96 /* inserting before the list head make the node the last node in the list */
97 udev_list_node_insert_between(&new->node, list->prev, list);
101 /* remove entry from a list */
102 void udev_list_entry_remove(struct udev_list_entry *entry)
104 udev_list_node_remove(&entry->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 struct udev_list_entry *udev_list_entry_add(struct udev *udev, struct udev_list_node *list,
116 const char *name, const char *value,
119 struct udev_list_entry *entry_loop = NULL;
120 struct udev_list_entry *entry_new;
122 if (flags & UDEV_LIST_UNIQUE) {
123 udev_list_entry_foreach(entry_loop, udev_list_get_entry(list)) {
124 if (strcmp(entry_loop->name, name) == 0) {
125 dbg(udev, "'%s' is already in the list\n", name);
126 free(entry_loop->value);
128 entry_loop->value = NULL;
129 dbg(udev, "'%s' value unset\n", name);
132 entry_loop->value = strdup(value);
133 if (entry_loop->value == NULL)
135 dbg(udev, "'%s' value replaced with '%s'\n", name, value);
141 if (flags & UDEV_LIST_SORT) {
142 udev_list_entry_foreach(entry_loop, udev_list_get_entry(list)) {
143 if (strcmp(entry_loop->name, name) > 0)
148 entry_new = malloc(sizeof(struct udev_list_entry));
149 if (entry_new == NULL)
151 memset(entry_new, 0x00, sizeof(struct udev_list_entry));
152 entry_new->udev = udev;
153 entry_new->name = strdup(name);
154 if (entry_new->name == NULL) {
160 entry_new->value = strdup(value);
161 if (entry_new->value == NULL) {
162 free(entry_new->name);
168 if (entry_loop != NULL)
169 udev_list_entry_insert_before(entry_new, entry_loop);
171 udev_list_entry_append(entry_new, list);
173 dbg(udev, "'%s=%s' added\n", entry_new->name, entry_new->value);
177 void udev_list_entry_delete(struct udev_list_entry *entry)
179 udev_list_node_remove(&entry->node);
185 void udev_list_cleanup_entries(struct udev *udev, struct udev_list_node *list)
187 struct udev_list_entry *entry_loop;
188 struct udev_list_entry *entry_tmp;
190 udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
191 udev_list_entry_delete(entry_loop);
194 struct udev_list_entry *udev_list_get_entry(struct udev_list_node *list)
196 if (udev_list_is_empty(list))
198 return list_node_to_entry(list->next);
202 * udev_list_entry_get_next:
203 * @list_entry: current entry
205 * Returns: the next entry from the list, #NULL is no more entries are found.
207 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
209 struct udev_list_node *next;
211 if (list_entry == NULL)
213 next = list_entry->node.next;
214 /* empty list or no more entries */
215 if (next == list_entry->list)
217 return list_node_to_entry(next);
221 * udev_list_entry_get_by_name:
222 * @list_entry: current entry
223 * @name: name string to match
225 * Returns: the entry where @name matched, #NULL if no matching entry is found.
227 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
229 struct udev_list_entry *entry;
231 udev_list_entry_foreach(entry, list_entry) {
232 if (strcmp(udev_list_entry_get_name(entry), name) == 0) {
233 dbg(entry->udev, "found '%s=%s'\n", entry->name, entry->value);
241 * udev_list_entry_get_name:
242 * @list_entry: current entry
244 * Returns: the name string of this entry.
246 UDEV_EXPORT const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
248 if (list_entry == NULL)
250 return list_entry->name;
254 * udev_list_entry_get_value:
255 * @list_entry: current entry
257 * Returns: the value string of this entry.
259 UDEV_EXPORT const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
261 if (list_entry == NULL)
263 return list_entry->value;
266 int udev_list_entry_get_num(struct udev_list_entry *list_entry)
268 if (list_entry == NULL)
270 return list_entry->num;
273 void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
275 if (list_entry == NULL)
277 list_entry->num = num;