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 | * |
| 10 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
| 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 | |
| 16 | #ifdef L_llist_add_to |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 17 | /* Add data to the start of the linked list. */ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 18 | void llist_add_to(llist_t **old_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 19 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 20 | llist_t *new_head = xmalloc(sizeof(llist_t)); |
Rob Landley | 5edc102 | 2006-05-26 23:00:10 +0000 | [diff] [blame] | 21 | new_head->data = data; |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 22 | new_head->link = *old_head; |
| 23 | *old_head = new_head; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 24 | } |
| 25 | #endif |
| 26 | |
| 27 | #ifdef L_llist_add_to_end |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 28 | /* Add data to the end of the linked list. */ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 29 | void llist_add_to_end(llist_t **list_head, void *data) |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 30 | { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 31 | llist_t *new_item = xmalloc(sizeof(llist_t)); |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 32 | new_item->data = data; |
| 33 | new_item->link = NULL; |
| 34 | |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 35 | if (!*list_head) *list_head = new_item; |
| 36 | else { |
| 37 | llist_t *tail = *list_head; |
| 38 | while (tail->link) tail = tail->link; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 39 | tail->link = new_item; |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 40 | } |
Bernhard Reutner-Fischer | bee9eb1 | 2005-09-29 12:55:10 +0000 | [diff] [blame] | 41 | } |
| 42 | #endif |
| 43 | |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 44 | #ifdef L_llist_pop |
| 45 | /* Remove first element from the list and return it */ |
| 46 | void *llist_pop(llist_t **head) |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 47 | { |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 48 | void *data; |
| 49 | |
| 50 | if(!*head) data = *head; |
| 51 | else { |
| 52 | void *next = (*head)->link; |
| 53 | data = (*head)->data; |
Rob Landley | cc848dd | 2006-05-11 18:25:24 +0000 | [diff] [blame] | 54 | free(*head); |
| 55 | *head = next; |
Rob Landley | a6b5b60 | 2006-05-08 19:03:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return data; |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #ifdef L_llist_free |
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 */ |
| 65 | void 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); |
| 69 | if (freeit) freeit(data); |
| 70 | } |
Bernhard Reutner-Fischer | 56b2171 | 2005-10-06 12:10:48 +0000 | [diff] [blame] | 71 | } |
| 72 | #endif |