1
0

roll_stdio(3)

This commit is contained in:
dtb 2022-08-19 21:29:46 -04:00
parent 01c6262409
commit a998eb9ef7
2 changed files with 29 additions and 4 deletions

View File

@ -58,7 +58,7 @@ sysexits_bin: src/sysexits.c
sysexits: sysexits_bin sysexits: sysexits_bin
bin/sysexits >include/sysexits.h 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 calculate: libstr sysexits src/calculate.c
$(CC) $(CFLAGS) -o src/calculate.o -c 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 roll: libio sysexits roll.o
$(CC) $(CFLAGS) -o bin/roll build/libio.o build/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 rldecode.o: sysexits src/runlength.c
$(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c

View File

@ -21,6 +21,7 @@ int main(int argc, char *argv[]){
"\tDice should be formatted [rolls]d[sides], e.g. 1d3, 5d6...\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)); srand(time(NULL));
@ -32,7 +33,7 @@ int main(int argc, char *argv[]){
if(!isdigit(**argv)){ if(!isdigit(**argv)){
error: fprintf(stderr, error: fprintf(stderr,
"%s: %s: Improperly formatted die (should be" "%s: %s: Improperly formatted die (should be"
" [rolls]d[sides]).", " [rolls]d[sides]).\n",
argv0, argvc argv0, argvc
); );
return EX_USAGE; return EX_USAGE;
@ -48,8 +49,26 @@ error: fprintf(stderr,
if(**argv != '\0') if(**argv != '\0')
goto error; 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) 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; return EX_OK;