b6cfe33f9ba903eeee037c8b1f157b26f845e66f
[radsecproxy.git] / lib / rsp_list.c
1 /*
2  * Copyright (C) 2006-2009 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 #if defined HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include "rsp_list.h"
16
17 /* allocates and initialises list structure; returns NULL if malloc fails */
18 struct list *list_create() {
19     struct list *list = malloc(sizeof(struct list));
20     if (list)
21         memset(list, 0, sizeof(struct list));
22     return list;
23 }
24
25 /* frees all memory associated with the list */
26 void list_destroy(struct list *list) {
27     struct list_node *node, *next;
28
29     if (!list)
30         return;
31
32     for (node = list->first; node; node = next) {
33         free(node->data);
34         next = node->next;
35         free(node);
36     }
37     free(list);
38 }
39
40 /* appends entry to list; returns 1 if ok, 0 if malloc fails */
41 int list_push(struct list *list, void *data) {
42     struct list_node *node;
43
44     node = malloc(sizeof(struct list_node));
45     if (!node)
46         return 0;
47
48     node->next = NULL;
49     node->data = data;
50
51     if (list->first)
52         list->last->next = node;
53     else
54         list->first = node;
55     list->last = node;
56
57     list->count++;
58     return 1;
59 }
60
61 /* removes first entry from list and returns data */
62 void *list_shift(struct list *list) {
63     struct list_node *node;
64     void *data;
65
66     if (!list || !list->first)
67         return NULL;
68
69     node = list->first;
70     list->first = node->next;
71     if (!list->first)
72         list->last = NULL;
73     data = node->data;
74     free(node);
75     list->count--;
76     return data;
77 }
78
79 /* removes all entries with matching data pointer */
80 void list_removedata(struct list *list, void *data) {
81     struct list_node *node, *t;
82
83     if (!list || !list->first)
84         return;
85
86     node = list->first;
87     while (node->data == data) {
88         list->first = node->next;
89         free(node);
90         list->count--;
91         node = list->first;
92         if (!node) {
93             list->last = NULL;
94             return;
95         }
96     }
97     for (; node->next; node = node->next)
98         if (node->next->data == data) {
99             t = node->next;
100             node->next = t->next;
101             free(t);
102             list->count--;
103             if (!node->next) { /* we removed the last one */
104                 list->last = node;
105                 return;
106             }
107         }
108 }
109
110 /* returns first node */
111 struct list_node *list_first(struct list *list) {
112     return list ? list->first : NULL;
113 }
114
115 /* returns the next node after the argument */
116 struct list_node *list_next(struct list_node *node) {
117     return node->next;
118 }
119
120 /* returns number of nodes */
121 uint32_t list_count(struct list *list) {
122     return list->count;
123 }
124
125 /* Local Variables: */
126 /* c-file-style: "stroustrup" */
127 /* End: */