4 |
* |
* |
5 |
* Copyright (C) 2003 Glenn McGrath |
* Copyright (C) 2003 Glenn McGrath |
6 |
* Copyright (C) 2005 Vladimir Oleynik |
* Copyright (C) 2005 Vladimir Oleynik |
7 |
* Copyright (C) 2005 Bernhard Fischer |
* Copyright (C) 2005 Bernhard Reutner-Fischer |
8 |
* Copyright (C) 2006 Rob Landley <rob@landley.net> |
* Copyright (C) 2006 Rob Landley <rob@landley.net> |
9 |
* |
* |
10 |
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
11 |
*/ |
*/ |
12 |
|
|
|
#include <stdlib.h> |
|
13 |
#include "libbb.h" |
#include "libbb.h" |
14 |
|
|
15 |
/* Add data to the start of the linked list. */ |
/* Add data to the start of the linked list. */ |
16 |
void llist_add_to(llist_t **old_head, void *data) |
void FAST_FUNC llist_add_to(llist_t **old_head, void *data) |
17 |
{ |
{ |
18 |
llist_t *new_head = xmalloc(sizeof(llist_t)); |
llist_t *new_head = xmalloc(sizeof(llist_t)); |
19 |
|
|
20 |
new_head->data = data; |
new_head->data = data; |
21 |
new_head->link = *old_head; |
new_head->link = *old_head; |
22 |
*old_head = new_head; |
*old_head = new_head; |
23 |
} |
} |
24 |
|
|
25 |
/* Add data to the end of the linked list. */ |
/* Add data to the end of the linked list. */ |
26 |
void llist_add_to_end(llist_t **list_head, void *data) |
void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) |
27 |
{ |
{ |
28 |
llist_t *new_item = xmalloc(sizeof(llist_t)); |
llist_t *new_item = xmalloc(sizeof(llist_t)); |
29 |
|
|
30 |
new_item->data = data; |
new_item->data = data; |
31 |
new_item->link = NULL; |
new_item->link = NULL; |
32 |
|
|
33 |
if (!*list_head) *list_head = new_item; |
if (!*list_head) |
34 |
|
*list_head = new_item; |
35 |
else { |
else { |
36 |
llist_t *tail = *list_head; |
llist_t *tail = *list_head; |
37 |
while (tail->link) tail = tail->link; |
|
38 |
|
while (tail->link) |
39 |
|
tail = tail->link; |
40 |
tail->link = new_item; |
tail->link = new_item; |
41 |
} |
} |
42 |
} |
} |
43 |
|
|
44 |
/* Remove first element from the list and return it */ |
/* Remove first element from the list and return it */ |
45 |
void *llist_pop(llist_t **head) |
void* FAST_FUNC llist_pop(llist_t **head) |
46 |
{ |
{ |
47 |
void *data; |
void *data, *next; |
48 |
|
|
49 |
if(!*head) data = *head; |
if (!*head) |
50 |
else { |
return NULL; |
51 |
void *next = (*head)->link; |
|
52 |
data = (*head)->data; |
data = (*head)->data; |
53 |
free(*head); |
next = (*head)->link; |
54 |
*head = next; |
free(*head); |
55 |
} |
*head = next; |
56 |
|
|
57 |
return data; |
return data; |
58 |
} |
} |
59 |
|
|
60 |
|
/* Unlink arbitrary given element from the list */ |
61 |
|
void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
62 |
|
{ |
63 |
|
llist_t *crt; |
64 |
|
|
65 |
|
if (!(elm && *head)) |
66 |
|
return; |
67 |
|
|
68 |
|
if (elm == *head) { |
69 |
|
*head = (*head)->link; |
70 |
|
return; |
71 |
|
} |
72 |
|
|
73 |
|
for (crt = *head; crt; crt = crt->link) { |
74 |
|
if (crt->link == elm) { |
75 |
|
crt->link = elm->link; |
76 |
|
return; |
77 |
|
} |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
/* Recursively free all elements in the linked list. If freeit != NULL |
/* Recursively free all elements in the linked list. If freeit != NULL |
82 |
* call it on each datum in the list */ |
* call it on each datum in the list */ |
83 |
void llist_free(llist_t *elm, void (*freeit)(void *data)) |
void FAST_FUNC llist_free(llist_t *elm, void (*freeit) (void *data)) |
84 |
{ |
{ |
85 |
while (elm) { |
while (elm) { |
86 |
void *data = llist_pop(&elm); |
void *data = llist_pop(&elm); |
87 |
if (freeit) freeit(data); |
|
88 |
|
if (freeit) |
89 |
|
freeit(data); |
90 |
} |
} |
91 |
} |
} |
92 |
|
|
93 |
/* Reverse list order. Useful since getopt32 saves option params |
/* Reverse list order. */ |
94 |
* in reverse order */ |
llist_t* FAST_FUNC llist_rev(llist_t *list) |
|
llist_t* rev_llist(llist_t *list) |
|
95 |
{ |
{ |
96 |
llist_t *new = NULL; |
llist_t *rev = NULL; |
97 |
|
|
98 |
while (list) { |
while (list) { |
99 |
llist_t *next = list->link; |
llist_t *next = list->link; |
100 |
list->link = new; |
|
101 |
new = list; |
list->link = rev; |
102 |
|
rev = list; |
103 |
list = next; |
list = next; |
104 |
} |
} |
105 |
return new; |
return rev; |
106 |
} |
} |