Always distribute radsecproxy-hash.1.
[libradsec.git] / 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 #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     list->count++;
54     return 1;
55 }
56
57 /* removes first entry from list and returns data */
58 void *list_shift(struct list *list) {
59     struct list_node *node;
60     void *data;
61
62     if (!list || !list->first)
63         return NULL;
64
65     node = list->first;
66     list->first = node->next;
67     if (!list->first)
68         list->last = NULL;
69     data = node->data;
70     free(node);
71     list->count--;
72     return data;
73 }
74
75 /* removes all entries with matching data pointer */
76 void list_removedata(struct list *list, void *data) {
77     struct list_node *node, *t;
78
79     if (!list || !list->first)
80         return;
81
82     node = list->first;
83     while (node->data == data) {
84         list->first = node->next;
85         free(node);
86         list->count--;
87         node = list->first;
88         if (!node) {
89             list->last = NULL;
90             return;
91         }
92     }
93     for (; node->next; node = node->next)
94         if (node->next->data == data) {
95             t = node->next;
96             node->next = t->next;
97             free(t);
98             list->count--;
99             if (!node->next) { /* we removed the last one */
100                 list->last = node;
101                 return;
102             }
103         }
104 }
105
106 /* returns first node */
107 struct list_node *list_first(struct list *list) {
108     return list ? list->first : NULL;
109 }
110
111 /* returns the next node after the argument */
112 struct list_node *list_next(struct list_node *node) {
113     return node->next;
114 }
115
116 /* returns number of nodes */
117 uint32_t list_count(struct list *list) {
118     return list->count;
119 }
120
121 /* Local Variables: */
122 /* c-file-style: "stroustrup" */
123 /* End: */