1
0

simplify a bit

This commit is contained in:
dtb 2023-12-23 08:17:11 -07:00
parent 4a0ceca8b0
commit 106effb923

View File

@ -1,26 +1,19 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /* isdigit(3) */
#include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), stderr, NULL */
#include <stdlib.h> /* srand(3), strtol(3) */
#include <time.h> /* time(3) */
#ifndef DONT_USE_SYSTEM_SYSEXITS
#if !defined EX_SOFTWARE || !defined EX_USAGE
# include <sysexits.h>
#else
# include "../include/sysexits.h"
#endif /* ifndef DONT_USE_SYSTEM_SYSEXITS */
#include <time.h>
#endif
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 */
@ -29,7 +22,8 @@ int main(int argc, char *argv[]){
if(argc < 2){
fprintf(stderr,
"Usage: %s [dice...]\n"
"\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
);
return EX_USAGE;
@ -41,31 +35,21 @@ int main(int argc, char *argv[]){
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)))
if(**argv >= 0x80 || !isdigit(**argv))
goto error;
s = strtol(*argv, &ep, SIDES_BASE);
errno = 0;
r = strtol(*argv, &ep, 10);
if(errno != 0)
goto range;
if(*(*argv = ep) != 'd'
|| **argv >= 0x80
|| !isdigit(*(++*argv)))
goto error;
s = strtol(*argv, &ep, 10);
if(errno != 0)
goto range;
if(*ep != '\0')
@ -76,4 +60,18 @@ range: fprintf(stderr,
}
return EX_OK;
error: fprintf(stderr,
"%s: %s: Improperly formatted die"
" (should be [rolls]d[sides]).\n",
argv0, argvc
);
return EX_USAGE;
range: fprintf(stderr,
"%s: %s: Number out of working range for this program.\n",
argv0, argvc
);
return EX_SOFTWARE;
}