summaryrefslogtreecommitdiffstats
path: root/mfpplist.c
blob: 5f36c344f7c1b5aaa642f5408ea522391fbf95f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * mfpplist.c
 *
 * List operations for mfpp
 */

#include <stdlib.h>
#include "mfpp.h"

/*
 * makelist: Converts a single item into a list
 */

list *makelist(char *txt)
{
  list *hdr;

  if ( !val )
    return nulllist();

  hdr = xmalloc(sizeof(list));
  hdr->len = 1;
  hdr->head = hdr->tail = xmalloc(sizeof(struct listelm));
  hdr->head->txt = txt;
  hdr->head->next = NULL;

  return hdr;
}

/*
 * nulllist: Make an empty list
 */
list *nulllist(void)
{
  list *hdr;

  hdr = xmalloc(sizeof(list));
  hdr->len = 0;
  hdr->head = hdr->tail = NULL;
  return hdr;
}

/*
 * listcat: Concatenate two lists (freeing the old lists!)
 */
list *listcat(list *dst, list *src)
{
  dst->len += src->len;

  if ( dst->tail )
    {
      dst->tail->next = src->head;
      if ( src->tail )
	dst->tail = src->tail;
    }
  else
    {
      dst->head = src->head;
      dst->tail = src->tail;
    }

  free(src);

  return dst;
}

/*
 * listdup: Duplicate a list
 */
list *listdup(list *src)
{
  list *dst;
  struct listelm *sp, *dp, **dpp;

  dst = xmalloc(sizeof(list));
  dst->len = src->len;
  if ( !src->head )
    {
      dst->head = dst->tail = NULL;
    }
  else
    {
      dpp = &(dst->head);

      for ( sp = src->head ; sp ; sp = sp->next )
	{
	  dp = xmalloc(sizeof(struct listelm));
	  *dpp = dp;
	  dp->txt = xstrdup(sp->txt);
	  dpp = &(dp->next);
	}
      *dpp = NULL;
      dst->tail = dp;
    }
  
  return dst;
}

/*
 * freelist: Free the entire contents of a list
 */
void freelist(list *junk)
{
  struct listelm *p1, *p2;

  p1 = junk->head;
  while ( p1 )
    {
      p2 = p1;
      p1 = p1->next;
      free(p2);
    }

  free(junk);
}