1 /*
2  * list.c
3  *
4  * This file is part of msmtp, an SMTP client, and of mpop, a POP3 client.
5  *
6  * Copyright (C) 2000, 2003, 2004, 2005, 2006, 2007
7  * Martin Lambers <marlam@marlam.de>
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 3 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 
27 #include <stdlib.h>
28 
29 #include "xalloc.h"
30 #include "list.h"
31 
32 
33 /*
34  * list_new()
35  */
36 
list_new(void)37 list_t *list_new(void)
38 {
39     list_t *head, *foot;
40 
41     head = xmalloc(sizeof(list_t));
42     foot = xmalloc(sizeof(list_t));
43     head->next = foot;
44     head->data = NULL;
45     foot->next = foot;
46     foot->data = NULL;
47     return head;
48 }
49 
50 
51 /*
52  * list_free()
53  */
54 
list_free(list_t * l)55 void list_free(list_t *l)
56 {
57     list_t *p;
58 
59     while (l->next != l)
60     {
61         p = l;
62         l = l->next;
63         free(p);
64     }
65     free(l);
66 }
67 
68 
69 /*
70  * list_xfree()
71  */
72 
list_xfree(list_t * l,void (* destruct)(void *))73 void list_xfree(list_t *l, void (*destruct)(void *))
74 {
75     list_t *p;
76 
77     while (l->next != l)
78     {
79         p = l;
80         l = l->next;
81         destruct(p->data);
82         free(p);
83     }
84     free(l);
85 }
86 
87 
88 /*
89  * list_insert()
90  */
91 
list_insert(list_t * l,void * data)92 void list_insert(list_t *l, void *data)
93 {
94     list_t *t;
95 
96     t = xmalloc(sizeof(list_t));
97     t->data = data;
98     t->next = l->next;
99     l->next = t;
100 }
101 
102 
103 /*
104  * list_remove()
105  */
106 
list_remove(list_t * l)107 void list_remove(list_t *l)
108 {
109     list_t *p;
110 
111     p = l->next;
112     l->next = l->next->next;
113     free(p);
114 }
115 
116 
117 /*
118  * list_xremove()
119  */
120 
list_xremove(list_t * l,void (* destruct)(void *))121 void list_xremove(list_t *l, void (*destruct)(void *))
122 {
123     list_t *p;
124 
125     p = l->next;
126     l->next = l->next->next;
127     destruct(p->data);
128     free(p);
129 }
130 
131 
132 /*
133  * list_is_empty()
134  */
135 
list_is_empty(list_t * l)136 int list_is_empty(list_t *l)
137 {
138     return (l->next->next == l->next);
139 }
140 
141 
142 /*
143  * list_last()
144  */
145 
list_last(list_t * e)146 list_t *list_last(list_t *e)
147 {
148     while (!(list_is_empty(e)))
149     {
150         e = e->next;
151     }
152     return e;
153 }
154