fix runlength building
This commit is contained in:
parent
57ea5c20b8
commit
d76402418c
3
.gitignore
vendored
3
.gitignore
vendored
@ -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/*
|
||||
|
11
Makefile
11
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
|
||||
|
@ -2,8 +2,6 @@
|
||||
#include <stdio.h> /* fprintf(3), getc(3), putc(3) */
|
||||
#include <stdlib.h> /* stdin, stderr, stdout */
|
||||
#include <sysexits.h> /* EX_DATAERR, EX_OK */
|
||||
#include <unistd.h> /* 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user