[Aggregator] Test numbers from the command line

Check the return value of most calls to strtol().
Force base 10 when reading integers from the command line.
This commit is contained in:
Matteo Cypriani 2013-09-27 14:37:31 -04:00
parent ed690ad196
commit 89f0b583e3
1 changed files with 36 additions and 9 deletions

View File

@ -363,6 +363,8 @@ int parse_config_file(int argc, char **argv)
int parse_command_line(int argc, char **argv) int parse_command_line(int argc, char **argv)
{ {
int opt ; int opt ;
long arg_long ; // Integer value of optarg
char *endptr ; // Return value of strtol()
optind = 1 ; // Rewind argument parsing optind = 1 ; // Rewind argument parsing
@ -374,10 +376,20 @@ int parse_command_line(int argc, char **argv)
cfg_setbool(cfg, "autocalibration", cfg_true) ; cfg_setbool(cfg, "autocalibration", cfg_true) ;
break ; break ;
case 'c' : case 'c' :
cfg_setint(cfg, "check_interval", strtol(optarg, NULL, 0)) ; arg_long = strtol(optarg, &endptr, 10) ;
if (endptr != optarg)
cfg_setint(cfg, "check_interval", arg_long) ;
else
fprintf(stderr, "Warning! Bad check_interval:"
" failing to the default value.\n") ;
break ; break ;
case 'C' : case 'C' :
cfg_setint(cfg, "ac_order_interval", strtol(optarg, NULL, 0)) ; arg_long = strtol(optarg, &endptr, 10) ;
if (endptr != optarg)
cfg_setint(cfg, "ac_order_interval", arg_long) ;
else
fprintf(stderr, "Warning! Bad ac_order_interval:"
" failing back to the default value.\n") ;
break ; break ;
case 'D' : case 'D' :
cfg_setbool(cfg, "daemon", cfg_true) ; cfg_setbool(cfg, "daemon", cfg_true) ;
@ -394,36 +406,51 @@ int parse_command_line(int argc, char **argv)
break ; break ;
case 'H' : case 'H' :
cfg_setint(cfg, "autocalibration_hello_port", cfg_setint(cfg, "autocalibration_hello_port",
strtol(optarg, NULL, 0)) ; strtol(optarg, NULL, 10)) ;
break ; break ;
case 'i' : case 'i' :
cfg_setstr(cfg, "positioner_host", optarg) ; cfg_setstr(cfg, "positioner_host", optarg) ;
break ; break ;
case 'k' : case 'k' :
cfg_setint(cfg, "keep_timeout", strtol(optarg, NULL, 0)) ; arg_long = strtol(optarg, &endptr, 10) ;
if (endptr != optarg)
cfg_setint(cfg, "keep_timeout", arg_long) ;
else
fprintf(stderr, "Warning! Bad keep_timeout:"
" failing back to the default value.\n") ;
break ; break ;
case 'K' : case 'K' :
cfg_setint(cfg, "cp_keep_timeout", strtol(optarg, NULL, 0)) ; arg_long = strtol(optarg, &endptr, 10) ;
if (endptr != optarg)
cfg_setint(cfg, "cp_keep_timeout", arg_long) ;
else
fprintf(stderr, "Warning! Bad cp_keep_timeout:"
" failing back to the default value.\n") ;
break ; break ;
case 'l' : case 'l' :
cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 0)) ; cfg_setint(cfg, "listening_port", strtol(optarg, NULL, 10)) ;
break ; break ;
case 'o' : case 'o' :
cfg_setstr(cfg, "output_file", optarg) ; cfg_setstr(cfg, "output_file", optarg) ;
break ; break ;
case 'O' : case 'O' :
cfg_setint(cfg, "autocalibration_order_port", cfg_setint(cfg, "autocalibration_order_port",
strtol(optarg, NULL, 0)) ; strtol(optarg, NULL, 10)) ;
break ; break ;
case 'p' : case 'p' :
cfg_setint(cfg, "positioner_port", strtol(optarg, NULL, 0)) ; cfg_setint(cfg, "positioner_port", strtol(optarg, NULL, 10)) ;
break ; break ;
case 'q' : case 'q' :
// Quiet mode: reset the verbose level // Quiet mode: reset the verbose level
cfg_setint(cfg, "verbose", 0) ; cfg_setint(cfg, "verbose", 0) ;
break ; break ;
case 't' : case 't' :
cfg_setint(cfg, "aggregate_timeout", strtol(optarg, NULL, 0)) ; arg_long = strtol(optarg, &endptr, 10) ;
if (endptr != optarg)
cfg_setint(cfg, "aggregate_timeout", arg_long) ;
else
fprintf(stderr, "Warning! Bad aggregate_timeout:"
" failing back to the default value.\n") ;
break ; break ;
case 'v' : case 'v' :
// Increment the verbose level // Increment the verbose level