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 --- nand-utils/nanddump.c | 15 +++++++-------- nand-utils/nandtest.c | 9 +++++++-- nand-utils/nandwrite.c | 17 ++++++++--------- nand-utils/nftl_format.c | 33 +++++++++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 23 deletions(-) (limited to 'nand-utils') diff --git a/nand-utils/nanddump.c b/nand-utils/nanddump.c index ec17314..a8ad334 100644 --- a/nand-utils/nanddump.c +++ b/nand-utils/nanddump.c @@ -63,9 +63,8 @@ static void display_help(int status) static void display_version(void) { - printf("%1$s " VERSION "\n" - "\n" - "%1$s comes with NO WARRANTY\n" + common_print_version(); + printf("%1$s comes with NO WARRANTY\n" "to the extent permitted by law.\n" "\n" "You may redistribute copies of %1$s\n" @@ -101,9 +100,9 @@ static void process_options(int argc, char * const argv[]) for (;;) { int option_index = 0; - static const char short_options[] = "hs:f:l:opqnca"; + static const char short_options[] = "hs:f:l:opqncaV"; static const struct option long_options[] = { - {"version", no_argument, 0, 0}, + {"version", no_argument, 0, 'V'}, {"bb", required_argument, 0, 0}, {"omitoob", no_argument, 0, 0}, {"help", no_argument, 0, 'h'}, @@ -128,9 +127,6 @@ static void process_options(int argc, char * const argv[]) switch (c) { case 0: switch (option_index) { - case 0: - display_version(); - break; case 1: /* Handle --bb=METHOD */ if (!strcmp(optarg, "padbad")) @@ -152,6 +148,9 @@ static void process_options(int argc, char * const argv[]) break; } break; + case 'V': + display_version(); + break; case 's': start_addr = simple_strtoll(optarg, &error); break; diff --git a/nand-utils/nandtest.c b/nand-utils/nandtest.c index 2ef7cc8..5676733 100644 --- a/nand-utils/nandtest.c +++ b/nand-utils/nandtest.c @@ -22,6 +22,7 @@ void usage(int status) fprintf(status ? stderr : stdout, "usage: %s [OPTIONS] \n\n" " -h, --help Display this help output\n" + " -V, --version Display version information and exit\n" " -m, --markbad Mark blocks bad if they appear so\n" " -s, --seed Supply random seed\n" " -p, --passes Number of passes\n" @@ -160,9 +161,10 @@ int main(int argc, char **argv) seed = time(NULL); for (;;) { - static const char short_options[] = "hkl:mo:p:r:s:"; + static const char short_options[] = "hkl:mo:p:r:s:V"; static const struct option long_options[] = { { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'V' }, { "markbad", no_argument, 0, 'm' }, { "seed", required_argument, 0, 's' }, { "passes", required_argument, 0, 'p' }, @@ -181,7 +183,10 @@ int main(int argc, char **argv) case 'h': usage(0); break; - + case 'V': + common_print_version(); + exit(EXIT_SUCCESS); + break; case '?': usage(1); break; diff --git a/nand-utils/nandwrite.c b/nand-utils/nandwrite.c index 9c3fe8f..1c00cdf 100644 --- a/nand-utils/nandwrite.c +++ b/nand-utils/nandwrite.c @@ -61,16 +61,15 @@ static void display_help(int status) " --input-size=length Only read |length| bytes of the input file\n" " -q, --quiet Don't display progress messages\n" " -h, --help Display this help and exit\n" -" --version Output version information and exit\n" +" -V, --version Output version information and exit\n" ); exit(status); } static void display_version(void) { - printf("%1$s " VERSION "\n" - "\n" - "Copyright (C) 2003 Thomas Gleixner \n" + common_print_version(); + printf("Copyright (C) 2003 Thomas Gleixner \n" "\n" "%1$s comes with NO WARRANTY\n" "to the extent permitted by law.\n" @@ -103,10 +102,10 @@ static void process_options(int argc, char * const argv[]) for (;;) { int option_index = 0; - static const char short_options[] = "hb:mnNoOpqs:a"; + static const char short_options[] = "hb:mnNoOpqs:aV"; static const struct option long_options[] = { /* Order of these args with val==0 matters; see option_index. */ - {"version", no_argument, 0, 0}, + {"version", no_argument, 0, 'V'}, {"input-skip", required_argument, 0, 0}, {"input-size", required_argument, 0, 0}, {"help", no_argument, 0, 'h'}, @@ -131,9 +130,6 @@ static void process_options(int argc, char * const argv[]) switch (c) { case 0: switch (option_index) { - case 0: /* --version */ - display_version(); - break; case 1: /* --input-skip */ inputskip = simple_strtoll(optarg, &error); break; @@ -142,6 +138,9 @@ static void process_options(int argc, char * const argv[]) break; } break; + case 'V': + display_version(); + break; case 'q': quiet = true; break; diff --git a/nand-utils/nftl_format.c b/nand-utils/nftl_format.c index 1fc3b36..8485553 100644 --- a/nand-utils/nftl_format.c +++ b/nand-utils/nftl_format.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -40,6 +41,8 @@ #include #include +#include "common.h" + unsigned char BadUnitTable[MAX_ERASE_ZONES]; unsigned char *readbuf; unsigned char *writebuf[4]; @@ -53,6 +56,12 @@ struct INFTLMediaHeader *INFTLhdr; static int do_oobcheck = 1; static int do_rwecheck = 1; +static const struct option long_opts[] = { + {"version", no_argument, 0, 'V'}, + {"help", no_argument, 0, 'h'}, + {0, 0, 0, 0}, +}; + static unsigned char check_block_1(unsigned long block) { unsigned char oobbuf[16]; @@ -195,6 +204,21 @@ void usage(int rc) exit(rc); } +static void display_version(void) +{ + common_print_version(); + printf("Copyright (C) 2005 Thomas Gleixner \n" + "\n" + "%1$s comes with NO WARRANTY\n" + "to the extent permitted by law.\n" + "\n" + "You may redistribute copies of %1$s\n" + "under the terms of the GNU General Public Licence.\n" + "See the file `COPYING' for more information.\n", + PROGRAM_NAME); + exit(EXIT_SUCCESS); +} + int main(int argc, char **argv) { unsigned long startofs = 0, part_size = 0; @@ -207,16 +231,14 @@ int main(int argc, char **argv) char *mtddevice; const char *nftl; int c, do_inftl = 0, do_bbt = 0; - - - printf("version 1.24 2005/11/07 11:15:13 gleixner\n"); + int idx = 0; if (argc < 2) usage(1); nftl = "NFTL"; - while ((c = getopt(argc, argv, "?hib")) > 0) { + while ((c = getopt_long(argc, argv, "?hibV", long_opts, &idx)) != -1) { switch (c) { case 'i': nftl = "INFTL"; @@ -229,6 +251,9 @@ int main(int argc, char **argv) case '?': usage(0); break; + case 'V': + display_version(); + break; default: usage(1); break; -- cgit v1.2.3