diff --git a/owlps-listener/owlps-listenerd.c b/owlps-listener/owlps-listenerd.c index 80c0953..d91bfaa 100644 --- a/owlps-listener/owlps-listenerd.c +++ b/owlps-listener/owlps-listenerd.c @@ -529,6 +529,8 @@ int parse_command_line(int argc, char **argv) int parse_main_options(int argc, char **argv) { int opt ; + long arg_long ; // Integer value of optarg + char *endptr ; // Return value of strtol() optind = 1 ; // Rewind argument parsing @@ -559,7 +561,7 @@ int parse_main_options(int argc, char **argv) break ; case 'H' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_HELLO_PORT(strtol(optarg, NULL, 0)) ; + SET_AUTOCALIBRATION_HELLO_PORT(strtol(optarg, NULL, 10)) ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'i' : @@ -580,27 +582,27 @@ int parse_main_options(int argc, char **argv) #endif // OWLPS_LISTENER_KEEPS_MONITOR break ; case 'l' : - SET_LISTENING_PORT(strtol(optarg, NULL, 0)) ; + SET_LISTENING_PORT(strtol(optarg, NULL, 10)) ; break ; case 'm' : SET_MODE(optarg[0]) ; break ; case 'n' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_NBPKT(strtol(optarg, NULL, 0)) ; + SET_AUTOCALIBRATION_NBPKT(strtol(optarg, NULL, 10)) ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'O' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_ORDER_PORT(strtol(optarg, NULL, 0)) ; + SET_AUTOCALIBRATION_ORDER_PORT(strtol(optarg, NULL, 10)) ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'p' : - SET_AGGREGATION_PORT(strtol(optarg, NULL, 0)) ; + SET_AGGREGATION_PORT(strtol(optarg, NULL, 10)) ; break ; case 'P' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_REQUEST_PORT(strtol(optarg, NULL, 0)) ; + SET_AUTOCALIBRATION_REQUEST_PORT(strtol(optarg, NULL, 10)) ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'q' : @@ -614,12 +616,22 @@ int parse_main_options(int argc, char **argv) break ; case 't' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_DELAY(strtol(optarg, NULL, 0)) ; + arg_long = strtol(optarg, &endptr, 10) ; + if (endptr != optarg) + SET_AUTOCALIBRATION_DELAY(arg_long) ; + else + fprintf(stderr, "Warning! Bad autocalibration_delay:" + " failing back to the default value.\n") ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'T' : #ifdef OWLPS_LISTENER_USES_PTHREAD - SET_AUTOCALIBRATION_HELLO_DELAY(strtol(optarg, NULL, 0)) ; + arg_long = strtol(optarg, &endptr, 10) ; + if (endptr != optarg) + SET_AUTOCALIBRATION_HELLO_DELAY(arg_long) ; + else + fprintf(stderr, "Warning! Bad autocalibration_hello_delay:" + " failing back to the default value.\n") ; #endif // OWLPS_LISTENER_USES_PTHREAD break ; case 'v' : @@ -641,28 +653,69 @@ int parse_main_options(int argc, char **argv) #ifdef OWLPS_LISTENER_USES_PTHREAD +/* Parses remaining arguments (possible calibration data) */ int parse_calibration_data(int argc, char **argv) { - /* Parse remaining arguments (possible calibration data) */ - if (argc - optind != 0) + /* No more arguments to parse */ + if (argc - optind == 0) + return 0 ; + + /* Exactly 4 more arguments */ + if (argc - optind == 4) { - if (argc - optind == 4) + char *endptr ; + unsigned long arg_ulong ; + double arg_double ; + + coordinates_provided = true ; + + arg_ulong = strtoul(argv[optind], &endptr, 10) ; + if (endptr == argv[optind]) { - coordinates_provided = true ; - SET_MY_DIRECTION(strtoul(argv[optind++], NULL, 0)) ; - SET_MY_POSITION_X(strtod(argv[optind++], NULL)) ; - SET_MY_POSITION_Y(strtod(argv[optind++], NULL)) ; - SET_MY_POSITION_Z(strtod(argv[optind], NULL)) ; + fprintf(stderr, + "Error in calibration data: wrong direction!\n") ; + goto error ; } - else // Bad number of arguments + SET_MY_DIRECTION(arg_ulong) ; + optind++ ; + + arg_double = strtod(argv[optind], &endptr) ; + if (endptr == argv[optind]) { - print_usage() ; - owl_run = false ; - return OWL_ERR_BAD_USAGE ; + fprintf(stderr, + "Error in calibration data: wrong X coordinate!\n") ; + goto error ; } + SET_MY_POSITION_X(arg_double) ; + optind++ ; + + arg_double = strtod(argv[optind], &endptr) ; + if (endptr == argv[optind]) + { + fprintf(stderr, + "Error in calibration data: wrong Y coordinate!\n") ; + goto error ; + } + SET_MY_POSITION_Y(arg_double) ; + optind++ ; + + arg_double = strtod(argv[optind], &endptr) ; + if (endptr == argv[optind]) + { + fprintf(stderr, + "Error in calibration data: wrong Z coordinate!\n") ; + goto error ; + } + SET_MY_POSITION_Z(arg_double) ; + + return 0 ; // No error occurred } - return 0 ; + /* Bad number of arguments or parse error */ + error: + print_usage() ; + owl_run = false ; + return OWL_ERR_BAD_USAGE ; } #endif // OWLPS_LISTENER_USES_PTHREAD @@ -772,7 +825,7 @@ int check_configuration() } if (GET_AUTOCALIBRATION_NBPKT() < 1) - fprintf(stderr, "Warning! autocalibration_nb_packets is null," + fprintf(stderr, "Warning! autocalibration_nb_packets is zero," " no autocalibration request will be sent!\n") ; if (coordinates_provided)