- s/tap/tun/
[l2tpns.git] / ll.c
1 // L2TPNS Linked List Stuff
2
3 char const *cvs_id_ll = "$Id: ll.c,v 1.4 2004/06/28 02:43:13 fred_nerk Exp $";
4
5 #include <stdio.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <malloc.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <time.h>
12 #include <arpa/inet.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include "ll.h"
16
17 linked_list *ll_init()
18 {
19 return (linked_list *)calloc(sizeof(linked_list), 1);
20 }
21
22 void ll_done(linked_list *l)
23 {
24 li *i = l->head, *n;
25
26 while (i)
27 {
28 n = i->next;
29 free(i);
30 i = n;
31 }
32
33 free(l);
34 }
35
36 li *ll_push(linked_list *l, void *data)
37 {
38 li *i;
39
40 if (!l) return NULL;
41 if (!(i = (li *)calloc(sizeof(li), 1))) return NULL;
42
43 i->data = data;
44 i->next = NULL;
45 if (l->end)
46 l->end->next = i;
47 else
48 l->head = i;
49 l->end = i;
50
51 return i;
52 }
53
54 void *ll_pop(linked_list *l)
55 {
56 li *i;
57 void *data;
58
59 if (!l) return NULL;
60 if (!l->head)
61 return NULL;
62
63 data = l->head->data;
64 i = l->head->next;
65 free(l->head);
66 l->head = i;
67 return data;
68 }
69
70 void ll_iterate(linked_list *l, int(*func)(void *))
71 {
72 li *i;
73 if (!l || !func) return;
74
75 for (i = l->head; i; i = i->next)
76 {
77 if (i->data)
78 if (!func(i))
79 break;
80 }
81 }
82
83 void ll_reset(linked_list *l)
84 {
85 if (!l) return;
86 l->current = NULL;
87 }
88
89 void *ll_next(linked_list *l)
90 {
91 if (!l) return NULL;
92 if (!l->current)
93 l->current = l->head;
94 else
95 l->current = l->current->next;
96 if (!l->current)
97 return NULL;
98 return l->current->data;
99 }
100
101 void ll_delete(linked_list *l, void *data)
102 {
103 li *i = l->head, *p = NULL;
104
105 while (i)
106 {
107 if (i->data == data)
108 {
109 if (l->head == i) l->head = i->next;
110 if (l->end == i) l->end = i->next;
111 if (p) p->next = i->next;
112 free(i);
113 l->current = NULL;
114 return;
115 }
116 p = i;
117 i = i->next;
118 }
119 }
120
121 int ll_size(linked_list *l)
122 {
123 int count = 0;
124 li *i;
125
126 if (!l) return 0;
127
128 for (i = l->head; i; i = i->next)
129 if (i->data) count++;
130
131 return count;
132 }
133
134 int ll_contains(linked_list *l, void *search)
135 {
136 li *i;
137 for (i = l->head; i; i = i->next)
138 if (i->data == search)
139 return 1;
140 return 0;
141 }
142