chiark / gitweb /
rules: delete arch specific rules
[elogind.git] / src / 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 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.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18
19 #include "libudev.h"
20 #include "libudev-private.h"
21
22 /**
23  * SECTION:libudev-list
24  * @short_description: list operation
25  *
26  * Libudev list operations.
27  */
28
29 /**
30  * udev_list_entry:
31  *
32  * Opaque object representing one entry in a list. An entry contains
33  * contains a name, and optionally a value.
34  */
35 struct udev_list_entry {
36         struct udev_list_node node;
37         struct udev_list *list;
38         char *name;
39         char *value;
40         int num;
41 };
42
43 /* the list's head points to itself if empty */
44 void udev_list_node_init(struct udev_list_node *list)
45 {
46         list->next = list;
47         list->prev = list;
48 }
49
50 int udev_list_node_is_empty(struct udev_list_node *list)
51 {
52         return list->next == list;
53 }
54
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)
58 {
59         next->prev = new;
60         new->next = next;
61         new->prev = prev;
62         prev->next = new;
63 }
64
65 void udev_list_node_append(struct udev_list_node *new, struct udev_list_node *list)
66 {
67         udev_list_node_insert_between(new, list->prev, list);
68 }
69
70 void udev_list_node_remove(struct udev_list_node *entry)
71 {
72         struct udev_list_node *prev = entry->prev;
73         struct udev_list_node *next = entry->next;
74
75         next->prev = prev;
76         prev->next = next;
77
78         entry->prev = NULL;
79         entry->next = NULL;
80 }
81
82 /* return list entry which embeds this node */
83 static struct udev_list_entry *list_node_to_entry(struct udev_list_node *node)
84 {
85         char *list;
86
87         list = (char *)node;
88         list -= offsetof(struct udev_list_entry, node);
89         return (struct udev_list_entry *)list;
90 }
91
92 void udev_list_init(struct udev *udev, struct udev_list *list, bool unique)
93 {
94         memset(list, 0x00, sizeof(struct udev_list));
95         list->udev = udev;
96         list->unique = unique;
97         udev_list_node_init(&list->node);
98 }
99
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)
102 {
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);
105         new->list = list;
106 }
107
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)
110 {
111         udev_list_node_insert_between(&new->node, entry->node.prev, &entry->node);
112         new->list = entry->list;
113 }
114
115 /* binary search in sorted array */
116 static int list_search(struct udev_list *list, const char *name)
117 {
118         unsigned int first, last;
119
120         first = 0;
121         last = list->entries_cur;
122         while (first < last) {
123                 unsigned int i;
124                 int cmp;
125
126                 i = (first + last)/2;
127                 cmp = strcmp(name, list->entries[i]->name);
128                 if (cmp < 0)
129                         last = i;
130                 else if (cmp > 0)
131                         first = i+1;
132                 else
133                         return i;
134         }
135
136         /* not found, return negative insertion-index+1 */
137         return -(first+1);
138 }
139
140 struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *name, const char *value)
141 {
142         struct udev_list_entry *entry;
143         int i = 0;
144
145         if (list->unique) {
146                 /* lookup existing name or insertion-index */
147                 i = list_search(list, name);
148                 if (i >= 0) {
149                         entry = list->entries[i];
150
151                         dbg(list->udev, "'%s' is already in the list\n", name);
152                         free(entry->value);
153                         if (value == NULL) {
154                                 entry->value = NULL;
155                                 dbg(list->udev, "'%s' value unset\n", name);
156                                 return entry;
157                         }
158                         entry->value = strdup(value);
159                         if (entry->value == NULL)
160                                 return NULL;
161                         dbg(list->udev, "'%s' value replaced with '%s'\n", name, value);
162                         return entry;
163                 }
164         }
165
166         /* add new name */
167         entry = calloc(1, sizeof(struct udev_list_entry));
168         if (entry == NULL)
169                 return NULL;
170         entry->name = strdup(name);
171         if (entry->name == NULL) {
172                 free(entry);
173                 return NULL;
174         }
175         if (value != NULL) {
176                 entry->value = strdup(value);
177                 if (entry->value == NULL) {
178                         free(entry->name);
179                         free(entry);
180                         return NULL;
181                 }
182         }
183
184         if (list->unique) {
185                 /* allocate or enlarge sorted array if needed */
186                 if (list->entries_cur >= list->entries_max) {
187                         unsigned int add;
188
189                         add = list->entries_max;
190                         if (add < 1)
191                                 add = 64;
192                         list->entries = realloc(list->entries, (list->entries_max + add) * sizeof(struct udev_list_entry *));
193                         if (list->entries == NULL) {
194                                 free(entry->name);
195                                 free(entry->value);
196                                 return NULL;
197                         }
198                         list->entries_max += add;
199                 }
200
201                 /* the negative i returned the insertion index */
202                 i = (-i)-1;
203
204                 /* insert into sorted list */
205                 if ((unsigned int)i < list->entries_cur)
206                         udev_list_entry_insert_before(entry, list->entries[i]);
207                 else
208                         udev_list_entry_append(entry, list);
209
210                 /* insert into sorted array */
211                 memmove(&list->entries[i+1], &list->entries[i],
212                         (list->entries_cur - i) * sizeof(struct udev_list_entry *));
213                 list->entries[i] = entry;
214                 list->entries_cur++;
215         } else {
216                 udev_list_entry_append(entry, list);
217         }
218
219         dbg(list->udev, "'%s=%s' added\n", entry->name, entry->value);
220         return entry;
221 }
222
223 void udev_list_entry_delete(struct udev_list_entry *entry)
224 {
225         if (entry->list->entries != NULL) {
226                 int i;
227                 struct udev_list *list = entry->list;
228
229                 /* remove entry from sorted array */
230                 i = list_search(list, entry->name);
231                 if (i >= 0) {
232                         memmove(&list->entries[i], &list->entries[i+1],
233                                 ((list->entries_cur-1) - i) * sizeof(struct udev_list_entry *));
234                         list->entries_cur--;
235                 }
236         }
237
238         udev_list_node_remove(&entry->node);
239         free(entry->name);
240         free(entry->value);
241         free(entry);
242 }
243
244 void udev_list_cleanup(struct udev_list *list)
245 {
246         struct udev_list_entry *entry_loop;
247         struct udev_list_entry *entry_tmp;
248
249         free(list->entries);
250         list->entries = NULL;
251         list->entries_cur = 0;
252         list->entries_max = 0;
253         udev_list_entry_foreach_safe(entry_loop, entry_tmp, udev_list_get_entry(list))
254                 udev_list_entry_delete(entry_loop);
255 }
256
257 struct udev_list_entry *udev_list_get_entry(struct udev_list *list)
258 {
259         if (udev_list_node_is_empty(&list->node))
260                 return NULL;
261         return list_node_to_entry(list->node.next);
262 }
263
264 /**
265  * udev_list_entry_get_next:
266  * @list_entry: current entry
267  *
268  * Returns: the next entry from the list, #NULL is no more entries are found.
269  */
270 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
271 {
272         struct udev_list_node *next;
273
274         if (list_entry == NULL)
275                 return NULL;
276         next = list_entry->node.next;
277         /* empty list or no more entries */
278         if (next == &list_entry->list->node)
279                 return NULL;
280         return list_node_to_entry(next);
281 }
282
283 /**
284  * udev_list_entry_get_by_name:
285  * @list_entry: current entry
286  * @name: name string to match
287  *
288  * Returns: the entry where @name matched, #NULL if no matching entry is found.
289  */
290 UDEV_EXPORT struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
291 {
292         int i;
293
294         if (list_entry == NULL)
295                 return NULL;
296
297         if (!list_entry->list->unique)
298                 return NULL;
299
300         i = list_search(list_entry->list, name);
301         if (i < 0)
302                 return NULL;
303         return list_entry->list->entries[i];
304 }
305
306 /**
307  * udev_list_entry_get_name:
308  * @list_entry: current entry
309  *
310  * Returns: the name string of this entry.
311  */
312 UDEV_EXPORT const char *udev_list_entry_get_name(struct udev_list_entry *list_entry)
313 {
314         if (list_entry == NULL)
315                 return NULL;
316         return list_entry->name;
317 }
318
319 /**
320  * udev_list_entry_get_value:
321  * @list_entry: current entry
322  *
323  * Returns: the value string of this entry.
324  */
325 UDEV_EXPORT const char *udev_list_entry_get_value(struct udev_list_entry *list_entry)
326 {
327         if (list_entry == NULL)
328                 return NULL;
329         return list_entry->value;
330 }
331
332 int udev_list_entry_get_num(struct udev_list_entry *list_entry)
333 {
334         if (list_entry == NULL)
335                 return -EINVAL;
336         return list_entry->num;
337 }
338
339 void udev_list_entry_set_num(struct udev_list_entry *list_entry, int num)
340 {
341         if (list_entry == NULL)
342                 return;
343         list_entry->num = num;
344 }