2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "libudev-private.h"
32 struct list_node node;
33 struct list_node *list;
38 static struct udev_list *node_to_entry(struct list_node *node)
43 list -= offsetof(struct udev_list, node);
44 return (struct udev_list *)list;
47 void list_init(struct list_node *list)
53 static int list_empty(struct list_node *list)
55 return list->next == list;
59 static void list_add(struct list_node *new, struct list_node *list)
61 struct list_node *next = list->next;
70 static void list_add_to_end(struct list_node *new, struct list_node *list)
72 struct list_node *prev = list->prev;
80 static void list_del(struct list_node *entry)
82 struct list_node *prev = entry->prev;
83 struct list_node *next = entry->next;
89 #define list_for_each_entry(pos, list) \
90 for (pos = node_to_entry((list)->next); \
91 &pos->node != (list); \
92 pos = node_to_entry(pos->node.next))
94 #define list_for_each_entry_safe(pos, tmp, list) \
95 for (pos = node_to_entry((list)->next), \
96 tmp = node_to_entry(pos->node.next); \
97 &pos->node != (list); \
98 pos = tmp, tmp = node_to_entry(tmp->node.next))
100 struct udev_list *list_insert_entry(struct udev *udev, struct list_node *list,
101 const char *name, const char *value, int sort)
103 struct udev_list *list_loop;
104 struct udev_list *list_new;
106 /* avoid duplicate entries */
107 list_for_each_entry(list_loop, list) {
108 if (strcmp(list_loop->name, name) == 0) {
109 dbg(udev, "'%s' is already in the list\n", name);
115 list_for_each_entry(list_loop, list) {
116 if (strcmp(list_loop->name, name) > 0)
121 list_new = malloc(sizeof(struct udev_list));
122 if (list_new == NULL)
124 memset(list_new, 0x00, sizeof(struct udev_list));
125 list_new->udev = udev;
126 list_new->list = list;
127 list_new->name = strdup(name);
128 if (list_new->name == NULL) {
133 list_new->value = strdup(value);
134 if (list_new->value == NULL) {
139 dbg(udev, "adding '%s=%s'\n", list_new->name, list_new->value);
140 list_add_to_end(&list_new->node, &list_loop->node);
144 void list_move_entry_to_end(struct udev_list *list_entry, struct list_node *list)
146 list_del(&list_entry->node);
147 list_add_to_end(&list_entry->node, list);
150 void list_cleanup(struct udev *udev, struct list_node *list)
152 struct udev_list *list_loop;
153 struct udev_list *list_tmp;
155 list_for_each_entry_safe(list_loop, list_tmp, list) {
156 list_del(&list_loop->node);
157 free(list_loop->name);
158 free(list_loop->value);
163 struct udev_list *list_get_entry(struct list_node *list)
165 if (list_empty(list))
167 return node_to_entry(list->next);
170 struct udev_list *udev_list_entry_get_next(struct udev_list *list_entry)
172 struct list_node *next;
174 next = list_entry->node.next;
175 /* empty list or no more emtries */
176 if (next == list_entry->list)
178 return node_to_entry(next);
181 const char *udev_list_entry_get_name(struct udev_list *list_entry)
183 if (list_entry == NULL)
185 return list_entry->name;
188 const char *udev_list_entry_get_value(struct udev_list *list_entry)
190 if (list_entry == NULL)
192 return list_entry->value;