How to verify if the user has passed only integer value to a getopt option from the command line ? User should pass only positive integer values .
Isdigit() function does not work properly here .
case 's' :
flags=1;
start = atoi(optarg);
How to check if start contains only integer value ?
./prog -scf
By default its taking s value as 0 when any character is entered , since i am using atoi function but here actually it should give error as cf is passed .
Even tried strtol() , but no use
start = (int) strtol(optarg, &ptr, 10);
even if i use strtol() function , it works only if both number and strings are passed .
eg ./prog -s5abc --> this works
eg ./prog -sabc -->does not work
here again start takes 0 value , as only charters is passed ! but if user itself passes 0 then how can i handle error ?
eg ./prog -s0 --> s takes 0 value, valid
eg ./prog -sabc --> s takes 0 value , but invalid
As you are working with signed numbers, this should do:
if (optarg[0] != '-' && (optarg[0] < '0' || optarg[0] > '9'))
*error handling*
for (size_t i = 1; optarg[i] != 0; i++)
if (optarg[i] < '0' || optarg[i] > '9')
*error handling*