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)); |
while (*list_head) |
29 |
new_item->data = data; |
list_head = &(*list_head)->link; |
30 |
new_item->link = NULL; |
*list_head = xzalloc(sizeof(llist_t)); |
31 |
|
(*list_head)->data = data; |
32 |
if (!*list_head) *list_head = new_item; |
/*(*list_head)->link = NULL;*/ |
|
else { |
|
|
llist_t *tail = *list_head; |
|
|
while (tail->link) tail = tail->link; |
|
|
tail->link = new_item; |
|
|
} |
|
33 |
} |
} |
34 |
|
|
35 |
/* Remove first element from the list and return it */ |
/* Remove first element from the list and return it */ |
36 |
void *llist_pop(llist_t **head) |
void* FAST_FUNC llist_pop(llist_t **head) |
37 |
{ |
{ |
38 |
void *data; |
void *data = NULL; |
39 |
|
llist_t *temp = *head; |
40 |
|
|
41 |
if(!*head) data = *head; |
if (temp) { |
42 |
else { |
data = temp->data; |
43 |
void *next = (*head)->link; |
*head = temp->link; |
44 |
data = (*head)->data; |
free(temp); |
|
free(*head); |
|
|
*head = next; |
|
45 |
} |
} |
|
|
|
46 |
return data; |
return data; |
47 |
} |
} |
48 |
|
|
49 |
|
/* Unlink arbitrary given element from the list */ |
50 |
|
void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
51 |
|
{ |
52 |
|
if (!elm) |
53 |
|
return; |
54 |
|
while (*head) { |
55 |
|
if (*head == elm) { |
56 |
|
*head = (*head)->link; |
57 |
|
break; |
58 |
|
} |
59 |
|
head = &(*head)->link; |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
/* Recursively free all elements in the linked list. If freeit != NULL |
/* Recursively free all elements in the linked list. If freeit != NULL |
64 |
* call it on each datum in the list */ |
* call it on each datum in the list */ |
65 |
void llist_free(llist_t *elm, void (*freeit)(void *data)) |
void FAST_FUNC llist_free(llist_t *elm, void (*freeit) (void *data)) |
66 |
{ |
{ |
67 |
while (elm) { |
while (elm) { |
68 |
void *data = llist_pop(&elm); |
void *data = llist_pop(&elm); |
69 |
if (freeit) freeit(data); |
|
70 |
|
if (freeit) |
71 |
|
freeit(data); |
72 |
} |
} |
73 |
} |
} |
74 |
|
|
75 |
/* Reverse list order. Useful since getopt32 saves option params |
/* Reverse list order. */ |
76 |
* in reverse order */ |
llist_t* FAST_FUNC llist_rev(llist_t *list) |
|
llist_t* rev_llist(llist_t *list) |
|
77 |
{ |
{ |
78 |
llist_t *new = NULL; |
llist_t *rev = NULL; |
79 |
|
|
80 |
while (list) { |
while (list) { |
81 |
llist_t *next = list->link; |
llist_t *next = list->link; |
82 |
list->link = new; |
|
83 |
new = list; |
list->link = rev; |
84 |
|
rev = list; |
85 |
list = next; |
list = next; |
86 |
} |
} |
87 |
return new; |
return rev; |
88 |
|
} |
89 |
|
|
90 |
|
llist_t* FAST_FUNC llist_find_str(llist_t *list, const char *str) |
91 |
|
{ |
92 |
|
while (list) { |
93 |
|
if (strcmp(list->data, str) == 0) |
94 |
|
break; |
95 |
|
list = list->link; |
96 |
|
} |
97 |
|
return list; |
98 |
} |
} |