From 7dd42c510dc8ab10f52dcc2d20dc8af412be0744 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 19 Jul 2016 15:06:58 +0200 Subject: Unify version string printing When a program does sophisticated enough command line processing (i.e. getopt), make sure it responds to -V and --version. When a program prints a version string, make sure it uses the common_print_version macro to print out its name, that it is part of mtd-utils and the mtd-utils version from the build system in a fashion similar to common program packages like the GNU coreutils. When a program responds to -V/--version or -h/--help, make sure it reports success exit status. Signed-off-by: David Oberhollenzer Signed-off-by: Richard Weinberger --- misc-utils/ftl_format.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'misc-utils/ftl_format.c') diff --git a/misc-utils/ftl_format.c b/misc-utils/ftl_format.c index b58677f..74322c7 100644 --- a/misc-utils/ftl_format.c +++ b/misc-utils/ftl_format.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include @@ -253,9 +254,17 @@ static int format_partition(int fd, int quiet, int interrogate, /*====================================================================*/ +static const struct option long_opts[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'V'}, + {0, 0, 0, 0}, +}; + +static const char *short_opts = "qir:s:b:Vh"; + int main(int argc, char *argv[]) { - int quiet, interrogate, reserve; + int quiet, interrogate, reserve, c; int optch, errflg, fd, ret; u_int spare, bootsize; char *s; @@ -269,7 +278,7 @@ int main(int argc, char *argv[]) errflg = 0; bootsize = 0; - while ((optch = getopt(argc, argv, "qir:s:b:")) != -1) { + while ((optch = getopt_long(argc, argv, short_opts, long_opts, &c)) != -1) { switch (optch) { case 'q': quiet = 1; break; @@ -284,14 +293,19 @@ int main(int argc, char *argv[]) if ((*s == 'k') || (*s == 'K')) bootsize *= 1024; break; - default: + case 'V': + common_print_version(); + exit(EXIT_SUCCESS); + case 'h': errflg = 1; break; + default: + errflg = -1; break; } } if (errflg || (optind != argc-1)) { fprintf(stderr, "usage: %s [-q] [-i] [-s spare-blocks]" " [-r reserve-percent] [-b bootsize] device\n", PROGRAM_NAME); - exit(EXIT_FAILURE); + exit(errflg > 0 ? EXIT_SUCCESS : EXIT_FAILURE); } if (stat(argv[optind], &buf) != 0) { -- cgit v1.2.3