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 |
| 8 | * |
| 9 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
| 10 | */ |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
| 13 | |
| 14 | #ifdef L_llist_add_to |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 15 | /* Add data to the start of the linked list. */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 16 | llist_t *llist_add_to(llist_t *old_head, char *new_item) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 17 | { |
| 18 | llist_t *new_head; |
| 19 | |
| 20 | new_head = xmalloc(sizeof(llist_t)); |
| 21 | new_head->data = new_item; |
| 22 | new_head->link = old_head; |
| 23 | |
| 24 | return (new_head); |
| 25 | } |
| 26 | #endif |
| 27 | |
| 28 | #ifdef L_llist_add_to_end |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 29 | /* Add data to the end of the linked list. */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 30 | llist_t *llist_add_to_end(llist_t *list_head, char *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 31 | { |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 32 | llist_t *new_item; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 33 | |
| 34 | new_item = xmalloc(sizeof(llist_t)); |
| 35 | new_item->data = data; |
| 36 | new_item->link = NULL; |
| 37 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 38 | if (list_head == NULL) { |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 39 | list_head = new_item; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 40 | } else { |
| 41 | llist_t *tail = list_head; |
| 42 | while (tail->link) |
| 43 | tail = tail->link; |
| 44 | tail->link = new_item; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 45 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 46 | return list_head; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 47 | } |
| 48 | #endif |
| 49 | |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 50 | #ifdef L_llist_free_one |
| 51 | /* Free the current list element and advance to the next entry in the list. |
| 52 | * Returns a pointer to the next element. */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 53 | llist_t *llist_free_one(llist_t *elm) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 54 | { |
| 55 | llist_t *next = elm ? elm->link : NULL; |
Rob Landley | ea9a471 | 2006-03-16 14:40:27 +0000 | [diff] [blame] | 56 | free(elm); |
| 57 | return next; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 58 | } |
| 59 | #endif |
| 60 | |
| 61 | #ifdef L_llist_free |
| 62 | /* Recursively free all elements in the linked list. */ |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 63 | void llist_free(llist_t *elm) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 64 | { |
| 65 | while ((elm = llist_free_one(elm))); |
| 66 | } |
| 67 | #endif |