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 |
| 7 | * Copyright (C) 2005 Bernhard 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 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 10 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
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 <stdlib.h> |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
| 15 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 16 | /* Add data to the start of the linked list. */ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 17 | void llist_add_to(llist_t **old_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 18 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 19 | llist_t *new_head = xmalloc(sizeof(llist_t)); |
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. */ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 26 | void llist_add_to_end(llist_t **list_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 27 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 28 | llist_t *new_item = xmalloc(sizeof(llist_t)); |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 29 | new_item->data = data; |
| 30 | new_item->link = NULL; |
| 31 | |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 32 | if (!*list_head) *list_head = new_item; |
| 33 | else { |
| 34 | llist_t *tail = *list_head; |
| 35 | while (tail->link) tail = tail->link; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 36 | tail->link = new_item; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 37 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 38 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 39 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 40 | /* Remove first element from the list and return it */ |
| 41 | void *llist_pop(llist_t **head) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 42 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 43 | void *data; |
| 44 | |
| 45 | if(!*head) data = *head; |
| 46 | else { |
| 47 | void *next = (*head)->link; |
| 48 | data = (*head)->data; |
Rob Landley | cc848dd | 2006-05-11 18:25:24 +0000 | [diff] [blame] | 49 | free(*head); |
| 50 | *head = next; |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return data; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 54 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 55 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 56 | /* Recursively free all elements in the linked list. If freeit != NULL |
| 57 | * call it on each datum in the list */ |
| 58 | void llist_free(llist_t *elm, void (*freeit)(void *data)) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 59 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 60 | while (elm) { |
| 61 | void *data = llist_pop(&elm); |
| 62 | if (freeit) freeit(data); |
| 63 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 64 | } |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 65 | |
| 66 | /* Reverse list order. Useful since getopt32 saves option params |
| 67 | * in reverse order */ |
| 68 | llist_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 | } |