]>
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/>.
25 #include "alloc-util.h"
26 #include "libudev-private.h"
29 * SECTION:libudev-list
30 * @short_description: list operation
32 * Libudev list operations.
38 * Opaque object representing one entry in a list. An entry contains
39 * contains a name, and optionally a value.
41 struct udev_list_entry
{
42 struct udev_list_node node
;
43 struct udev_list
*list
;
49 /* the list's head points to itself if empty */
50 void udev_list_node_init(struct udev_list_node
*list
)
56 int udev_list_node_is_empty(struct udev_list_node
*list
)
58 return list
->next
== list
;
61 static void udev_list_node_insert_between(struct udev_list_node
*new,
62 struct udev_list_node
*prev
,
63 struct udev_list_node
*next
)
71 void udev_list_node_append(struct udev_list_node
*new, struct udev_list_node
*list
)
73 udev_list_node_insert_between(new, list
->prev
, list
);
76 void udev_list_node_remove(struct udev_list_node
*entry
)
78 struct udev_list_node
*prev
= entry
->prev
;
79 struct udev_list_node
*next
= entry
->next
;
88 /* return list entry which embeds this node */
89 static inline struct udev_list_entry
*list_node_to_entry(struct udev_list_node
*node
)
91 return container_of(node
, struct udev_list_entry
, node
);
94 void udev_list_init(struct udev
*udev
, struct udev_list
*list
, bool unique
)
96 memzero(list
, sizeof(struct udev_list
));
98 list
->unique
= unique
;
99 udev_list_node_init(&list
->node
);
102 /* insert entry into a list as the last element */
103 static void udev_list_entry_append(struct udev_list_entry
*new, struct udev_list
*list
)
105 /* inserting before the list head make the node the last node in the list */
106 udev_list_node_insert_between(&new->node
, list
->node
.prev
, &list
->node
);
110 /* insert entry into a list, before a given existing entry */
111 static void udev_list_entry_insert_before(struct udev_list_entry
*new, struct udev_list_entry
*entry
)
113 udev_list_node_insert_between(&new->node
, entry
->node
.prev
, &entry
->node
);
114 new->list
= entry
->list
;
117 /* binary search in sorted array */
118 static int list_search(struct udev_list
*list
, const char *name
)
120 unsigned int first
, last
;
123 last
= list
->entries_cur
;
124 while (first
< last
) {
128 i
= (first
+ last
)/2;
129 cmp
= strcmp(name
, list
->entries
[i
]->name
);
138 /* not found, return negative insertion-index+1 */
142 struct udev_list_entry
*udev_list_entry_add(struct udev_list
*list
, const char *name
, const char *value
)
144 struct udev_list_entry
*entry
;
148 /* lookup existing name or insertion-index */
149 i
= list_search(list
, name
);
151 entry
= list
->entries
[i
];
158 entry
->value
= strdup(value
);
159 if (entry
->value
== NULL
)
166 entry
= new0(struct udev_list_entry
, 1);
169 entry
->name
= strdup(name
);
170 if (entry
->name
== NULL
) {
175 entry
->value
= strdup(value
);
176 if (entry
->value
== NULL
) {
184 /* allocate or enlarge sorted array if needed */
185 if (list
->entries_cur
>= list
->entries_max
) {
186 struct udev_list_entry
**entries
;
189 add
= list
->entries_max
;
192 entries
= realloc(list
->entries
, (list
->entries_max
+ add
) * sizeof(struct udev_list_entry
*));
193 if (entries
== NULL
) {
199 list
->entries
= entries
;
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
;
250 list
->entries
= mfree(list
->entries
);
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
);
257 struct udev_list_entry
*udev_list_get_entry(struct udev_list
*list
)
259 if (udev_list_node_is_empty(&list
->node
))
261 return list_node_to_entry(list
->node
.next
);
265 * udev_list_entry_get_next:
266 * @list_entry: current entry
268 * Get the next entry from the list.
270 * Returns: udev_list_entry, #NULL if no more entries are available.
272 _public_
struct udev_list_entry
*udev_list_entry_get_next(struct udev_list_entry
*list_entry
)
274 struct udev_list_node
*next
;
276 if (list_entry
== NULL
)
278 next
= list_entry
->node
.next
;
279 /* empty list or no more entries */
280 if (next
== &list_entry
->list
->node
)
282 return list_node_to_entry(next
);
286 * udev_list_entry_get_by_name:
287 * @list_entry: current entry
288 * @name: name string to match
290 * Lookup an entry in the list with a certain name.
292 * Returns: udev_list_entry, #NULL if no matching entry is found.
294 _public_
struct udev_list_entry
*udev_list_entry_get_by_name(struct udev_list_entry
*list_entry
, const char *name
)
298 if (list_entry
== NULL
)
301 if (!list_entry
->list
->unique
)
304 i
= list_search(list_entry
->list
, name
);
307 return list_entry
->list
->entries
[i
];
311 * udev_list_entry_get_name:
312 * @list_entry: current entry
314 * Get the name of a list entry.
316 * Returns: the name string of this entry.
318 _public_
const char *udev_list_entry_get_name(struct udev_list_entry
*list_entry
)
320 if (list_entry
== NULL
)
322 return list_entry
->name
;
326 * udev_list_entry_get_value:
327 * @list_entry: current entry
329 * Get the value of list entry.
331 * Returns: the value string of this entry.
333 _public_
const char *udev_list_entry_get_value(struct udev_list_entry
*list_entry
)
335 if (list_entry
== NULL
)
337 return list_entry
->value
;
340 int udev_list_entry_get_num(struct udev_list_entry
*list_entry
)
342 if (list_entry
== NULL
)
344 return list_entry
->num
;
347 void udev_list_entry_set_num(struct udev_list_entry
*list_entry
, int num
)
349 if (list_entry
== NULL
)
351 list_entry
->num
= num
;