diff options
author | Eric Biggers <ebiggers@google.com> | 2023-07-09 11:36:42 -0700 |
---|---|---|
committer | Eric Biggers <ebiggers@google.com> | 2023-07-09 11:36:46 -0700 |
commit | 4ba79698b0381fe953f2d74b622b3b9586969e2b (patch) | |
tree | 76d143ff9cec9d74db7471f48912d9a10e0ac52c | |
parent | 585e14a87e7b332dfd00c52fac5e96fc1c760136 (diff) | |
download | fsverity-utils-4ba79698b0381fe953f2d74b622b3b9586969e2b.tar.gz |
cmd_measure: reject options and handle "--" correctly
Even though 'fsverity measure' currently takes no command-line options,
it should still run getopt_long() so that arguments beginning with "-"
are treated in the standard way, instead of as filenames.
Signed-off-by: Eric Biggers <ebiggers@google.com>
-rw-r--r-- | programs/cmd_measure.c | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/programs/cmd_measure.c b/programs/cmd_measure.c index d78969c..e5cbff1 100644 --- a/programs/cmd_measure.c +++ b/programs/cmd_measure.c @@ -12,8 +12,13 @@ #include "fsverity.h" #include <fcntl.h> +#include <getopt.h> #include <sys/ioctl.h> +static const struct option longopts[] = { + {NULL, 0, NULL, 0} +}; + /* Display the fs-verity digest of the given verity file(s). */ int fsverity_cmd_measure(const struct fsverity_command *cmd, int argc, char *argv[]) @@ -26,12 +31,22 @@ int fsverity_cmd_measure(const struct fsverity_command *cmd, int status; int i; - if (argc < 2) + /* + * No supported options, but run getopt_long() with an empty longopts + * array so that any options are rejected and "--" works as expected. + */ + if (getopt_long(argc, argv, "", longopts, NULL) != -1) + goto out_usage; + + argv += optind; + argc -= optind; + + if (argc < 1) goto out_usage; d = xzalloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE); - for (i = 1; i < argc; i++) { + for (i = 0; i < argc; i++) { d->digest_size = FS_VERITY_MAX_DIGEST_SIZE; if (!open_file(&file, argv[i], O_RDONLY, 0)) |