blob: 706751447e3dffa02ae3a7ae017cce3cf029d5fb [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. */
Denis Vlasenko8d9f4952007-04-08 15:08:42 +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. */
Denis Vlasenko8d9f4952007-04-08 15:08:42 +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 */
Denis Vlasenko8d9f4952007-04-08 15:08:42 +000046void *llist_pop(llist_t **head)
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000047{
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +000048 void *data, *next;
Rob Landleya6b5b602006-05-08 19:03:07 +000049
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000050 if (!*head)
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +000051 return NULL;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000052
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +000053 data = (*head)->data;
54 next = (*head)->link;
55 free(*head);
56 *head = next;
Rob Landleya6b5b602006-05-08 19:03:07 +000057
58 return data;
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000059}
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000060
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +000061/* Unlink arbitrary given element from the list */
62void llist_unlink(llist_t **head, llist_t *elm)
63{
64 llist_t *crt;
65
66 if (!(elm && *head))
67 return;
68
69 if (elm == *head) {
70 *head = (*head)->link;
71 return;
72 }
73
74 for (crt = *head; crt; crt = crt->link) {
75 if (crt->link == elm) {
76 crt->link = elm->link;
77 return;
78 }
79 }
80}
81
Rob Landleya6b5b602006-05-08 19:03:07 +000082/* Recursively free all elements in the linked list. If freeit != NULL
83 * call it on each datum in the list */
Denis Vlasenko8d9f4952007-04-08 15:08:42 +000084void llist_free(llist_t *elm, void (*freeit) (void *data))
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000085{
Rob Landleya6b5b602006-05-08 19:03:07 +000086 while (elm) {
87 void *data = llist_pop(&elm);
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000088
89 if (freeit)
90 freeit(data);
Rob Landleya6b5b602006-05-08 19:03:07 +000091 }
Bernhard Reutner-Fischer56b21712005-10-06 12:10:48 +000092}
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000093
Denis Vlasenko8d9f4952007-04-08 15:08:42 +000094#ifdef UNUSED
95/* Reverse list order. */
96llist_t *llist_rev(llist_t *list)
Denis Vlasenkoc8400a22006-10-25 00:33:44 +000097{
Denis Vlasenko8d9f4952007-04-08 15:08:42 +000098 llist_t *rev = NULL;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +000099
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000100 while (list) {
101 llist_t *next = list->link;
Bernhard Reutner-Fischerd909d232007-02-04 20:32:38 +0000102
Denis Vlasenko8d9f4952007-04-08 15:08:42 +0000103 list->link = rev;
104 rev = list;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000105 list = next;
106 }
Denis Vlasenko8d9f4952007-04-08 15:08:42 +0000107 return rev;
Denis Vlasenkoc8400a22006-10-25 00:33:44 +0000108}
Denis Vlasenko8d9f4952007-04-08 15:08:42 +0000109#endif