diff --git a/Makefile b/Makefile index 91e6dff..9cc73ad 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ sysexits_bin: src/sysexits.c sysexits: sysexits_bin bin/sysexits >include/sysexits.h -programs: echo false id lowercase nonzero roll simexec sleep streq str true +programs: echo false id lowercase nonzero pscat roll simexec sleep streq str true calculate: libstr sysexits src/calculate.c $(CC) $(CFLAGS) -o src/calculate.o -c src/calculate.c @@ -103,6 +103,12 @@ 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 +roll_stdio.o: lib/libio.h src/roll.c sysexits + $(CC) $(CFLAGS) -c -DUSE_STDIO -o build/roll_stdio.o src/roll.c + +roll_stdio: libio sysexits roll_stdio.o + $(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o + rldecode.o: sysexits src/runlength.c $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c diff --git a/src/roll.c b/src/roll.c index 2c068f8..f9008b2 100644 --- a/src/roll.c +++ b/src/roll.c @@ -19,8 +19,9 @@ int main(int argc, char *argv[]){ fprintf(stderr, "Usage: %s [dice...]\n" "\tDice should be formatted [rolls]d[sides], e.g. 1d3, 5d6...\n", - argv0 == NULL? program_name : argv0 + argv0 == NULL ? program_name : argv0 ); + return EX_USAGE; } srand(time(NULL)); @@ -32,7 +33,7 @@ int main(int argc, char *argv[]){ if(!isdigit(**argv)){ error: fprintf(stderr, "%s: %s: Improperly formatted die (should be" - " [rolls]d[sides]).", + " [rolls]d[sides]).\n", argv0, argvc ); return EX_USAGE; @@ -48,8 +49,26 @@ error: fprintf(stderr, if(**argv != '\0') goto error; + /* On 2022-08-19 I experimented with using fprintf(3) instead + * of libio's fdputd(3) to see if stdio was faster, thinking + * the buffering would make a difference. stdio flushes its + * buffer after every newline so in this particular code it + * cannot improve performance unless fdputd(3)'s implementation + * really sucked - no beans! + * It also is an issue where every benchmark reports 0.00 real + * / user / sys times for execution because this program does + * very little - but if you fire up strace(3) you will find the + * system calls (which are nowadays usually the slowest part of + * a given small UNIX utility) are the same either way. + * I'm keeping this little option here in case someone wants to + * peer review; no harm in it. */ while(--r >= 0) - fdputd(1, rand() % s + 1); +#ifdef USE_STDIO + fprintf(stdout, "%d\n", +#else + fdputd(1, +#endif + rand() % s + 1); } return EX_OK;