Rocco Rutte:
[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 "list.h"
17 #include "lib/mem.h"
18 #include "lib/str.h"
19
20 LIST *mutt_copy_list (LIST * p) {
21   LIST *t, *r = NULL, *l = NULL;
22
23   for (; p; p = p->next) {
24     t = (LIST *) mem_malloc (sizeof (LIST));
25     t->data = str_dup (p->data);
26     t->next = NULL;
27     if (l) {
28       r->next = t;
29       r = r->next;
30     }
31     else
32       l = r = t;
33   }
34   return (l);
35 }
36
37
38 LIST *mutt_add_list (LIST * head, const char *data) {
39   size_t len = str_len (data);
40   return (mutt_add_list_n (head, data, len ? len + 1 : 0));
41 }
42
43 LIST *mutt_add_list_n (LIST *head, const void *data, size_t len) {
44   LIST *tmp;
45
46   for (tmp = head; tmp && tmp->next; tmp = tmp->next);
47
48   if (tmp) {
49     tmp->next = mem_malloc (sizeof (LIST));
50     tmp = tmp->next;
51   } else
52     head = tmp = mem_malloc (sizeof (LIST));
53
54   tmp->data = mem_malloc (len);
55   if (len)
56     memcpy (tmp->data, data, len);
57   tmp->next = NULL;
58   return head;
59 }
60
61 void mutt_free_list (LIST ** list) {
62   LIST *p;
63
64   if (!list)
65     return;
66   while (*list) {
67     p = *list;
68     *list = (*list)->next;
69     mem_free (&p->data);
70     mem_free (&p);
71   }
72 }