Revive radsecproxy.h and hostport_types.h and move rsp_* into radsecproxy/.
[libradsec.git] / lib / radsecproxy / list.c
1 /* Copyright (c) 2007-2009, UNINETT AS */
2 /* See LICENSE for licensing information. */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include "list.h"
11
12 /* allocates and initialises list structure; returns NULL if malloc fails */
13 struct list *list_create() {
14     struct list *list = malloc(sizeof(struct list));
15     if (list)
16         memset(list, 0, sizeof(struct list));
17     return list;
18 }
19
20 /* frees all memory associated with the list */
21 void list_destroy(struct list *list) {
22     struct list_node *node, *next;
23
24     if (!list)
25         return;
26
27     for (node = list->first; node; node = next) {
28         free(node->data);
29         next = node->next;
30         free(node);
31     }
32     free(list);
33 }
34
35 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
36 int list_push(struct list *list, void *data) {
37     struct list_node *node;
38
39     node = malloc(sizeof(struct list_node));
40     if (!node)
41         return 0;
42
43     node->next = NULL;
44     node->data = data;
45
46     if (list->first)
47         list->last->next = node;
48     else
49         list->first = node;
50     list->last = node;
51
52     list->count++;
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     list->count--;
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         list->count--;
86         node = list->first;
87         if (!node) {
88             list->last = NULL;
89             return;
90         }
91     }
92     for (; node->next; node = node->next)
93         if (node->next->data == data) {
94             t = node->next;
95             node->next = t->next;
96             free(t);
97             list->count--;
98             if (!node->next) { /* we removed the last one */
99                 list->last = node;
100                 return;
101             }
102         }
103 }
104
105 /* returns first node */
106 struct list_node *list_first(struct list *list) {
107     return list ? list->first : NULL;
108 }
109
110 /* returns the next node after the argument */
111 struct list_node *list_next(struct list_node *node) {
112     return node->next;
113 }
114
115 /* returns number of nodes */
116 uint32_t list_count(struct list *list) {
117     return list->count;
118 }
119
120 /* Local Variables: */
121 /* c-file-style: "stroustrup" */
122 /* End: */