source: MondoRescue/branches/3.2/mindi-busybox/libbb/llist.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
File size: 1.9 KB
RevLine 
[821]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
[2725]7 * Copyright (C) 2005 Bernhard Reutner-Fischer
[821]8 * Copyright (C) 2006 Rob Landley <rob@landley.net>
9 *
[2725]10 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]11 */
[1765]12
[821]13#include "libbb.h"
14
15/* Add data to the start of the linked list. */
[2725]16void FAST_FUNC llist_add_to(llist_t **old_head, void *data)
[821]17{
18 llist_t *new_head = xmalloc(sizeof(llist_t));
[1765]19
[821]20 new_head->data = data;
21 new_head->link = *old_head;
22 *old_head = new_head;
23}
24
25/* Add data to the end of the linked list. */
[2725]26void FAST_FUNC llist_add_to_end(llist_t **list_head, void *data)
[821]27{
[2725]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;*/
[821]33}
34
35/* Remove first element from the list and return it */
[2725]36void* FAST_FUNC llist_pop(llist_t **head)
[821]37{
[2725]38 void *data = NULL;
39 llist_t *temp = *head;
[821]40
[2725]41 if (temp) {
42 data = temp->data;
43 *head = temp->link;
44 free(temp);
45 }
[821]46 return data;
47}
48
[1765]49/* Unlink arbitrary given element from the list */
[2725]50void FAST_FUNC llist_unlink(llist_t **head, llist_t *elm)
[1765]51{
[2725]52 if (!elm)
[1765]53 return;
[2725]54 while (*head) {
55 if (*head == elm) {
56 *head = (*head)->link;
57 break;
[1765]58 }
[2725]59 head = &(*head)->link;
[1765]60 }
61}
62
[821]63/* Recursively free all elements in the linked list. If freeit != NULL
64 * call it on each datum in the list */
[3232]65void FAST_FUNC llist_free(llist_t *elm, void (*freeit)(void *data))
[821]66{
67 while (elm) {
68 void *data = llist_pop(&elm);
[1765]69
70 if (freeit)
71 freeit(data);
[821]72 }
73}
[1765]74
75/* Reverse list order. */
[2725]76llist_t* FAST_FUNC llist_rev(llist_t *list)
[1765]77{
78 llist_t *rev = NULL;
79
80 while (list) {
81 llist_t *next = list->link;
82
83 list->link = rev;
84 rev = list;
85 list = next;
86 }
87 return rev;
88}
[2725]89
90llist_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}
Note: See TracBrowser for help on using the repository browser.