#include #include #include #include #include #include enum { HASHSIZE = 101 }; /* Shamelessly copied from KnR. */ typedef struct NLIST { char *name; void *defn; struct NLIST *next; } nlist; unsigned int hash(char *s) { unsigned int hashval; for(hashval = 0; *s != '\0'; s++) { hashval = *s + 31 * hashval; } return hashval % HASHSIZE; } static nlist *hashtab[HASHSIZE]; struct nlist *lookup(char *s) { nlist *np; for(np = hashtab[hash(s)];np != NULL; np = np->next) { } } /* Key is the name of this particular argument -- "a", "h", or "find", for example */ /* Value is generally a boolean (to indicate presence), but can be used for other things. */ /* */ typedef struct CELL { struct CELL *prev; char *key; char value; struct CELL *next; } cell; /* arrccat -- function to copy arr2len bytes from arr2 to the arr1len'th position of arr1. returns *newarr, and stores the length */ /* of newarr[] in arr1len (that's why it needs to be a pointer to arr1len and not just a long unsigned integer). */ char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len) { char *newarr; unsigned int i, i2; newarr = malloc(*arr1len+arr2len); i = i2 = 0; for(i = 0; i < *arr1len; i++) { newarr[i] = arr1[i]; } for(i2 = 0; i2 < arr2len; i2++, i++) { newarr[i] = arr2[i2]; } *arr1len = *arr1len+arr2len; return newarr; } /* Generate, initialize and return a doubly-linked list cell */ cell *cellgen(cell *pcell, char *key, int value, cell *ncell) { cell *newcell = malloc(sizeof(cell)); (*newcell).prev = pcell; (*newcell).key = key; (*newcell).value = value; (*newcell).next = ncell; return newcell; } /* Generate, initialize and return a doubly-linked list containing one cell per array key */ /* The final cell is guaranteed to have a value of NULL, 0, 0, NULL and serves as a sentinel. */ /* argstr is expected to be of similar format to argv -- an array of strings. argstrlen should */ /* correspond to the number of discrete strings are contained within argstr. */ cell *arglistgen(char **argstr, unsigned int argstrlen) { unsigned int i, i2; i = i2 = 0; cell *ci, *ci2; ci = ci2 = 0; for(i = 0; i <= argstrlen; i++) { if(!ci) { ci = ci2 = cellgen(NULL, argstr[i], 0, NULL); } else { ci -> next = cellgen(ci, argstr[i], 0, NULL); ci = ci -> next; } } return(ci2); } /* inlist_- -- return 1 if an element is in the provided list, 0 otherwise. list should be the first cell in a list */ int inlist_p(char *tofind, cell *list) { cell *ci = list; for(; ci -> next != NULL and ci -> key != NULL; ci = ci -> next) { if(!strcmp(ci->key, tofind)) { return 1; } } return 0; }