-
Notifications
You must be signed in to change notification settings - Fork 24
/
list.c
57 lines (47 loc) · 1.13 KB
/
list.c
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
/* list.c: routines for manipulating the List type */
#include "rc.h"
/*
These list routines assign meta values of null to the resulting lists;
it is impossible to glob with the value of a variable unless this value
is rescanned with eval---therefore it is safe to throw away the meta-ness
of the list.
*/
/* free a list from malloc space */
extern void listfree(List *p) {
while (p != NULL) {
List *n = p->n;
efree(p->w);
efree(p);
p = n;
}
}
/* Copy list into malloc space (for storing a variable) */
extern List *listcpy(List *s, void *(*alloc)(size_t)) {
List *top, *r;
for (top = r = NULL; s != NULL; s = s->n) {
if (top == NULL)
r = top = (*alloc)(sizeof (List));
else
r = r->n = (*alloc)(sizeof (List));
r->w = (*alloc)(strlen(s->w) + 1);
strcpy(r->w, s->w);
r->m = NULL;
}
if (r != NULL)
r->n = NULL;
return top;
}
/* Length of list */
extern size_t listlen(List *s) {
size_t size;
for (size = 0; s != NULL; s = s->n)
size += strlen(s->w) + 1;
return size;
}
/* Number of elements in list */
extern int listnel(List *s) {
int nel;
for (nel = 0; s != NULL; s = s->n)
nel++;
return nel;
}