How would I go about checking for mishaps in typos that is suppose to be in a number format as such 3s or 3m or 3 if someone enters something like this,
Code:
userx@slackwhere\⚡/media/data/C-Projects/mhsetroot/mhsetroot/bin/Debug\⚡\ >
./mhsetroot -over-ride -z f3m /media/data/wallpaper
f3m //that is what was typed in
0 // that is what what the value is in the var because nothing usable is returned.
defaults time settings // that is what gets called if value is zero,
therefore the function that sets the default time when -z option is not used is called due to var being zero.
Code:
int seconds_or_minutes(char *what_is_it)
{
printf("%s\n",what_is_it);
const char sec = 's';
const char min = 'm';
char str1[1+strlen(what_is_it)];
strcpy(str1, what_is_it);
if ( strrchr(what_is_it, sec) )
{
return (atoi( strtok(str1, "s") ));
}
if( strrchr(what_is_it, min) )
{
return (atoi(strtok(str1,"m")) * 60);
}
if (isdigit(what_is_it))
{
printf("is not a number\n");
exit(1);
}
printf("%s",what_is_it);
//defaults to seconds
return atoi(what_is_it);
}
just to show how it calls for the value using getopts, and the other function that sets default time when option -z not used. So you'll know what is going on with that end of it.
Code:
case 'z':
if (optarg == NULL)
printf("< %s >\n",optarg);
else
opts.ch_t = seconds_or_minutes(optarg);
printf("%d\n",opts.ch_t);
break;
-------------------------------------
#ifndef DEFAULT_TIME
#define DEFAULT_TIME (5 * 60)
#endif // DEFAULT_TIME
void default_time_settings(void)
{
printf("defaults time settings\n");
if(opts.ch_t == 0)
opts.ch_t = DEFAULT_TIME;
}
So, what would, is a good way to accomplish this feet (feat) of getting it to stop the program if such a mishaps as that is preformed?