Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 1 | /* 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 |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005 Bernhard Reutner-Fischer |
Rob Landley | cc848dd | 2006-05-11 18:25:24 +0000 | [diff] [blame] | 8 | * Copyright (C) 2006 Rob Landley <rob@landley.net> |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 11 | */ |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 12 | |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
| 14 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 15 | /* Add data to the start of the linked list. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 16 | void FAST_FUNC llist_add_to(llist_t **old_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 17 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 18 | llist_t *new_head = xmalloc(sizeof(llist_t)); |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 19 | |
Rob Landley | 5edc102 | 2006-05-26 23:00:10 +0000 | [diff] [blame] | 20 | new_head->data = data; |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 21 | new_head->link = *old_head; |
| 22 | *old_head = new_head; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 23 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 25 | /* Add data to the end of the linked list. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 26 | void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 27 | { |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 28 | while (*list_head) |
| 29 | list_head = &(*list_head)->link; |
| 30 | *list_head = xzalloc(sizeof(llist_t)); |
| 31 | (*list_head)->data = data; |
| 32 | /*(*list_head)->link = NULL;*/ |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 33 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 34 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 35 | /* Remove first element from the list and return it */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 36 | void* FAST_FUNC llist_pop(llist_t **head) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 37 | { |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 38 | void *data = NULL; |
| 39 | llist_t *temp = *head; |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 41 | if (temp) { |
| 42 | data = temp->data; |
| 43 | *head = temp->link; |
| 44 | free(temp); |
| 45 | } |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 46 | return data; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 47 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 49 | /* Unlink arbitrary given element from the list */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 50 | void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm) |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 51 | { |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 52 | if (!elm) |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 53 | return; |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 54 | while (*head) { |
| 55 | if (*head == elm) { |
| 56 | *head = (*head)->link; |
| 57 | break; |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 58 | } |
Denis Vlasenko | 1265df1 | 2009-01-13 15:22:50 +0000 | [diff] [blame] | 59 | head = &(*head)->link; |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 63 | /* Recursively free all elements in the linked list. If freeit != NULL |
| 64 | * call it on each datum in the list */ |
Denys Vlasenko | 9be4702 | 2011-05-16 12:21:31 +0200 | [diff] [blame] | 65 | void FAST_FUNC llist_free(llist_t *elm, void (*freeit)(void *data)) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 66 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 67 | while (elm) { |
| 68 | void *data = llist_pop(&elm); |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 69 | |
| 70 | if (freeit) |
| 71 | freeit(data); |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 72 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 73 | } |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 75 | /* Reverse list order. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 76 | llist_t* FAST_FUNC llist_rev(llist_t *list) |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 77 | { |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 78 | llist_t *rev = NULL; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 79 | |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 80 | while (list) { |
| 81 | llist_t *next = list->link; |
Bernhard Reutner-Fischer | d909d23 | 2007-02-04 20:32:38 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 83 | list->link = rev; |
| 84 | rev = list; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 85 | list = next; |
| 86 | } |
Denis Vlasenko | 8d9f495 | 2007-04-08 15:08:42 +0000 | [diff] [blame] | 87 | return rev; |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 88 | } |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 89 | |
| 90 | llist_t* FAST_FUNC llist_find_str(llist_t *list, const char *str) |
| 91 | { |
| 92 | while (list) { |
| 93 | if (strcmp(list->data, str) == 0) |
| 94 | break; |
| 95 | list = list->link; |
| 96 | } |
| 97 | return list; |
| 98 | } |