minor fix in list.c
[libradsec.git] / list.c
1 /*
2  * Copyright (C) 2006-2008 Stig Venaas <venaas@uninett.no>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include "list.h"
12
13 /* allocates and initialises list structure; returns NULL if malloc fails */
14 struct list *list_create() {
15     struct list *list = malloc(sizeof(struct list));
16     if (list)
17         memset(list, 0, sizeof(struct list));
18     return list;
19 }
20
21 /* frees all memory associated with the list */
22 void list_destroy(struct list *list) {
23     struct list_node *node, *next;
24
25     if (!list)
26         return;
27     
28     for (node = list->first; node; node = next) {
29         free(node->data);
30         next = node->next;
31         free(node);
32     }
33     free(list);
34 }
35
36 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
37 int list_push(struct list *list, void *data) {
38     struct list_node *node;
39
40     node = malloc(sizeof(struct list_node));
41     if (!node)
42         return 0;
43     
44     node->next = NULL;
45     node->data = data;
46
47     if (list->first)
48         list->last->next = node;
49     else
50         list->first = node;
51     list->last = node;
52     
53     return 1;
54 }
55
56 /* removes first entry from list and returns data */
57 void *list_shift(struct list *list) {
58     struct list_node *node;
59     void *data;
60     
61     if (!list || !list->first)
62         return NULL;
63     
64     node = list->first;
65     list->first = node->next;
66     if (!list->first)
67         list->last = NULL;
68     data = node->data;
69     free(node);
70     
71     return data;
72 }
73
74 /* removes all entries with matching data pointer */
75 void list_removedata(struct list *list, void *data) {
76     struct list_node *node, *t;
77     
78     if (!list || !list->first)
79         return;
80
81     node = list->first;
82     while (node->data == data) {
83         list->first = node->next;
84         free(node);
85         node = list->first;
86         if (!node) {
87             list->last = NULL;
88             return;
89         }
90     }
91     for (; node->next; node = node->next)
92         if (node->next->data == data) {
93             t = node->next;
94             node->next = t->next;
95             free(t);
96             if (!node->next) { /* we removed the last one */
97                 list->last = node;
98                 return;
99             }
100         }
101 }
102
103 /* returns first node */
104 struct list_node *list_first(struct list *list) {
105     return list ? list->first : NULL;
106 }
107
108 /* returns the next node after the argument */
109 struct list_node *list_next(struct list_node *node) {
110     return node->next;
111 }