allow multiple servers for a realm, so far only first will be used
[radsecproxy.git] / list.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "list.h"
4
5 /* allocates and initialises list structure; returns NULL if malloc fails */
6 struct list *list_create() {
7     struct list *list = malloc(sizeof(struct list));
8     if (list)
9         memset(list, 0, sizeof(struct list));
10     return list;
11 }
12
13 /* frees all memory associated with the list */
14 void list_destroy(struct list *list) {
15     struct list_node *node, *next;
16     
17     for (node = list->first; node; node = next) {
18         free(node->data);
19         next = node->next;
20         free(node);
21     }
22     free(list);
23 }
24
25 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
26 int list_push(struct list *list, void *data) {
27     struct list_node *node;
28
29     node = malloc(sizeof(struct list_node));
30     if (!node)
31         return 0;
32     
33     node->next = NULL;
34     node->data = data;
35
36     if (list->first)
37         list->last->next = node;
38     else
39         list->first = node;
40     list->last = node;
41     
42     return 1;
43 }
44
45 /* removes first entry from list and returns data */
46 void *list_shift(struct list *list) {
47     struct list_node *node;
48     void *data;
49     
50     if (!list->first)
51         return NULL;
52     
53     node = list->first;
54     list->first = node->next;
55     if (!list->first)
56         list->last = NULL;
57     data = node->data;
58     free(node);
59     
60     return data;
61 }
62
63 /* removes first entry with matching data pointer */
64 void list_removedata(struct list *list, void *data) {
65     struct list_node *node, *t;
66     
67     if (!list->first)
68         return;
69
70     node = list->first;
71     if (node->data == data) {
72         list->first = node->next;
73         if (!list->first)
74             list->last = NULL;
75         free(node);
76         return;
77     }
78     for (; node->next; node = node->next)
79         if (node->next->data == data) {
80             t = node->next;
81             node->next = node->next->next;
82             if (!node->next) /* we removed the last one */
83                 list->last = node;
84             free(t);
85             return;
86         }
87 }
88
89 /* returns first node */
90 struct list_node *list_first(struct list *list) {
91     return list ? list->first : NULL;
92 }
93
94 /* returns the next node after the argument */
95 struct list_node *list_next(struct list_node *node) {
96     return node->next;
97 }