udev/mtd_probe: implement --help

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-03-16 16:46:44 +01:00
parent 4b0f967484
commit 8b89b9d7c3
+34 -4
View File
@@ -20,6 +20,7 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <mtd/mtd-user.h>
#include <stdio.h>
#include <stdlib.h>
@@ -32,14 +33,43 @@
#include "fd-util.h"
#include "mtd_probe.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 /dev/mtd[n]\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;
}
int main(int argc, char** argv) {
_cleanup_close_ int mtd_fd = -EBADF;
mtd_info_t mtd_info;
int r;
if (argc != 2) {
printf("usage: mtd_probe /dev/mtd[n]\n");
return EXIT_FAILURE;
}
r = parse_argv(argc, argv);
if (r <= 0)
return r < 0 ? EXIT_FAILURE : 0;
mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (mtd_fd < 0) {