From 00bbff20c67e26bdc6f12e56c7fd1af1d05631d8 Mon Sep 17 00:00:00 2001 From: Dave Vandervies Date: Sat, 26 Oct 2019 16:30:05 -0400 Subject: [PATCH] Use getopt --- dpu.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/dpu.c b/dpu.c index 1b3b185..2b4c318 100644 --- a/dpu.c +++ b/dpu.c @@ -2,27 +2,38 @@ #include #include +#include + int main (int argc, char **argv){ char name[FILENAME_MAX]; FILE *bob,*joe,*moe; int thing, factor, arg; - if(argc >= 3){ - char *end_char; - factor = strtol(argv[1], &end_char, 0); - if(!*(argv[1]) || *end_char) - { - fputs("I can haz number?\n", stderr); + int modifier; + factor=0; + while((modifier=getopt(argc,argv,"0123456789"))+1){ + switch(modifier){ + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + factor=factor*10+modifier-'0'; + break; + default: + fputs("You're going to have to be clearer about what you want.\n",stderr); return EXIT_FAILURE; } - arg = 2; - }else{ - factor = 9; - arg = 1; } - if(argc<2){ + if(factor==0){ + factor=9; + } + argc-=optind; + argv+=optind; + arg=0; + if(argc<1){ fputs("I pity the fool who don't have an input file!\n",stderr); return EXIT_FAILURE; } + if(argc>1){ + fputs("Be patient, one thing at a time!\n",stderr); + } if(strlen(argv[arg]) >= FILENAME_MAX - 4) { fputs("It's a filename, not a novel.\n", stderr); -- 2.37.3