blob: 842e8f7bbb20eb2ac67168f324f4c1979a54f451 [file] [log] [blame]
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +00001/* 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
Rob Landleycc848dd2006-05-11 18:25:24 +00008 * Copyright (C) 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +00009 *
10 * Licensed under the GPL v2, see the file LICENSE in this tarball.
11 */
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000012#include <stdlib.h>
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000013#include "libbb.h"
14
15#ifdef L_llist_add_to
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000016/* Add data to the start of the linked list. */
Rob Landleydfba7412006-03-06 20:47:33 +000017llist_t *llist_add_to(llist_t *old_head, char *new_item)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000018{
19 llist_t *new_head;
20
21 new_head = xmalloc(sizeof(llist_t));
22 new_head->data = new_item;
23 new_head->link = old_head;
24
25 return (new_head);
26}
27#endif
28
29#ifdef L_llist_add_to_end
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000030/* Add data to the end of the linked list. */
Rob Landleydfba7412006-03-06 20:47:33 +000031llist_t *llist_add_to_end(llist_t *list_head, char *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000032{
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000033 llist_t *new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000034
35 new_item = xmalloc(sizeof(llist_t));
36 new_item->data = data;
37 new_item->link = NULL;
38
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000039 if (list_head == NULL) {
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000040 list_head = new_item;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000041 } else {
42 llist_t *tail = list_head;
43 while (tail->link)
44 tail = tail->link;
45 tail->link = new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000046 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000047 return list_head;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000048}
49#endif
50
Rob Landleya6b5b602006-05-08 19:03:07 +000051#ifdef L_llist_pop
52/* Remove first element from the list and return it */
53void *llist_pop(llist_t **head)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000054{
Rob Landleya6b5b602006-05-08 19:03:07 +000055 void *data;
56
57 if(!*head) data = *head;
58 else {
59 void *next = (*head)->link;
60 data = (*head)->data;
Rob Landleycc848dd2006-05-11 18:25:24 +000061 free(*head);
62 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000063 }
64
65 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000066}
67#endif
68
69#ifdef L_llist_free
Rob Landleya6b5b602006-05-08 19:03:07 +000070/* Recursively free all elements in the linked list. If freeit != NULL
71 * call it on each datum in the list */
72void llist_free(llist_t *elm, void (*freeit)(void *data))
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000073{
Rob Landleya6b5b602006-05-08 19:03:07 +000074 while (elm) {
75 void *data = llist_pop(&elm);
76 if (freeit) freeit(data);
77 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000078}
79#endif