license statements.
[apps/madmutt.git] / list.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9 #if HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18
19 #include "list.h"
20
21 LIST *mutt_copy_list (LIST * p) {
22   LIST *t, *r = NULL, *l = NULL;
23
24   for (; p; p = p->next) {
25     t = p_new(LIST, 1);
26     t->data = m_strdup(p->data);
27     t->next = NULL;
28     if (l) {
29       r->next = t;
30       r = r->next;
31     }
32     else
33       l = r = t;
34   }
35   return (l);
36 }
37
38
39 LIST *mutt_add_list (LIST * head, const char *data) {
40   size_t len = m_strlen(data);
41   return (mutt_add_list_n (head, data, len ? len + 1 : 0));
42 }
43
44 LIST *mutt_add_list_n (LIST *head, const void *data, size_t len) {
45   LIST *tmp;
46
47   for (tmp = head; tmp && tmp->next; tmp = tmp->next);
48
49   if (tmp) {
50     tmp->next = p_new(LIST, 1);
51     tmp = tmp->next;
52   } else
53     head = tmp = p_new(LIST, 1);
54
55   tmp->data = p_new(char, len);
56   if (len)
57     memcpy (tmp->data, data, len);
58   tmp->next = NULL;
59   return head;
60 }
61
62 void mutt_free_list (LIST ** list) {
63   LIST *p;
64
65   if (!list)
66     return;
67   while (*list) {
68     p = *list;
69     *list = (*list)->next;
70     p_delete(&p->data);
71     p_delete(&p);
72   }
73 }