]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-list.c
00e260193b573fcd300a03cb0791597581dd496a
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
13 #include "alloc-util.h"
14 #include "libudev-private.h"
17 * SECTION:libudev-list
18 * @short_description: list operation
20 * Libudev list operations.
26 * Opaque object representing one entry in a list. An entry contains
27 * contains a name, and optionally a value.
29 struct udev_list_entry
{
30 struct udev_list_node node
;
31 struct udev_list
*list
;
37 /* the list's head points to itself if empty */
38 void udev_list_node_init(struct udev_list_node
*list
)
44 int udev_list_node_is_empty(struct udev_list_node
*list
)
46 return list
->next
== list
;
49 static void udev_list_node_insert_between(struct udev_list_node
*new,
50 struct udev_list_node
*prev
,
51 struct udev_list_node
*next
)
59 void udev_list_node_append(struct udev_list_node
*new, struct udev_list_node
*list
)
61 udev_list_node_insert_between(new, list
->prev
, list
);
64 void udev_list_node_remove(struct udev_list_node
*entry
)
66 struct udev_list_node
*prev
= entry
->prev
;
67 struct udev_list_node
*next
= entry
->next
;
76 /* return list entry which embeds this node */
77 static inline struct udev_list_entry
*list_node_to_entry(struct udev_list_node
*node
)
79 return container_of(node
, struct udev_list_entry
, node
);
82 void udev_list_init(struct udev
*udev
, struct udev_list
*list
, bool unique
)
84 memzero(list
, sizeof(struct udev_list
));
86 list
->unique
= unique
;
87 udev_list_node_init(&list
->node
);
90 /* insert entry into a list as the last element */
91 static void udev_list_entry_append(struct udev_list_entry
*new, struct udev_list
*list
)
93 /* inserting before the list head make the node the last node in the list */
94 udev_list_node_insert_between(&new->node
, list
->node
.prev
, &list
->node
);
98 /* insert entry into a list, before a given existing entry */
99 static void udev_list_entry_insert_before(struct udev_list_entry
*new, struct udev_list_entry
*entry
)
101 udev_list_node_insert_between(&new->node
, entry
->node
.prev
, &entry
->node
);
102 new->list
= entry
->list
;
105 /* binary search in sorted array */
106 static int list_search(struct udev_list
*list
, const char *name
)
108 unsigned int first
, last
;
111 last
= list
->entries_cur
;
112 while (first
< last
) {
116 i
= (first
+ last
)/2;
117 cmp
= strcmp(name
, list
->entries
[i
]->name
);
126 /* not found, return negative insertion-index+1 */
130 struct udev_list_entry
*udev_list_entry_add(struct udev_list
*list
, const char *name
, const char *value
) {
131 struct udev_list_entry
*entry
;
135 /* lookup existing name or insertion-index */
136 i
= list_search(list
, name
);
138 entry
= list
->entries
[i
];
145 entry
->value
= strdup(value
);
153 entry
= new0(struct udev_list_entry
, 1);
157 entry
->name
= strdup(name
);
162 entry
->value
= strdup(value
);
170 /* allocate or enlarge sorted array if needed */
171 if (list
->entries_cur
>= list
->entries_max
) {
172 struct udev_list_entry
**entries
;
175 add
= list
->entries_max
;
178 entries
= reallocarray(list
->entries
, list
->entries_max
+ add
, sizeof(struct udev_list_entry
*));
184 list
->entries
= entries
;
185 list
->entries_max
+= add
;
188 /* the negative i returned the insertion index */
191 /* insert into sorted list */
192 if ((unsigned int)i
< list
->entries_cur
)
193 udev_list_entry_insert_before(entry
, list
->entries
[i
]);
195 udev_list_entry_append(entry
, list
);
197 /* insert into sorted array */
198 memmove(&list
->entries
[i
+1], &list
->entries
[i
],
199 (list
->entries_cur
- i
) * sizeof(struct udev_list_entry
*));
200 list
->entries
[i
] = entry
;
203 udev_list_entry_append(entry
, list
);
208 void udev_list_entry_delete(struct udev_list_entry
*entry
)
210 if (entry
->list
->entries
!= NULL
) {
212 struct udev_list
*list
= entry
->list
;
214 /* remove entry from sorted array */
215 i
= list_search(list
, entry
->name
);
217 memmove(&list
->entries
[i
], &list
->entries
[i
+1],
218 ((list
->entries_cur
-1) - i
) * sizeof(struct udev_list_entry
*));
223 udev_list_node_remove(&entry
->node
);
229 void udev_list_cleanup(struct udev_list
*list
)
231 struct udev_list_entry
*entry_loop
;
232 struct udev_list_entry
*entry_tmp
;
234 list
->entries
= mfree(list
->entries
);
235 list
->entries_cur
= 0;
236 list
->entries_max
= 0;
237 udev_list_entry_foreach_safe(entry_loop
, entry_tmp
, udev_list_get_entry(list
))
238 udev_list_entry_delete(entry_loop
);
241 struct udev_list_entry
*udev_list_get_entry(struct udev_list
*list
)
243 if (udev_list_node_is_empty(&list
->node
))
245 return list_node_to_entry(list
->node
.next
);
249 * udev_list_entry_get_next:
250 * @list_entry: current entry
252 * Get the next entry from the list.
254 * Returns: udev_list_entry, #NULL if no more entries are available.
256 _public_
struct udev_list_entry
*udev_list_entry_get_next(struct udev_list_entry
*list_entry
)
258 struct udev_list_node
*next
;
260 if (list_entry
== NULL
)
262 next
= list_entry
->node
.next
;
263 /* empty list or no more entries */
264 if (next
== &list_entry
->list
->node
)
266 return list_node_to_entry(next
);
270 * udev_list_entry_get_by_name:
271 * @list_entry: current entry
272 * @name: name string to match
274 * Lookup an entry in the list with a certain name.
276 * Returns: udev_list_entry, #NULL if no matching entry is found.
278 _public_
struct udev_list_entry
*udev_list_entry_get_by_name(struct udev_list_entry
*list_entry
, const char *name
)
282 if (list_entry
== NULL
)
285 if (!list_entry
->list
->unique
)
288 i
= list_search(list_entry
->list
, name
);
291 return list_entry
->list
->entries
[i
];
295 * udev_list_entry_get_name:
296 * @list_entry: current entry
298 * Get the name of a list entry.
300 * Returns: the name string of this entry.
302 _public_
const char *udev_list_entry_get_name(struct udev_list_entry
*list_entry
)
304 if (list_entry
== NULL
)
306 return list_entry
->name
;
310 * udev_list_entry_get_value:
311 * @list_entry: current entry
313 * Get the value of list entry.
315 * Returns: the value string of this entry.
317 _public_
const char *udev_list_entry_get_value(struct udev_list_entry
*list_entry
)
319 if (list_entry
== NULL
)
321 return list_entry
->value
;
324 int udev_list_entry_get_num(struct udev_list_entry
*list_entry
)
326 if (list_entry
== NULL
)
328 return list_entry
->num
;
331 void udev_list_entry_set_num(struct udev_list_entry
*list_entry
, int num
)
333 if (list_entry
== NULL
)
335 list_entry
->num
= num
;