]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-list.c
2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "libudev-private.h"
31 * SECTION:libudev-list
32 * @short_description: list operation
34 * Libudev list operations.
40 * Opaque object representing one entry in a list. An entry contains
41 * contains a name, and optionally a value.
43 struct udev_list_entry
{
44 struct udev_list_node node
;
45 struct udev_list
*list
;
51 /* the list's head points to itself if empty */
52 void udev_list_node_init(struct udev_list_node
*list
)
58 int udev_list_node_is_empty(struct udev_list_node
*list
)
60 return list
->next
== list
;
63 static void udev_list_node_insert_between(struct udev_list_node
*new,
64 struct udev_list_node
*prev
,
65 struct udev_list_node
*next
)
73 void udev_list_node_append(struct udev_list_node
*new, struct udev_list_node
*list
)
75 udev_list_node_insert_between(new, list
->prev
, list
);
78 void udev_list_node_remove(struct udev_list_node
*entry
)
80 struct udev_list_node
*prev
= entry
->prev
;
81 struct udev_list_node
*next
= entry
->next
;
90 /* return list entry which embeds this node */
91 static inline struct udev_list_entry
*list_node_to_entry(struct udev_list_node
*node
)
93 return container_of(node
, struct udev_list_entry
, node
);
96 void udev_list_init(struct udev
*udev
, struct udev_list
*list
, bool unique
)
98 memset(list
, 0x00, sizeof(struct udev_list
));
100 list
->unique
= unique
;
101 udev_list_node_init(&list
->node
);
104 /* insert entry into a list as the last element */
105 void udev_list_entry_append(struct udev_list_entry
*new, struct udev_list
*list
)
107 /* inserting before the list head make the node the last node in the list */
108 udev_list_node_insert_between(&new->node
, list
->node
.prev
, &list
->node
);
112 /* insert entry into a list, before a given existing entry */
113 void udev_list_entry_insert_before(struct udev_list_entry
*new, struct udev_list_entry
*entry
)
115 udev_list_node_insert_between(&new->node
, entry
->node
.prev
, &entry
->node
);
116 new->list
= entry
->list
;
119 /* binary search in sorted array */
120 static int list_search(struct udev_list
*list
, const char *name
)
122 unsigned int first
, last
;
125 last
= list
->entries_cur
;
126 while (first
< last
) {
130 i
= (first
+ last
)/2;
131 cmp
= strcmp(name
, list
->entries
[i
]->name
);
140 /* not found, return negative insertion-index+1 */
144 struct udev_list_entry
*udev_list_entry_add(struct udev_list
*list
, const char *name
, const char *value
)
146 struct udev_list_entry
*entry
;
150 /* lookup existing name or insertion-index */
151 i
= list_search(list
, name
);
153 entry
= list
->entries
[i
];
160 entry
->value
= strdup(value
);
161 if (entry
->value
== NULL
)
168 entry
= calloc(1, sizeof(struct udev_list_entry
));
171 entry
->name
= strdup(name
);
172 if (entry
->name
== NULL
) {
177 entry
->value
= strdup(value
);
178 if (entry
->value
== NULL
) {
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
) {
200 list
->entries_max
+= add
;
203 /* the negative i returned the insertion index */
206 /* insert into sorted list */
207 if ((unsigned int)i
< list
->entries_cur
)
208 udev_list_entry_insert_before(entry
, list
->entries
[i
]);
210 udev_list_entry_append(entry
, list
);
212 /* insert into sorted array */
213 memmove(&list
->entries
[i
+1], &list
->entries
[i
],
214 (list
->entries_cur
- i
) * sizeof(struct udev_list_entry
*));
215 list
->entries
[i
] = entry
;
218 udev_list_entry_append(entry
, list
);
224 void udev_list_entry_delete(struct udev_list_entry
*entry
)
226 if (entry
->list
->entries
!= NULL
) {
228 struct udev_list
*list
= entry
->list
;
230 /* remove entry from sorted array */
231 i
= list_search(list
, entry
->name
);
233 memmove(&list
->entries
[i
], &list
->entries
[i
+1],
234 ((list
->entries_cur
-1) - i
) * sizeof(struct udev_list_entry
*));
239 udev_list_node_remove(&entry
->node
);
245 void udev_list_cleanup(struct udev_list
*list
)
247 struct udev_list_entry
*entry_loop
;
248 struct udev_list_entry
*entry_tmp
;
251 list
->entries
= NULL
;
252 list
->entries_cur
= 0;
253 list
->entries_max
= 0;
254 udev_list_entry_foreach_safe(entry_loop
, entry_tmp
, udev_list_get_entry(list
))
255 udev_list_entry_delete(entry_loop
);
258 struct udev_list_entry
*udev_list_get_entry(struct udev_list
*list
)
260 if (udev_list_node_is_empty(&list
->node
))
262 return list_node_to_entry(list
->node
.next
);
266 * udev_list_entry_get_next:
267 * @list_entry: current entry
269 * Get the next entry from the list.
271 * Returns: udev_list_entry, #NULL if no more entries are available.
273 _public_
struct udev_list_entry
*udev_list_entry_get_next(struct udev_list_entry
*list_entry
)
275 struct udev_list_node
*next
;
277 if (list_entry
== NULL
)
279 next
= list_entry
->node
.next
;
280 /* empty list or no more entries */
281 if (next
== &list_entry
->list
->node
)
283 return list_node_to_entry(next
);
287 * udev_list_entry_get_by_name:
288 * @list_entry: current entry
289 * @name: name string to match
291 * Lookup an entry in the list with a certain name.
293 * Returns: udev_list_entry, #NULL if no matching entry is found.
295 _public_
struct udev_list_entry
*udev_list_entry_get_by_name(struct udev_list_entry
*list_entry
, const char *name
)
299 if (list_entry
== NULL
)
302 if (!list_entry
->list
->unique
)
305 i
= list_search(list_entry
->list
, name
);
308 return list_entry
->list
->entries
[i
];
312 * udev_list_entry_get_name:
313 * @list_entry: current entry
315 * Get the name of a list entry.
317 * Returns: the name string of this entry.
319 _public_
const char *udev_list_entry_get_name(struct udev_list_entry
*list_entry
)
321 if (list_entry
== NULL
)
323 return list_entry
->name
;
327 * udev_list_entry_get_value:
328 * @list_entry: current entry
330 * Get the value of list entry.
332 * Returns: the value string of this entry.
334 _public_
const char *udev_list_entry_get_value(struct udev_list_entry
*list_entry
)
336 if (list_entry
== NULL
)
338 return list_entry
->value
;
341 int udev_list_entry_get_num(struct udev_list_entry
*list_entry
)
343 if (list_entry
== NULL
)
345 return list_entry
->num
;
348 void udev_list_entry_set_num(struct udev_list_entry
*list_entry
, int num
)
350 if (list_entry
== NULL
)
352 list_entry
->num
= num
;