blob: fde25e8b7c553dbcb5b888bc0a3dd67ed21c7d5d [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 Landley8bb50782006-05-26 23:44:51 +000017void llist_add_to(llist_t **old_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000018{
Rob Landley8bb50782006-05-26 23:44:51 +000019 llist_t *new_head = xmalloc(sizeof(llist_t));
Rob Landley5edc1022006-05-26 23:00:10 +000020 new_head->data = data;
Rob Landley8bb50782006-05-26 23:44:51 +000021 new_head->link = *old_head;
22 *old_head = new_head;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000023}
24#endif
25
26#ifdef L_llist_add_to_end
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000027/* Add data to the end of the linked list. */
Rob Landley8bb50782006-05-26 23:44:51 +000028void llist_add_to_end(llist_t **list_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000029{
Rob Landley8bb50782006-05-26 23:44:51 +000030 llist_t *new_item = xmalloc(sizeof(llist_t));
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000031 new_item->data = data;
32 new_item->link = NULL;
33
Rob Landley8bb50782006-05-26 23:44:51 +000034 if (!*list_head) *list_head = new_item;
35 else {
36 llist_t *tail = *list_head;
37 while (tail->link) tail = tail->link;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000038 tail->link = new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000039 }
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000040}
41#endif
42
Rob Landleya6b5b602006-05-08 19:03:07 +000043#ifdef L_llist_pop
44/* Remove first element from the list and return it */
45void *llist_pop(llist_t **head)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000046{
Rob Landleya6b5b602006-05-08 19:03:07 +000047 void *data;
48
49 if(!*head) data = *head;
50 else {
51 void *next = (*head)->link;
52 data = (*head)->data;
Rob Landleycc848dd2006-05-11 18:25:24 +000053 free(*head);
54 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000055 }
56
57 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000058}
59#endif
60
61#ifdef L_llist_free
Rob Landleya6b5b602006-05-08 19:03:07 +000062/* Recursively free all elements in the linked list. If freeit != NULL
63 * call it on each datum in the list */
64void llist_free(llist_t *elm, void (*freeit)(void *data))
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000065{
Rob Landleya6b5b602006-05-08 19:03:07 +000066 while (elm) {
67 void *data = llist_pop(&elm);
68 if (freeit) freeit(data);
69 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000070}
71#endif