Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/libbb/llist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 984 - (hide annotations) (download)
Sun May 30 11:32:42 2010 UTC (14 years ago) by niro
File MIME type: text/plain
File size: 1977 byte(s)
-updated to busybox-1.16.1 and enabled blkid/uuid support in default config
1 niro 532 /* 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 niro 816 * Copyright (C) 2005 Bernhard Reutner-Fischer
8 niro 532 * 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 "libbb.h"
14    
15     /* Add data to the start of the linked list. */
16 niro 816 void FAST_FUNC llist_add_to(llist_t **old_head, void *data)
17 niro 532 {
18     llist_t *new_head = xmalloc(sizeof(llist_t));
19 niro 816
20 niro 532 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. */
26 niro 816 void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data)
27 niro 532 {
28 niro 984 while (*list_head)
29     list_head = &(*list_head)->link;
30     *list_head = xzalloc(sizeof(llist_t));
31     (*list_head)->data = data;
32     /*(*list_head)->link = NULL;*/
33 niro 532 }
34    
35     /* Remove first element from the list and return it */
36 niro 816 void* FAST_FUNC llist_pop(llist_t **head)
37 niro 532 {
38 niro 984 void *data = NULL;
39     llist_t *temp = *head;
40 niro 532
41 niro 984 if (temp) {
42     data = temp->data;
43     *head = temp->link;
44     free(temp);
45     }
46 niro 532 return data;
47     }
48    
49 niro 816 /* Unlink arbitrary given element from the list */
50     void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm)
51     {
52 niro 984 if (!elm)
53 niro 816 return;
54 niro 984 while (*head) {
55     if (*head == elm) {
56     *head = (*head)->link;
57     break;
58 niro 816 }
59 niro 984 head = &(*head)->link;
60 niro 816 }
61     }
62    
63 niro 532 /* Recursively free all elements in the linked list. If freeit != NULL
64     * call it on each datum in the list */
65 niro 816 void FAST_FUNC llist_free(llist_t *elm, void (*freeit) (void *data))
66 niro 532 {
67     while (elm) {
68     void *data = llist_pop(&elm);
69 niro 816
70     if (freeit)
71     freeit(data);
72 niro 532 }
73     }
74    
75 niro 816 /* Reverse list order. */
76     llist_t* FAST_FUNC llist_rev(llist_t *list)
77 niro 532 {
78 niro 816 llist_t *rev = NULL;
79    
80 niro 532 while (list) {
81     llist_t *next = list->link;
82 niro 816
83     list->link = rev;
84     rev = list;
85 niro 532 list = next;
86     }
87 niro 816 return rev;
88 niro 532 }
89 niro 984
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     }