diff --git a/.gitignore b/.gitignore index 8b4c981..eeb8b1a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,9 +4,12 @@ bin/id bin/lowercase bin/nonzero bin/nutshell +bin/rldecode +bin/rlencode bin/roll bin/simexec bin/sleep +bin/str bin/streq bin/sysexits build/* diff --git a/Makefile b/Makefile index 359c2e6..9b2f657 100644 --- a/Makefile +++ b/Makefile @@ -86,8 +86,15 @@ roll.o: lib/libio.h src/roll.c sysexits roll: libio sysexits roll.o $(CC) $(CFLAGS) -o bin/roll build/libio.o build/roll.o -runlength: sysexits src/runlength.c usefulmacros - $(CC) $(CFLAGS) -o bin/runlength src/runlength.c +rldecode.o: sysexits src/runlength.c + $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c + +rlencode.o: sysexits src/runlength.c + $(CC) $(CFLAGS) -Df=encode -c -o build/rlencode.o src/runlength.c + +runlength: rldecode.o rlencode.o + $(CC) $(CFLAGS) -o bin/rldecode build/rldecode.o + $(CC) $(CFLAGS) -o bin/rlencode build/rlencode.o simexec.o: libio sysexits src/simexec.c $(CC) $(CFLAGS) -c -o build/simexec.o src/simexec.c diff --git a/src/runlength.c b/src/runlength.c index 532f046..137651a 100644 --- a/src/runlength.c +++ b/src/runlength.c @@ -2,8 +2,6 @@ #include /* fprintf(3), getc(3), putc(3) */ #include /* stdin, stderr, stdout */ #include /* EX_DATAERR, EX_OK */ -#include /* for noargvzero.h */ -#include "noargvzero.h" /* Unicode compatibility coming someday */ #define GETC getc @@ -15,7 +13,7 @@ /* if(STRICT) just print it out */ #define STRICT 0 -int decode(FILE *input, FILE *output){ +static int decode(FILE *input, FILE *output){ int c[2]; unsigned int num; enum{NUMBER_PARSING = 1} state; @@ -43,10 +41,10 @@ int decode(FILE *input, FILE *output){ c[0] = c[1]; } - if(state == NUMBER_PARSING && !STRICT){ + if(state == NUMBER_PARSING && !STRICT) /* it doesn't make sense to put this in a loop */ - PUTC(c[0], output); PUTC(c[0], output); - }else if(state == NUMBER_PARSING) + { PUTC(c[0], output); PUTC(c[0], output); } + else if(state == NUMBER_PARSING) return EX_DATAERR; else if(c[0] != EOF) PUTC(c[0], output); @@ -54,7 +52,7 @@ int decode(FILE *input, FILE *output){ return EX_OK; } -int encode(FILE *input, FILE *output){ +static int encode(FILE *input, FILE *output){ int c[2]; unsigned int num; enum{COUNTING = 1} state; @@ -89,32 +87,21 @@ next: if(c[1] == EOF) } } -int (*procedures[2])(FILE *input, FILE *output) = {decode, encode}; - -/* this is inelegant */ -char procedure_names[2][7] = {"decode", "encode"}; - int main(int argc, char *argv[]){ - int i; - enum{DECODE = 0, ENCODE = 1, INVALID = 2} procedure; - procedure = 0; - int retval; + int r; - NOARGVZERO(argv); - - if(argc != 2){ -usage: fprintf(stderr, "Usage: %s {decode,encode}\n", argv[0]); + if(argc != 1){ + fprintf(stderr, + "Usage: %s\n", + argv[0] == NULL ? + (f == decode ? "decode" : "encode") + : argv[0] + ); return EX_USAGE; } - /* imperfect */ - for(i = 0; argv[1][i] != '\0' || procedure_names[procedure][i] != '\0'; ++i) - if(argv[1][i] != procedure_names[procedure][i]) - if(++procedure == INVALID) - goto usage; - - if((retval = procedures[procedure](stdin, stdout)) == EX_DATAERR) + if((r = f(stdin, stdout)) == EX_DATAERR) fprintf(stderr, "%s: syntax error.\n", argv[0]); - return retval; + return r; }