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 --- jffsX-utils/jffs2dump.c | 29 ++++++++++++----------------- jffsX-utils/jffs2reader.c | 18 +++++++++++++++--- jffsX-utils/mkfs.jffs2.c | 8 ++++---- jffsX-utils/sumtool.c | 9 ++++----- 4 files changed, 35 insertions(+), 29 deletions(-) (limited to 'jffsX-utils') diff --git a/jffsX-utils/jffs2dump.c b/jffsX-utils/jffs2dump.c index f8b8ac7..4b3164b 100644 --- a/jffsX-utils/jffs2dump.c +++ b/jffsX-utils/jffs2dump.c @@ -54,8 +54,8 @@ void display_help (void) { printf("Usage: %s [OPTION]... INPUTFILE\n" "Dump the contents of a binary JFFS2 image.\n\n" - " --help display this help and exit\n" - " --version display version information and exit\n" + " -h, --help display this help and exit\n" + " -V, --version display version information and exit\n" " -b, --bigendian image is big endian\n" " -l, --littleendian image is little endian\n" " -c, --content dump image contents\n" @@ -70,9 +70,8 @@ void display_help (void) 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" @@ -102,10 +101,10 @@ void process_options (int argc, char *argv[]) for (;;) { int option_index = 0; - static const char *short_options = "blce:rd:o:v"; + static const char *short_options = "blce:rd:o:vVh"; 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'}, {"bigendian", no_argument, 0, 'b'}, {"littleendian", no_argument, 0, 'l'}, {"content", no_argument, 0, 'c'}, @@ -124,15 +123,11 @@ void process_options (int argc, char *argv[]) } switch (c) { - case 0: - switch (option_index) { - case 0: - display_help(); - break; - case 1: - display_version(); - break; - } + case 'h': + display_help(); + break; + case 'V': + display_version(); break; case 'v': verbose = 1; diff --git a/jffsX-utils/jffs2reader.c b/jffsX-utils/jffs2reader.c index a62da9a..09d1d89 100644 --- a/jffsX-utils/jffs2reader.c +++ b/jffsX-utils/jffs2reader.c @@ -71,6 +71,7 @@ BUGS: #include #include #include +#include #include #include #include @@ -79,6 +80,14 @@ BUGS: #include "mtd/jffs2-user.h" #include "common.h" +static struct option long_opt[] = { + {"help", 0, NULL, 'h'}, + {"version", 0, NULL, 'V'}, + {NULL, 0, NULL, 0}, +}; + +static const char *short_opt = "rd:f:tVh"; + #define SCRATCH_SIZE (5*1024*1024) /* macro to avoid "lvalue required as left operand of assignment" error */ @@ -857,7 +866,7 @@ void catfile(char *o, size_t size, char *path, char *b, size_t bsize, int main(int argc, char **argv) { - int fd, opt, recurse = 0, want_ctime = 0; + int fd, opt, c, recurse = 0, want_ctime = 0; struct stat st; char *scratch, *dir = NULL, *file = NULL; @@ -865,7 +874,7 @@ int main(int argc, char **argv) char *buf; - while ((opt = getopt(argc, argv, "rd:f:t")) > 0) { + while ((opt = getopt_long(argc, argv, short_opt, long_opt, &c)) > 0) { switch (opt) { case 'd': dir = optarg; @@ -879,11 +888,14 @@ int main(int argc, char **argv) case 't': want_ctime++; break; + case 'V': + common_print_version(); + exit(EXIT_SUCCESS); default: fprintf(stderr, "Usage: %s [-d|-f] < path >\n", PROGRAM_NAME); - exit(EXIT_FAILURE); + exit(opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE); } } diff --git a/jffsX-utils/mkfs.jffs2.c b/jffsX-utils/mkfs.jffs2.c index b83c1ee..5446a16 100644 --- a/jffsX-utils/mkfs.jffs2.c +++ b/jffsX-utils/mkfs.jffs2.c @@ -1427,8 +1427,6 @@ static const char helptext[] = " -V, --version Display version information\n" " -i, --incremental=FILE Parse FILE and generate appendage output for it\n\n"; -static const char revtext[] = "1.60"; - int load_next_block() { int ret; @@ -1631,14 +1629,16 @@ int main(int argc, char **argv) case 'h': case '?': - errmsg_die("%s", helptext); + puts(helptext); + exit(EXIT_SUCCESS); case 'v': verbose = 1; break; case 'V': - errmsg_die("revision %s\n", revtext); + common_print_version(); + exit(EXIT_SUCCESS); case 'e': { char *next; diff --git a/jffsX-utils/sumtool.c b/jffsX-utils/sumtool.c index 886b545..0958615 100644 --- a/jffsX-utils/sumtool.c +++ b/jffsX-utils/sumtool.c @@ -113,8 +113,6 @@ static const char helptext[] = " eraseblock\n\n"; -static const char revtext[] = "$Revision: 1.9 $"; - static unsigned char ffbuf[16] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff @@ -159,14 +157,15 @@ void process_options (int argc, char **argv) break; case 'h': case '?': - errmsg_die("%s", helptext); + puts(helptext); + exit(EXIT_SUCCESS); case 'v': verbose = 1; break; case 'V': - errmsg_die("revision %.*s\n", - (int) strlen(revtext) - 13, revtext + 11); + common_print_version(); + exit(EXIT_SUCCESS); case 'e': { char *next; -- cgit v1.2.3