list.c (1160B)
1 /* list.c: routines for manipulating the List type */ 2 3 #include "rc.h" 4 5 /* 6 These list routines assign meta values of null to the resulting lists; 7 it is impossible to glob with the value of a variable unless this value 8 is rescanned with eval---therefore it is safe to throw away the meta-ness 9 of the list. 10 */ 11 12 /* free a list from malloc space */ 13 14 extern void listfree(List *p) { 15 while (p != NULL) { 16 List *n = p->n; 17 efree(p->w); 18 efree(p); 19 p = n; 20 } 21 } 22 23 /* Copy list into malloc space (for storing a variable) */ 24 25 extern List *listcpy(List *s, void *(*alloc)(size_t)) { 26 List *top, *r; 27 for (top = r = NULL; s != NULL; s = s->n) { 28 if (top == NULL) 29 r = top = (*alloc)(sizeof (List)); 30 else 31 r = r->n = (*alloc)(sizeof (List)); 32 r->w = (*alloc)(strlen(s->w) + 1); 33 strcpy(r->w, s->w); 34 r->m = NULL; 35 } 36 if (r != NULL) 37 r->n = NULL; 38 return top; 39 } 40 41 /* Length of list */ 42 43 extern size_t listlen(List *s) { 44 size_t size; 45 for (size = 0; s != NULL; s = s->n) 46 size += strlen(s->w) + 1; 47 return size; 48 } 49 50 /* Number of elements in list */ 51 52 extern int listnel(List *s) { 53 int nel; 54 for (nel = 0; s != NULL; s = s->n) 55 nel++; 56 return nel; 57 }