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

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;
}