Use getcwd() instead of get_current_dir_name().
[tinc] / src / list.c
1 /*
2     list.c -- functions to deal with double linked lists
3     Copyright (C) 2000-2005 Ivo Timmermans
4                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "list.h"
24 #include "xalloc.h"
25
26 /* (De)constructors */
27
28 list_t *list_alloc(list_action_t delete) {
29         list_t *list;
30
31         list = xmalloc_and_zero(sizeof(list_t));
32         list->delete = delete;
33
34         return list;
35 }
36
37 void list_free(list_t *list) {
38         free(list);
39 }
40
41 list_node_t *list_alloc_node(void) {
42         return xmalloc_and_zero(sizeof(list_node_t));
43 }
44
45 void list_free_node(list_t *list, list_node_t *node) {
46         if(node->data && list->delete)
47                 list->delete(node->data);
48
49         free(node);
50 }
51
52 /* Insertion and deletion */
53
54 list_node_t *list_insert_head(list_t *list, void *data) {
55         list_node_t *node;
56
57         node = list_alloc_node();
58
59         node->data = data;
60         node->prev = NULL;
61         node->next = list->head;
62         list->head = node;
63
64         if(node->next)
65                 node->next->prev = node;
66         else
67                 list->tail = node;
68
69         list->count++;
70
71         return node;
72 }
73
74 list_node_t *list_insert_tail(list_t *list, void *data) {
75         list_node_t *node;
76
77         node = list_alloc_node();
78
79         node->data = data;
80         node->next = NULL;
81         node->prev = list->tail;
82         list->tail = node;
83
84         if(node->prev)
85                 node->prev->next = node;
86         else
87                 list->head = node;
88
89         list->count++;
90
91         return node;
92 }
93
94 void list_unlink_node(list_t *list, list_node_t *node) {
95         if(node->prev)
96                 node->prev->next = node->next;
97         else
98                 list->head = node->next;
99
100         if(node->next)
101                 node->next->prev = node->prev;
102         else
103                 list->tail = node->prev;
104
105         list->count--;
106 }
107
108 void list_delete_node(list_t *list, list_node_t *node) {
109         list_unlink_node(list, node);
110         list_free_node(list, node);
111 }
112
113 void list_delete_head(list_t *list) {
114         list_delete_node(list, list->head);
115 }
116
117 void list_delete_tail(list_t *list) {
118         list_delete_node(list, list->tail);
119 }
120
121 /* Head/tail lookup */
122
123 void *list_get_head(list_t *list) {
124         if(list->head)
125                 return list->head->data;
126         else
127                 return NULL;
128 }
129
130 void *list_get_tail(list_t *list) {
131         if(list->tail)
132                 return list->tail->data;
133         else
134                 return NULL;
135 }
136
137 /* Fast list deletion */
138
139 void list_delete_list(list_t *list) {
140         list_node_t *node, *next;
141
142         for(node = list->head; node; node = next) {
143                 next = node->next;
144                 list_free_node(list, node);
145         }
146
147         list_free(list);
148 }
149
150 /* Traversing */
151
152 void list_foreach_node(list_t *list, list_action_node_t action) {
153         list_node_t *node, *next;
154
155         for(node = list->head; node; node = next) {
156                 next = node->next;
157                 action(node);
158         }
159 }
160
161 void list_foreach(list_t *list, list_action_t action) {
162         list_node_t *node, *next;
163
164         for(node = list->head; node; node = next) {
165                 next = node->next;
166                 if(node->data)
167                         action(node->data);
168         }
169 }