blob: 8a74832ee3ff54e50d08c4e86f0c1daefbf0965e [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 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000011 */
"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
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}
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000024
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000025/* Add data to the end of the linked list. */
Rob Landley8bb50782006-05-26 23:44:51 +000026void llist_add_to_end(llist_t **list_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000027{
Rob Landley8bb50782006-05-26 23:44:51 +000028 llist_t *new_item = xmalloc(sizeof(llist_t));
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000029 new_item->data = data;
30 new_item->link = NULL;
31
Rob Landley8bb50782006-05-26 23:44:51 +000032 if (!*list_head) *list_head = new_item;
33 else {
34 llist_t *tail = *list_head;
35 while (tail->link) tail = tail->link;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000036 tail->link = new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000037 }
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000038}
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000039
Rob Landleya6b5b602006-05-08 19:03:07 +000040/* Remove first element from the list and return it */
41void *llist_pop(llist_t **head)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000042{
Rob Landleya6b5b602006-05-08 19:03:07 +000043 void *data;
44
45 if(!*head) data = *head;
46 else {
47 void *next = (*head)->link;
48 data = (*head)->data;
Rob Landleycc848dd2006-05-11 18:25:24 +000049 free(*head);
50 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000051 }
52
53 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000054}
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000055
Rob Landleya6b5b602006-05-08 19:03:07 +000056/* Recursively free all elements in the linked list. If freeit != NULL
57 * call it on each datum in the list */
58void llist_free(llist_t *elm, void (*freeit)(void *data))
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000059{
Rob Landleya6b5b602006-05-08 19:03:07 +000060 while (elm) {
61 void *data = llist_pop(&elm);
62 if (freeit) freeit(data);
63 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000064}
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000065
66/* Reverse list order. Useful since getopt32 saves option params
67 * in reverse order */
68llist_t* rev_llist(llist_t *list)
69{
70 llist_t *new = NULL;
71 while (list) {
72 llist_t *next = list->link;
73 list->link = new;
74 new = list;
75 list = next;
76 }
77 return new;
78}