1 /*
2 ** LIST: Simple doubly-linked list implementation.
3 ** Copyright (C) 2002 Michael W. Shaffer <mwshaffer@angrypot.com>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program (see the file COPYING). If not, write to:
17 **
18 ** The Free Software Foundation, Inc.
19 ** 59 Temple Place, Suite 330,
20 ** Boston, MA 02111-1307 USA
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "list.h"
26
list_init(struct list * list)27 void list_init (struct list *list)
28 {
29 list->head = NULL;
30 return;
31 }
32
list_free(struct list * list)33 void list_free (struct list *list)
34 {
35 struct list_item *next = NULL;
36 struct list_item *curr = NULL;
37
38 if (!list)
39 return;
40
41 if (!list->head)
42 goto EXIT;
43
44 for (curr = list->head ; curr ; curr = next) {
45 if (curr->data)
46 free (curr->data);
47 next = curr->next;
48 free (curr);
49 }
50
51 EXIT:
52 list->head = NULL;
53 return;
54 }
55
list_insert(struct list * list,void * data,long size)56 struct list_item *list_insert (struct list *list, void *data, long size)
57 {
58 struct list_item *new = NULL;
59
60 if (!(list && data && (size > 0)))
61 goto ERROR;
62
63 new = (struct list_item *) malloc (sizeof (struct list_item));
64 if (!new)
65 goto ERROR;
66 memset (new, 0, sizeof (struct list_item));
67
68 new->size = size;
69 new->data = (void *) malloc (size + 1);
70 if (!new->data)
71 goto ERROR;
72 memset (new->data, 0, (size + 1));
73 memcpy (new->data, data, size);
74
75 if (list->head)
76 list->head->prev = new;
77 new->next = list->head;
78 list->head = new;
79
80 new->list = list;
81
82 goto EXIT;
83
84 ERROR:
85 if (new && new->data)
86 free (new->data);
87 if (new)
88 free (new);
89 new = NULL;
90 EXIT:
91 return new;
92 }
93
list_search(struct list * list,void * data,long size)94 struct list_item *list_search (struct list *list, void *data, long size)
95 {
96 struct list_item *curr = NULL;
97
98 if (!(list && data && (size > 0)))
99 return NULL;
100
101 for (curr = list->head ; curr ; curr = curr->next) {
102 if (curr->size == size) {
103 if (!memcmp (curr->data, data, size))
104 return curr;
105 }
106 }
107 return NULL;
108 }
109
list_delete(struct list_item * item)110 void list_delete (struct list_item *item)
111 {
112 if (!(item && item->list))
113 return;
114
115 if (item->next) {
116 item->next->prev = item->prev;
117 } else {
118 if (item->list->head == item)
119 item->list->head = NULL;
120 }
121
122 if (item->prev) {
123 item->prev->next = item->next;
124 } else {
125 item->list->head = item->next;
126 }
127
128 free (item->data);
129 free (item);
130
131 return;
132 }
133
134