blob: 0a5978a26905a1a1435e17bbd92f1779b6dfd487 [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. */
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +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));
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000020
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}
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000025
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000026/* Add data to the end of the linked list. */
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000027void llist_add_to_end(llist_t ** list_head, void *data)
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000028{
Rob Landley8bb50782006-05-26 23:44:51 +000029 llist_t *new_item = xmalloc(sizeof(llist_t));
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000030
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000031 new_item->data = data;
32 new_item->link = NULL;
33
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000034 if (!*list_head)
35 *list_head = new_item;
Rob Landley8bb50782006-05-26 23:44:51 +000036 else {
37 llist_t *tail = *list_head;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000038
39 while (tail->link)
40 tail = tail->link;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000041 tail->link = new_item;
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000042 }
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000043}
Bernhard Reutner-Fischerbee9eb12005-09-29 12:55:10 +000044
Rob Landleya6b5b602006-05-08 19:03:07 +000045/* Remove first element from the list and return it */
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000046void *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
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000050 if (!*head)
51 data = *head;
Rob Landleya6b5b602006-05-08 19:03:07 +000052 else {
53 void *next = (*head)->link;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000054
Rob Landleya6b5b602006-05-08 19:03:07 +000055 data = (*head)->data;
Rob Landleycc848dd2006-05-11 18:25:24 +000056 free(*head);
57 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000058 }
59
60 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000061}
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000062
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 */
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000065void 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);
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000069
70 if (freeit)
71 freeit(data);
Rob Landleya6b5b602006-05-08 19:03:07 +000072 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000073}
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000074
75/* Reverse list order. Useful since getopt32 saves option params
76 * in reverse order */
Denis Vlasenko54cf5112007-02-17 18:11:45 +000077llist_t *llist_rev(llist_t * list)
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000078{
79 llist_t *new = NULL;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000080
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000081 while (list) {
82 llist_t *next = list->link;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000083
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000084 list->link = new;
85 new = list;
86 list = next;
87 }
88 return new;
89}