- fix the build if we have ftpput without ftpget (speeling reeoe)
[oweals/busybox.git] / libbb / llist.c
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, see the file LICENSE in this tarball.
11  */
12 #include <stdlib.h>
13 #include "libbb.h"
14
15 #ifdef L_llist_add_to
16 /* Add data to the start of the linked list.  */
17 void 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 #endif
25
26 #ifdef L_llist_add_to_end
27 /* Add data to the end of the linked list.  */
28 void llist_add_to_end(llist_t **list_head, void *data)
29 {
30         llist_t *new_item = xmalloc(sizeof(llist_t));
31         new_item->data = data;
32         new_item->link = NULL;
33
34         if (!*list_head) *list_head = new_item;
35         else {
36                 llist_t *tail = *list_head;
37                 while (tail->link) tail = tail->link;
38                 tail->link = new_item;
39         }
40 }
41 #endif
42
43 #ifdef L_llist_pop
44 /* Remove first element from the list and return it */
45 void *llist_pop(llist_t **head)
46 {
47         void *data;
48
49         if(!*head) data = *head;
50         else {
51                 void *next = (*head)->link;
52                 data = (*head)->data;
53                 free(*head);
54                 *head = next;
55         }
56
57         return data;
58 }
59 #endif
60
61 #ifdef L_llist_free
62 /* Recursively free all elements in the linked list.  If freeit != NULL
63  * call it on each datum in the list */
64 void llist_free(llist_t *elm, void (*freeit)(void *data))
65 {
66         while (elm) {
67                 void *data = llist_pop(&elm);
68                 if (freeit) freeit(data);
69         }
70 }
71 #endif