Remove redundant material from common.h and adjust Makefile to account
for wc being gone.
This commit is contained in:
parent
c772d723ef
commit
2baca20487
@ -7,7 +7,7 @@ LDLIBS =
|
|||||||
PREFIX = /usr/local/bin/
|
PREFIX = /usr/local/bin/
|
||||||
DESTDIR =
|
DESTDIR =
|
||||||
|
|
||||||
all: echo wc yes true false cat
|
all: echo yes true false cat
|
||||||
echo: echo.c
|
echo: echo.c
|
||||||
wc: wc.c
|
wc: wc.c
|
||||||
yes: yes.c
|
yes: yes.c
|
||||||
@ -17,7 +17,7 @@ false:
|
|||||||
cat: cat.c
|
cat: cat.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm echo true wc yes false cat
|
rm echo true yes false cat
|
||||||
|
|
||||||
.SUFFIXES: .c .o
|
.SUFFIXES: .c .o
|
||||||
.c.o:
|
.c.o:
|
||||||
|
105
src/common.h
105
src/common.h
@ -5,53 +5,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
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 */
|
/* 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). */
|
/* 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 *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len)
|
||||||
@ -75,61 +28,3 @@ char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t arr2len)
|
|||||||
|
|
||||||
return newarr;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user