Remove wc.c. Will write it (and rewrite cat) when better way to handle

argument parsing is discovered.
master
Mid Favila 4 months ago
parent 0289a812f5
commit c772d723ef
  1. 1
      src/cat.c
  2. 102
      src/common.h
  3. 67
      src/wc.c

@ -100,7 +100,6 @@ int main(int argc, char **argv)
buf = tmpbuf;
}
}
if(!fppresence)

@ -5,18 +5,52 @@
#include <unistd.h>
#include <sys/stat.h>
/* arrconcat -- function to copy copy_n bytes from arr2 to arr1, starting at startpos (in arr1) */
void arrconcat(char* arr1, char* arr2, unsigned int startpos, unsigned int copy_n)
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 i, i2;
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(i = startpos, i2 = 0; i <= copy_n && arr2 != NULL; i++, i2++)
for(np = hashtab[hash(s)];np != NULL; np = np->next)
{
arr1[i] = arr2[i2];
}
}
/* 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). */
@ -41,3 +75,61 @@ char *arrccat(char *arr1, char *arr2, size_t *arr1len, size_t 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;
}

@ -1,67 +0,0 @@
#include "common.h"
enum {
LINES,
WORDS,
BYTES
};
int* count_values(FILE *fp)
{
static int count[3] = {0};
char in_word, c;
in_word = c = 0;
while((c = getc(fp)) != EOF)
if(in_word)
switch(c)
{
case '\n':
count[LINES]++, count[BYTES]++;
in_word = 0;
break;
case ' ':
in_word = 0;
count[BYTES]++;
break;
case '\t':
in_word = 0;
count[BYTES]++;
break;
default:
count[BYTES]++;
break;
}
else
switch(c)
{
case '\n':
count[LINES]++, count[BYTES]++;
break;
case ' ':
count[BYTES]++;
break;
case '\t':
count[BYTES]++;
break;
default:
count[WORDS]++, count[BYTES]++;
in_word = 1;
break;
}
return count;
}
int main(int argc, char **argv)
{
return 0;
err:
}
Loading…
Cancel
Save