roll(1)
This commit is contained in:
parent
1cb1e48415
commit
07216c778a
56
src/roll.c
Normal file
56
src/roll.c
Normal file
@ -0,0 +1,56 @@
|
||||
#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
|
||||
);
|
||||
}
|
||||
|
||||
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]).",
|
||||
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;
|
||||
|
||||
while(--r >= 0)
|
||||
fdputd(1, rand() % s + 1);
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
}
|
Loading…
Reference in New Issue
Block a user