1
0

independ roll(1)

This commit is contained in:
dtb 2022-09-13 00:02:15 -04:00
parent 261f2d46d7
commit 07e7768399
6 changed files with 112 additions and 87 deletions

2
.gitignore vendored
View File

@ -9,7 +9,6 @@ bin/retval
bin/rldecode bin/rldecode
bin/rlencode bin/rlencode
bin/roll bin/roll
bin/roll_stdio
bin/rot13 bin/rot13
bin/simexec bin/simexec
bin/sleep bin/sleep
@ -19,5 +18,6 @@ bin/sysexits
build/* build/*
include/sysexits.h include/sysexits.h
lib/*.o lib/*.o
roll/roll
rot13/rot13 rot13/rot13
src/*.o src/*.o

View File

@ -37,7 +37,6 @@ cleanprograms:
$(RM) bin/rldecode $(RM) bin/rldecode
$(RM) bin/rlencode $(RM) bin/rlencode
$(RM) bin/roll $(RM) bin/roll
$(RM) bin/roll_stdio
$(RM) bin/tail $(RM) bin/tail
$(RM) bin/true $(RM) bin/true
@ -106,17 +105,11 @@ retval.o: libio src/retval.c sysexits
retval: libio retval.o retval: libio retval.o
$(CC) $(CFLAGS) -o bin/retval build/retval.o build/libio.o $(CC) $(CFLAGS) -o bin/retval build/retval.o build/libio.o
roll.o: lib/libio.h src/roll.c sysexits roll: roll/roll
$(CC) $(CFLAGS) -c -o build/roll.o src/roll.c mv roll/roll bin/roll
roll: libio roll.o roll/roll: sysexits roll/roll.c roll/Makefile
$(CC) $(CFLAGS) -o bin/roll build/libio.o build/roll.o $(MAKE) -C roll sane
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
rot13: rot13/rot13 rot13: rot13/rot13
mv rot13/rot13 bin/rot13 mv rot13/rot13 bin/rot13

27
roll/Makefile Normal file
View File

@ -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

79
roll/roll.c Normal file
View File

@ -0,0 +1,79 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef DONT_USE_SYSTEM_SYSEXITS
# include <sysexits.h>
#else
# include "../include/sysexits.h"
#endif /* ifndef DONT_USE_SYSTEM_SYSEXITS */
#include <time.h>
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;
}

View File

@ -7,6 +7,7 @@ dist: ../dist/rot13.tar.gz
sane: rot13.c ../include/sysexits.h sane: rot13.c ../include/sysexits.h
$(CC) -DDONT_USE_SYSTEM_SYSEXITS -o rot13 rot13.c $(CC) -DDONT_USE_SYSTEM_SYSEXITS -o rot13 rot13.c
rot13: rot13.c rot13: rot13.c
$(CC) -o rot13 rot13.c $(CC) -o rot13 rot13.c

View File

@ -1,75 +0,0 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <time.h>
#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;
}