chiark / gitweb /
libudev: rework list handling
[elogind.git] / udev / lib / libudev-list.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26
27 #include "libudev.h"
28 #include "libudev-private.h"
29
30 struct udev_list {
31         struct udev *udev;
32         struct list_node node;
33         struct list_node *list;
34         char *name;
35         char *value;
36 };
37
38 static struct udev_list *node_to_entry(struct list_node *node)
39 {
40         char *list;
41
42         list = (char *)node;
43         list -= offsetof(struct udev_list, node);
44         return (struct udev_list *)list;
45 }
46
47 void list_init(struct list_node *list)
48 {
49         list->next = list;
50         list->prev = list;
51 }
52
53 static int list_empty(struct list_node *list)
54 {
55         return list->next == list;
56 }
57
58 #if 0
59 static void list_add(struct list_node *new, struct list_node *list)
60 {
61         struct list_node *next = list->next;
62
63         next->prev = new;
64         new->next = next;
65         new->prev = list;
66         list->next = new;
67 }
68 #endif
69
70 static void list_add_to_end(struct list_node *new, struct list_node *list)
71 {
72         struct list_node *prev = list->prev;
73
74         list->prev = new;
75         new->next = list;
76         new->prev = prev;
77         prev->next = new;
78 }
79
80 static void list_del(struct list_node *entry)
81 {
82         struct list_node *prev = entry->prev;
83         struct list_node *next = entry->next;
84
85         next->prev = prev;
86         prev->next = next;
87 }
88
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))
93
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))
99
100 struct udev_list *list_insert_entry(struct udev *udev, struct list_node *list,
101                                     const char *name, const char *value, int sort)
102 {
103         struct udev_list *list_loop;
104         struct udev_list *list_new;
105
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);
110                         return list_loop;
111                 }
112         }
113
114         if (sort) {
115                 list_for_each_entry(list_loop, list) {
116                         if (strcmp(list_loop->name, name) > 0)
117                                 break;
118                 }
119         }
120
121         list_new = malloc(sizeof(struct udev_list));
122         if (list_new == NULL)
123                 return 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) {
129                 free(list_new);
130                 return NULL;
131         }
132         if (value != NULL) {
133                 list_new->value = strdup(value);
134                 if (list_new->value == NULL) {
135                         free(list_new);
136                         return NULL;
137                 }
138         }
139         dbg(udev, "adding '%s=%s'\n", list_new->name, list_new->value);
140         list_add_to_end(&list_new->node, &list_loop->node);
141         return list_new;
142 }
143
144 void list_move_entry_to_end(struct udev_list *list_entry, struct list_node *list)
145 {
146         list_del(&list_entry->node);
147         list_add_to_end(&list_entry->node, list);
148 }
149
150 void list_cleanup(struct udev *udev, struct list_node *list)
151 {
152         struct udev_list *list_loop;
153         struct udev_list *list_tmp;
154
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);
159                 free(list_loop);
160         }
161 }
162
163 struct udev_list *list_get_entry(struct list_node *list)
164 {
165         if (list_empty(list))
166                 return NULL;
167         return node_to_entry(list->next);
168 }
169
170 struct udev_list *udev_list_entry_get_next(struct udev_list *list_entry)
171 {
172         struct list_node *next;
173
174         next = list_entry->node.next;
175         /* empty list or no more emtries */
176         if (next == list_entry->list)
177                 return NULL;
178         return node_to_entry(next);
179 }
180
181 const char *udev_list_entry_get_name(struct udev_list *list_entry)
182 {
183         if (list_entry == NULL)
184                 return NULL;
185         return list_entry->name;
186 }
187
188 const char *udev_list_entry_get_value(struct udev_list *list_entry)
189 {
190         if (list_entry == NULL)
191                 return NULL;
192         return list_entry->value;
193 }