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/flash_erase.c | 27 +++++++++++---------------- misc-utils/flash_unlock.c | 8 ++++---- misc-utils/flashcp.c | 13 +++++++++---- misc-utils/ftl_check.c | 14 ++++++++++++-- misc-utils/ftl_format.c | 22 ++++++++++++++++++---- misc-utils/mtdpart.c | 13 ++++++------- 6 files changed, 60 insertions(+), 37 deletions(-) (limited to 'misc-utils') diff --git a/misc-utils/flash_erase.c b/misc-utils/flash_erase.c index 933373a..2bca78d 100644 --- a/misc-utils/flash_erase.c +++ b/misc-utils/flash_erase.c @@ -76,9 +76,8 @@ static void display_help (void) static void display_version (void) { - printf("%1$s version " VERSION "\n" - "\n" - "Copyright (C) 2000 Arcom Control Systems Ltd\n" + common_print_version(); + printf("Copyright (C) 2000 Arcom Control Systems Ltd\n" "\n" "%1$s comes with NO WARRANTY\n" "to the extent permitted by law.\n" @@ -105,10 +104,10 @@ int main(int argc, char *argv[]) */ for (;;) { int option_index = 0; - static const char *short_options = "jNqu"; + static const char *short_options = "jNquVh"; static const struct option long_options[] = { - {"help", no_argument, 0, 0}, - {"version", no_argument, 0, 0}, + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'V'}, {"jffs2", no_argument, 0, 'j'}, {"noskipbad", no_argument, 0, 'N'}, {"quiet", no_argument, 0, 'q'}, @@ -124,16 +123,12 @@ int main(int argc, char *argv[]) break; switch (c) { - case 0: - switch (option_index) { - case 0: - display_help(); - return 0; - case 1: - display_version(); - return 0; - } - break; + case 'h': + display_help(); + return EXIT_SUCCESS; + case 'V': + display_version(); + return EXIT_SUCCESS; case 'j': jffs2 = 1; break; diff --git a/misc-utils/flash_unlock.c b/misc-utils/flash_unlock.c index f51870a..94f4761 100644 --- a/misc-utils/flash_unlock.c +++ b/misc-utils/flash_unlock.c @@ -46,7 +46,7 @@ static void usage(int status) "\n" "Options:\n" " -h --help Display this help and exit\n" - " --version Display version information and exit\n" + " -V --version Display version information and exit\n" " -i --islocked Check if flash region is locked\n" " -l --lock Lock a region of flash\n" " -u --unlock Unlock a region of flash\n" @@ -59,13 +59,13 @@ static void usage(int status) exit(status); } -static const char short_opts[] = "hilu"; +static const char short_opts[] = "hiluV"; static const struct option long_opts[] = { { "help", no_argument, 0, 'h' }, { "islocked", no_argument, 0, 'i' }, { "lock", no_argument, 0, 'l' }, { "unlock", no_argument, 0, 'u' }, - { "version", no_argument, 0, 'v' }, + { "version", no_argument, 0, 'V' }, { NULL, 0, 0, 0 }, }; @@ -101,7 +101,7 @@ static void process_args(int argc, char *argv[]) req = REQUEST_UNLOCK; req_set++; break; - case 'v': + case 'V': common_print_version(); exit(0); default: diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c index 3fddeb0..6594a45 100644 --- a/misc-utils/flashcp.c +++ b/misc-utils/flashcp.c @@ -43,9 +43,7 @@ #include #include -typedef int bool; -#define true 1 -#define false 0 +#include "common.h" #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 @@ -96,9 +94,11 @@ static void showusage(bool error) "\n" "usage: %1$s [ -v | --verbose ] \n" " %1$s -h | --help\n" + " %1$s -V | --version\n" "\n" " -h | --help Show this help message\n" " -v | --verbose Show progress reports\n" + " -V | --version Show version information and exit\n" " File which you want to copy to flash\n" " Flash device to write to (e.g. /dev/mtd0, /dev/mtd1, etc.)\n" "\n", @@ -182,10 +182,11 @@ int main (int argc,char *argv[]) for (;;) { int option_index = 0; - static const char *short_options = "hv"; + static const char *short_options = "hvV"; static const struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"verbose", no_argument, 0, 'v'}, + {"version", no_argument, 0, 'V'}, {0, 0, 0, 0}, }; @@ -204,6 +205,10 @@ int main (int argc,char *argv[]) flags |= FLAG_VERBOSE; DEBUG("Got FLAG_VERBOSE\n"); break; + case 'V': + common_print_version(); + exit(EXIT_SUCCESS); + break; default: DEBUG("Unknown parameter: %s\n",argv[option_index]); showusage(true); diff --git a/misc-utils/ftl_check.c b/misc-utils/ftl_check.c index 0eada8f..d7d2e8b 100644 --- a/misc-utils/ftl_check.c +++ b/misc-utils/ftl_check.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -175,14 +176,23 @@ void showusage(void) /*====================================================================*/ +static const struct option long_opts[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'V'}, + {0, 0, 0, 0}, +}; + int main(int argc, char *argv[]) { - int optch, errflg, fd; + int c, optch, errflg, fd; struct stat buf; errflg = 0; - while ((optch = getopt(argc, argv, "h")) != -1) { + while ((optch = getopt_long(argc, argv, "hV", long_opts, &c)) != -1) { switch (optch) { + case 'V': + common_print_version(); + exit(EXIT_SUCCESS); case 'h': errflg = 1; break; default: 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) { diff --git a/misc-utils/mtdpart.c b/misc-utils/mtdpart.c index 0016e34..e480e1b 100644 --- a/misc-utils/mtdpart.c +++ b/misc-utils/mtdpart.c @@ -34,7 +34,7 @@ static void display_help(int status) "Adds a partition to an MTD device, or remove an existing partition from it.\n" "\n" " -h, --help Display this help and exit\n" -" --version Output version information and exit\n" +" -V, --version Output version information and exit\n" "\n" "START location and SIZE of the partition are in bytes. They should align on\n" "eraseblock size.\n", @@ -45,9 +45,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" @@ -77,9 +76,9 @@ static void process_options(int argc, char * const argv[]) for (;;) { int option_index = 0; - static const char short_options[] = "h"; + static const char short_options[] = "hV"; static const struct option long_options[] = { - {"version", no_argument, 0, 0}, + {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}, }; @@ -91,7 +90,7 @@ static void process_options(int argc, char * const argv[]) } switch (c) { - case 0: + case 'V': display_version(); break; case 'h': -- cgit v1.2.3