Added input digit checks

This commit is contained in:
Martin Lund 2016-05-23 19:01:40 +02:00
parent aecc829829
commit 81be02fc03

View file

@ -67,6 +67,22 @@ void print_help(char *argv[])
printf("\n"); printf("\n");
} }
long string_to_long(char *string)
{
long result;
char *end_token;
errno = 0;
result = strtol(string, &end_token, 10);
if ((errno != 0) || (*end_token != 0))
{
error_printf("Invalid digit");
exit(EXIT_FAILURE);
}
return result;
}
void parse_options(int argc, char *argv[]) void parse_options(int argc, char *argv[])
{ {
int c; int c;
@ -117,11 +133,11 @@ void parse_options(int argc, char *argv[])
break; break;
case 'b': case 'b':
option.baudrate = atoi(optarg); option.baudrate = string_to_long(optarg);
break; break;
case 'd': case 'd':
option.databits = atoi(optarg); option.databits = string_to_long(optarg);
break; break;
case 'f': case 'f':
@ -129,7 +145,7 @@ void parse_options(int argc, char *argv[])
break; break;
case 's': case 's':
option.stopbits = atoi(optarg); option.stopbits = string_to_long(optarg);
break; break;
case 'p': case 'p':
@ -137,7 +153,7 @@ void parse_options(int argc, char *argv[])
break; break;
case 'o': case 'o':
option.output_delay = atoi(optarg); option.output_delay = string_to_long(optarg);
break; break;
case 'n': case 'n':