mirror of
https://github.com/Lekensteyn/ltunify.git
synced 2025-12-10 18:35:35 +00:00
ltunify: refactor numeric device check
This commit is contained in:
22
ltunify.c
22
ltunify.c
@@ -876,6 +876,15 @@ static void print_usage(const char *program_name) {
|
|||||||
print_device_types();
|
print_device_types();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool is_numeric_device_index(const char *str) {
|
||||||
|
char *end;
|
||||||
|
unsigned long device_index = strtoul(str, &end, 0);
|
||||||
|
|
||||||
|
// if a number was found, there must be no other characters thereafter
|
||||||
|
return !*end &&
|
||||||
|
device_index >= 1 && device_index <= DEVICES_MAX;
|
||||||
|
}
|
||||||
|
|
||||||
// Return number of commands and command arguments, -1 on error. If the program
|
// Return number of commands and command arguments, -1 on error. If the program
|
||||||
// should not run (--help), then 0 is returned and args is NULL.
|
// should not run (--help), then 0 is returned and args is NULL.
|
||||||
static int validate_args(int argc, char **argv, char ***argsp, char **hidraw_path) {
|
static int validate_args(int argc, char **argv, char ***argsp, char **hidraw_path) {
|
||||||
@@ -928,23 +937,16 @@ static int validate_args(int argc, char **argv, char ***argsp, char **hidraw_pat
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (!strcmp(cmd, "unpair") || !strcmp(cmd, "info")) {
|
} else if (!strcmp(cmd, "unpair") || !strcmp(cmd, "info")) {
|
||||||
unsigned long device_index;
|
|
||||||
char *end;
|
|
||||||
if (args_count < 1) {
|
if (args_count < 1) {
|
||||||
fprintf(stderr, "%s requires a device index\n", cmd);
|
fprintf(stderr, "%s requires a device index\n", cmd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
device_index = strtoul(args[1], &end, 0);
|
if (!is_numeric_device_index(args[1]) &&
|
||||||
if (*end != '\0') {
|
device_type_from_str(args[1]) == -1) {
|
||||||
if (device_type_from_str(args[1]) == -1) {
|
fprintf(stderr, "Invalid device type, must be a numeric index or:\n");
|
||||||
fprintf(stderr, "Invalid device type. Valid types are:\n");
|
|
||||||
print_device_types();
|
print_device_types();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (device_index < 1 || device_index > DEVICES_MAX) {
|
|
||||||
fprintf(stderr, "Device index must be a number between 1 and 6.\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Unrecognized command: %s\n", cmd);
|
fprintf(stderr, "Unrecognized command: %s\n", cmd);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user