summaryrefslogtreecommitdiff
path: root/libbb/llist.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-12-27 04:35:04 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-12-27 04:35:04 +0000
commit8d42f86b146871ae4c4cafd3801a85f381249a14 (patch)
treeb963999fc54eddb65f1929b894f868e24851fc9c /libbb/llist.c
downloadbusybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.tar.gz
busybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.tar.bz2
busybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.zip
Correcting branch name to be like previous ones
Diffstat (limited to 'libbb/llist.c')
-rw-r--r--libbb/llist.c78
1 files changed, 78 insertions, 0 deletions
diff --git a/libbb/llist.c b/libbb/llist.c
new file mode 100644
index 000000000..8a74832ee
--- /dev/null
+++ b/libbb/llist.c
@@ -0,0 +1,78 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * linked list helper functions.
4 *
5 * Copyright (C) 2003 Glenn McGrath
6 * Copyright (C) 2005 Vladimir Oleynik
7 * Copyright (C) 2005 Bernhard Fischer
8 * Copyright (C) 2006 Rob Landley <rob@landley.net>
9 *
10 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
11 */
12
13#include <stdlib.h>
14#include "libbb.h"
15
16/* Add data to the start of the linked list. */
17void llist_add_to(llist_t **old_head, void *data)
18{
19 llist_t *new_head = xmalloc(sizeof(llist_t));
20 new_head->data = data;
21 new_head->link = *old_head;
22 *old_head = new_head;
23}
24
25/* Add data to the end of the linked list. */
26void llist_add_to_end(llist_t **list_head, void *data)
27{
28 llist_t *new_item = xmalloc(sizeof(llist_t));
29 new_item->data = data;
30 new_item->link = NULL;
31
32 if (!*list_head) *list_head = new_item;
33 else {
34 llist_t *tail = *list_head;
35 while (tail->link) tail = tail->link;
36 tail->link = new_item;
37 }
38}
39
40/* Remove first element from the list and return it */
41void *llist_pop(llist_t **head)
42{
43 void *data;
44
45 if(!*head) data = *head;
46 else {
47 void *next = (*head)->link;
48 data = (*head)->data;
49 free(*head);
50 *head = next;
51 }
52
53 return data;
54}
55
56/* Recursively free all elements in the linked list. If freeit != NULL
57 * call it on each datum in the list */
58void llist_free(llist_t *elm, void (*freeit)(void *data))
59{
60 while (elm) {
61 void *data = llist_pop(&elm);
62 if (freeit) freeit(data);
63 }
64}
65
66/* Reverse list order. Useful since getopt32 saves option params
67 * in reverse order */
68llist_t* rev_llist(llist_t *list)
69{
70 llist_t *new = NULL;
71 while (list) {
72 llist_t *next = list->link;
73 list->link = new;
74 new = list;
75 list = next;
76 }
77 return new;
78}