diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-03-13 11:39:44 +0100 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2023-03-16 16:46:44 +0100 |
commit | 4b0f96748411a1bef8bdc0bae6f9e9d316223028 (patch) | |
tree | 275000a732c133721b9f2ab1b8daf82c4372c791 /src/udev/fido_id | |
parent | udev/ata_id: stop using errno, fix logic (diff) | |
download | systemd-4b0f96748411a1bef8bdc0bae6f9e9d316223028.tar.xz systemd-4b0f96748411a1bef8bdc0bae6f9e9d316223028.zip |
udev/fido_id: implement --help
Diffstat (limited to 'src/udev/fido_id')
-rw-r--r-- | src/udev/fido_id/fido_id.c | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/src/udev/fido_id/fido_id.c b/src/udev/fido_id/fido_id.c index f2fbc38003..11f5320d2b 100644 --- a/src/udev/fido_id/fido_id.c +++ b/src/udev/fido_id/fido_id.c @@ -8,6 +8,7 @@ #include <errno.h> #include <fcntl.h> +#include <getopt.h> #include <linux/hid.h> #include <stdio.h> #include <stdlib.h> @@ -25,16 +26,43 @@ #include "string-util.h" #include "udev-util.h" +static const char *arg_device = NULL; + +static int parse_argv(int argc, char *argv[]) { + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + {} + }; + int c; + + while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) + switch (c) { + case 'h': + printf("%s [OPTIONS...] SYSFS_PATH\n\n" + " -h --help Show this help text\n", + program_invocation_short_name); + return 0; + case '?': + return -EINVAL; + default: + assert_not_reached(); + } + + if (argc > 2) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Error: unexpected argument."); + + arg_device = argv[optind]; + return 1; +} + static int run(int argc, char **argv) { _cleanup_(sd_device_unrefp) struct sd_device *device = NULL; _cleanup_free_ char *desc_path = NULL; _cleanup_close_ int fd = -EBADF; - struct sd_device *hid_device; const char *sys_path; uint8_t desc[HID_MAX_DESCRIPTOR_SIZE]; ssize_t desc_len; - int r; log_set_target(LOG_TARGET_AUTO); @@ -42,17 +70,18 @@ static int run(int argc, char **argv) { log_parse_environment(); log_open(); - if (argc > 2) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Usage: %s [SYSFS_PATH]", program_invocation_short_name); + r = parse_argv(argc, argv); + if (r <= 0) + return r; - if (argc == 1) { - r = device_new_from_strv(&device, environ); + if (arg_device) { + r = sd_device_new_from_syspath(&device, arg_device); if (r < 0) - return log_error_errno(r, "Failed to get current device from environment: %m"); + return log_error_errno(r, "Failed to get device from syspath %s: %m", arg_device); } else { - r = sd_device_new_from_syspath(&device, argv[1]); + r = device_new_from_strv(&device, environ); if (r < 0) - return log_error_errno(r, "Failed to get device from syspath: %m"); + return log_error_errno(r, "Failed to get current device from environment: %m"); } r = sd_device_get_parent(device, &hid_device); |