blob: 13b974decd8cdcd9edc1f9661e7f43b57c43607b [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 */
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +000012
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000013#include <stdlib.h>
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000014#include "libbb.h"
15
16#ifdef L_llist_add_to
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000017/* Add data to the start of the linked list. */
Rob Landley8bb50782006-05-26 23:44:51 +000018void llist_add_to(llist_t **old_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000019{
Rob Landley8bb50782006-05-26 23:44:51 +000020 llist_t *new_head = xmalloc(sizeof(llist_t));
Rob Landley5edc1022006-05-26 23:00:10 +000021 new_head->data = data;
Rob Landley8bb50782006-05-26 23:44:51 +000022 new_head->link = *old_head;
23 *old_head = new_head;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000024}
25#endif
26
27#ifdef L_llist_add_to_end
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000028/* Add data to the end of the linked list. */
Rob Landley8bb50782006-05-26 23:44:51 +000029void llist_add_to_end(llist_t **list_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000030{
Rob Landley8bb50782006-05-26 23:44:51 +000031 llist_t *new_item = xmalloc(sizeof(llist_t));
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000032 new_item->data = data;
33 new_item->link = NULL;
34
Rob Landley8bb50782006-05-26 23:44:51 +000035 if (!*list_head) *list_head = new_item;
36 else {
37 llist_t *tail = *list_head;
38 while (tail->link) tail = tail->link;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000039 tail->link = new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000040 }
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000041}
42#endif
43
Rob Landleya6b5b602006-05-08 19:03:07 +000044#ifdef L_llist_pop
45/* Remove first element from the list and return it */
46void *llist_pop(llist_t **head)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000047{
Rob Landleya6b5b602006-05-08 19:03:07 +000048 void *data;
49
50 if(!*head) data = *head;
51 else {
52 void *next = (*head)->link;
53 data = (*head)->data;
Rob Landleycc848dd2006-05-11 18:25:24 +000054 free(*head);
55 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000056 }
57
58 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000059}
60#endif
61
62#ifdef L_llist_free
Rob Landleya6b5b602006-05-08 19:03:07 +000063/* Recursively free all elements in the linked list. If freeit != NULL
64 * call it on each datum in the list */
65void llist_free(llist_t *elm, void (*freeit)(void *data))
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000066{
Rob Landleya6b5b602006-05-08 19:03:07 +000067 while (elm) {
68 void *data = llist_pop(&elm);
69 if (freeit) freeit(data);
70 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000071}
72#endif