opkg: fix nullpointer dereference
[oweals/opkg-lede.git] / libopkg / void_list.c
1 /* void_list.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include <errno.h>
20
21 #include "void_list.h"
22
23 int void_list_elt_init(void_list_elt_t *elt, void *data)
24 {
25     INIT_LIST_HEAD(&elt->node);
26     elt->data = data;
27
28     return 0;
29 }
30
31 void_list_elt_t * void_list_elt_new (void *data) {
32     void_list_elt_t *elt;
33     /* freed in void_list_elt_deinit */
34     elt = calloc(1, sizeof(void_list_elt_t));
35     if (elt == NULL) {
36         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
37         return NULL;
38     }
39     void_list_elt_init(elt, data);
40     return elt;
41 }
42
43 void void_list_elt_deinit(void_list_elt_t *elt)
44 {
45     list_del_init(&elt->node);
46     void_list_elt_init(elt, NULL);
47     free(elt);
48 }
49
50 int void_list_init(void_list_t *list)
51 {
52     INIT_LIST_HEAD(&list->head);
53     return 0;
54 }
55
56 void void_list_deinit(void_list_t *list)
57 {
58     void_list_elt_t *elt;
59
60     while (!void_list_empty(list)) {
61         elt = void_list_pop(list);
62         void_list_elt_deinit(elt);
63     }
64     INIT_LIST_HEAD(&list->head);
65 }
66
67 int void_list_append(void_list_t *list, void *data)
68 {
69     void_list_elt_t *elt = void_list_elt_new(data);
70      
71     list_add_tail(&elt->node, &list->head);
72
73     return 0;
74 }
75
76 int void_list_push(void_list_t *list, void *data)
77 {
78     void_list_elt_t *elt = void_list_elt_new(data);
79
80     list_add(&elt->node, &list->head);
81
82     return 0;
83 }
84
85 void_list_elt_t *void_list_pop(void_list_t *list)
86 {
87     struct list_head *node;
88
89     if (void_list_empty(list)) 
90         return NULL;
91     node = list->head.next;
92     list_del_init(node);
93     return list_entry(node, void_list_elt_t, node);
94 }
95
96 void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
97 {
98     void_list_elt_t *pos, *n;
99     void_list_elt_t *old_elt;
100     void *old_data = NULL;
101
102     old_elt = *iter;
103     if (!old_elt)
104         return old_data;
105     old_data = old_elt->data;
106
107     list_for_each_entry_safe(pos, n, &list->head, node) {
108         if (pos == old_elt)
109             break;
110     }
111     if ( pos != old_elt) {
112         fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
113         return NULL;
114     }
115
116     *iter = list_entry(pos->node.prev, void_list_elt_t, node);
117     void_list_elt_deinit(pos);
118
119     return old_data;
120 }
121
122 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
123 void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
124 {
125     void_list_elt_t *pos, *n;
126     void *old_data = NULL;
127     list_for_each_entry_safe(pos, n, &list->head, node) {
128         if ( pos->data && cmp(pos->data, target_data)==0 ){
129             old_data = pos->data;
130             void_list_elt_deinit(pos);
131             break;
132         }
133     }
134     return old_data;
135 }
136
137 void_list_elt_t *void_list_first(void_list_t *list) {
138     struct list_head *elt;
139     if (!list)
140         return NULL;
141     elt = list->head.next;
142     if (elt == &list->head ) {
143         return NULL;
144     }
145     return list_entry(elt, void_list_elt_t, node);
146 }
147
148 void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) {
149     struct list_head *elt;
150     if (!list || !node)
151         return NULL;
152     elt = node->node.prev;
153     if (elt == &list->head ) {
154         return NULL;
155     }
156     return list_entry(elt, void_list_elt_t, node);
157 }
158
159 void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) {
160     struct list_head *elt;
161     if (!list || !node)
162         return NULL;
163     elt = node->node.next;
164     if (elt == &list->head ) {
165         return NULL;
166     }
167     return list_entry(elt, void_list_elt_t, node);
168 }
169
170 void_list_elt_t *void_list_last(void_list_t *list) {
171     struct list_head *elt;
172     if (!list)
173         return NULL;
174     elt = list->head.prev;
175     if (elt == &list->head ) {
176         return NULL;
177     }
178     return list_entry(elt, void_list_elt_t, node);
179 }
180