From 07e7768399447d3bcb85bc884e13af08e38d00e1 Mon Sep 17 00:00:00 2001 From: dtb Date: Tue, 13 Sep 2022 00:02:15 -0400 Subject: [PATCH] independ roll(1) --- .gitignore | 2 +- Makefile | 15 +++------- roll/Makefile | 27 +++++++++++++++++ roll/roll.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ rot13/Makefile | 1 + src/roll.c | 75 ----------------------------------------------- 6 files changed, 112 insertions(+), 87 deletions(-) create mode 100644 roll/Makefile create mode 100644 roll/roll.c delete mode 100644 src/roll.c diff --git a/.gitignore b/.gitignore index 503d9e0..01b3b42 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,6 @@ bin/retval bin/rldecode bin/rlencode bin/roll -bin/roll_stdio bin/rot13 bin/simexec bin/sleep @@ -19,5 +18,6 @@ bin/sysexits build/* include/sysexits.h lib/*.o +roll/roll rot13/rot13 src/*.o diff --git a/Makefile b/Makefile index 91fac8e..7a8dcd1 100644 --- a/Makefile +++ b/Makefile @@ -37,7 +37,6 @@ cleanprograms: $(RM) bin/rldecode $(RM) bin/rlencode $(RM) bin/roll - $(RM) bin/roll_stdio $(RM) bin/tail $(RM) bin/true @@ -106,17 +105,11 @@ retval.o: libio src/retval.c sysexits retval: libio retval.o $(CC) $(CFLAGS) -o bin/retval build/retval.o build/libio.o -roll.o: lib/libio.h src/roll.c sysexits - $(CC) $(CFLAGS) -c -o build/roll.o src/roll.c +roll: roll/roll + mv roll/roll bin/roll -roll: libio 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 roll_stdio.o - $(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o +roll/roll: sysexits roll/roll.c roll/Makefile + $(MAKE) -C roll sane rot13: rot13/rot13 mv rot13/rot13 bin/rot13 diff --git a/roll/Makefile b/roll/Makefile new file mode 100644 index 0000000..435c0b3 --- /dev/null +++ b/roll/Makefile @@ -0,0 +1,27 @@ +all: roll + +clean: + rm -rf ../dist/roll ../dist/roll.tar ../dist/roll.tar.gz roll + +dist: ../dist/roll.tar.gz + +sane: roll.c ../include/sysexits.h + $(CC) -DDONT_USE_SYSTEM_SYSEXITS -o roll roll.c + +roll: roll.c + $(CC) -o roll roll.c + +../dist/roll: roll + mkdir -p ../dist/roll.tmp/bin/ + cp roll ../dist/roll.tmp/bin/roll + mv ../dist/roll.tmp ../dist/roll + +../dist/roll.tar: ../dist/roll + cd ../dist/roll && pax -w -x ustar . >../roll.tar.tmp + mv ../dist/roll.tar.tmp ../dist/roll.tar + +../dist/roll.tar.gz: ../dist/roll.tar + gzip -c <../dist/roll.tar >../dist/roll.tar.gz.tmp + mv ../dist/roll.tar.gz.tmp ../dist/roll.tar.gz + +.PHONY: all clean sane diff --git a/roll/roll.c b/roll/roll.c new file mode 100644 index 0000000..be441e0 --- /dev/null +++ b/roll/roll.c @@ -0,0 +1,79 @@ +#include +#include +#include + +#ifndef DONT_USE_SYSTEM_SYSEXITS +# include +#else +# include "../include/sysexits.h" +#endif /* ifndef DONT_USE_SYSTEM_SYSEXITS */ + +#include + +static char *program_name = "roll"; + +/* change these to watch the world burn */ +#define ROLLS_BASE 10 +#define SIDES_BASE 10 + +int main(int argc, char *argv[]){ + char *argv0; + char *argvc; + char *ep; + extern int errno; + int r; /* rolls */ + int s; /* sides */ + + argv0 = argv[0]; + + if(argc < 2){ + fprintf(stderr, + "Usage: %s [dice...]\n" + "\tDice should be formatted [rolls]d[sides], e.g. 1d3, 5d6...\n", + argv0 == NULL ? program_name : argv0 + ); + return EX_USAGE; + } + + srand(time(NULL)); + + while(--argc > 0){ + argvc = *++argv; + + /* Parse out [rolls]d[sides] */ + if(!isdigit(**argv)){ +error: fprintf(stderr, + "%s: %s: Improperly formatted die (should be" + " [rolls]d[sides]).\n", + argv0, argvc + ); + return EX_USAGE; + } + + errno = 0; + r = strtol(*argv, &ep, ROLLS_BASE); + + if(errno != 0){ +range: fprintf(stderr, + "%s: %s: Number out of working range for this" + " program.\n", + argv0, argvc + ); + return EX_SOFTWARE; + } + + if(*(*argv = ep) != 'd' || !isdigit(*(++*argv))) + goto error; + + s = strtol(*argv, &ep, SIDES_BASE); + if(errno != 0) + goto range; + if(*ep != '\0') + goto error; + + while(--r >= 0) + fprintf(stdout, "%d\n", rand() % s + 1); + } + + return EX_OK; +} diff --git a/rot13/Makefile b/rot13/Makefile index 6a0bab6..d4044d2 100644 --- a/rot13/Makefile +++ b/rot13/Makefile @@ -7,6 +7,7 @@ dist: ../dist/rot13.tar.gz sane: rot13.c ../include/sysexits.h $(CC) -DDONT_USE_SYSTEM_SYSEXITS -o rot13 rot13.c + rot13: rot13.c $(CC) -o rot13 rot13.c diff --git a/src/roll.c b/src/roll.c deleted file mode 100644 index f9008b2..0000000 --- a/src/roll.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include -#include -#include "libio.h" - -static char *program_name = "roll"; - -int main(int argc, char *argv[]){ - char *argv0; - char *argvc; - int r; /* rolls */ - int s; /* sides */ - - argv0 = argv[0]; - - if(argc < 2){ - fprintf(stderr, - "Usage: %s [dice...]\n" - "\tDice should be formatted [rolls]d[sides], e.g. 1d3, 5d6...\n", - argv0 == NULL ? program_name : argv0 - ); - return EX_USAGE; - } - - srand(time(NULL)); - - while(--argc > 0){ - argvc = *++argv; - - /* Parse out [rolls]d[sides] */ - if(!isdigit(**argv)){ -error: fprintf(stderr, - "%s: %s: Improperly formatted die (should be" - " [rolls]d[sides]).\n", - argv0, argvc - ); - return EX_USAGE; - } - r = parse_uint(*argv); - while(isdigit(**argv)) - ++*argv; - if(*((*argv)++) != 'd' || !isdigit(**argv)) - goto error; - s = parse_uint(*argv); - while(isdigit(**argv)) - ++*argv; - 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) -#ifdef USE_STDIO - fprintf(stdout, "%d\n", -#else - fdputd(1, -#endif - rand() % s + 1); - } - - return EX_OK; -}