From patchwork Thu May 9 17:59:21 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 242811 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:770:15f::2]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 96FE82C00BA for ; Fri, 10 May 2013 04:00:51 +1000 (EST) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV8k-00050P-Je; Thu, 09 May 2013 18:00:27 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV8V-0000ej-Eh; Thu, 09 May 2013 18:00:11 +0000 Received: from dev.gentoo.org ([2001:470:ea4a:1:214:c2ff:fe64:b2d3] helo=smtp.gentoo.org) by merlin.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1UaV8T-0000cA-31 for linux-mtd@lists.infradead.org; Thu, 09 May 2013 18:00:10 +0000 Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id F422033DFF1 for ; Thu, 9 May 2013 17:59:23 +0000 (UTC) From: Mike Frysinger To: linux-mtd@lists.infradead.org Subject: [PATCH mtd-utils 1/4 v2] nand{dump, test, write}: clean up --help handling Date: Thu, 9 May 2013 13:59:21 -0400 Message-Id: <1368122364-9754-1-git-send-email-vapier@gentoo.org> X-Mailer: git-send-email 1.8.2.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130509_140009_581317_699AFCFE X-CRM114-Status: GOOD ( 12.59 ) X-Spam-Score: -3.1 (---) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-3.1 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -1.2 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org We should send the output to stdout when the user passes -h/--help and then exit(0), but otherwise the output should go to stderr and then exit(1). Signed-off-by: Mike Frysinger --- v2 - no changes from v1 nanddump.c | 24 ++++++++++++------------ nandtest.c | 16 ++++++++++------ nandwrite.c | 26 +++++++++++++------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/nanddump.c b/nanddump.c index 4b3e14d..4ee7ed4 100644 --- a/nanddump.c +++ b/nanddump.c @@ -33,13 +33,13 @@ #include "common.h" #include -static void display_help(void) +static void display_help(int status) { - printf( + fprintf(status == EXIT_SUCCESS ? stdout : stderr, "Usage: %s [OPTIONS] MTD-device\n" "Dumps the contents of a nand mtd partition.\n" "\n" -" --help Display this help and exit\n" +"-h --help Display this help and exit\n" " --version Output version information and exit\n" " --bb=METHOD Choose bad block handling method (see below).\n" "-a --forcebinary Force printing of binary data to tty\n" @@ -58,7 +58,7 @@ static void display_help(void) " dumpbad: dump flash data, including any bad blocks\n" " skipbad: dump good data, completely skipping any bad blocks (default)\n", PROGRAM_NAME); - exit(EXIT_SUCCESS); + exit(status); } static void display_version(void) @@ -101,12 +101,12 @@ static void process_options(int argc, char * const argv[]) for (;;) { int option_index = 0; - static const char *short_options = "s:f:l:opqnca"; + static const char short_options[] = "hs:f:l:opqnca"; static const struct option long_options[] = { - {"help", no_argument, 0, 0}, {"version", no_argument, 0, 0}, {"bb", required_argument, 0, 0}, {"omitoob", no_argument, 0, 0}, + {"help", no_argument, 0, 'h'}, {"forcebinary", no_argument, 0, 'a'}, {"canonicalprint", no_argument, 0, 'c'}, {"file", required_argument, 0, 'f'}, @@ -129,12 +129,9 @@ static void process_options(int argc, char * const argv[]) case 0: switch (option_index) { case 0: - display_help(); - break; - case 1: display_version(); break; - case 2: + case 1: /* Handle --bb=METHOD */ if (!strcmp(optarg, "padbad")) bb_method = padbad; @@ -145,7 +142,7 @@ static void process_options(int argc, char * const argv[]) else error++; break; - case 3: /* --omitoob */ + case 2: /* --omitoob */ if (oob_default) { oob_default = false; omitoob = true; @@ -186,6 +183,9 @@ static void process_options(int argc, char * const argv[]) case 'n': noecc = true; break; + case 'h': + display_help(EXIT_SUCCESS); + break; case '?': error++; break; @@ -213,7 +213,7 @@ static void process_options(int argc, char * const argv[]) } if ((argc - optind) != 1 || error) - display_help(); + display_help(EXIT_FAILURE); mtddev = argv[optind]; } diff --git a/nandtest.c b/nandtest.c index 3437b57..1876bb2 100644 --- a/nandtest.c +++ b/nandtest.c @@ -16,9 +16,10 @@ #include #include "mtd/mtd-user.h" -void usage(void) +void usage(int status) { - fprintf(stderr, "usage: %s [OPTIONS] \n\n" + fprintf(status ? stderr : stdout, + "usage: %s [OPTIONS] \n\n" " -h, --help Display this help output\n" " -m, --markbad Mark blocks bad if they appear so\n" " -s, --seed Supply random seed\n" @@ -27,7 +28,7 @@ void usage(void) " -l, --length Length of flash to test\n" " -k, --keep Restore existing contents after test\n", PROGRAM_NAME); - exit(1); + exit(status); } struct mtd_info_user meminfo; @@ -142,7 +143,7 @@ int main(int argc, char **argv) seed = time(NULL); for (;;) { - static const char *short_options="hkl:mo:p:s:"; + static const char short_options[] = "hkl:mo:p:s:"; static const struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "markbad", no_argument, 0, 'm' }, @@ -160,8 +161,11 @@ int main(int argc, char **argv) switch (c) { case 'h': + usage(0); + break; + case '?': - usage(); + usage(1); break; case 'm': @@ -191,7 +195,7 @@ int main(int argc, char **argv) } } if (argc - optind != 1) - usage(); + usage(1); fd = open(argv[optind], O_RDWR); if (fd < 0) { diff --git a/nandwrite.c b/nandwrite.c index a6b6581..edf9f83 100644 --- a/nandwrite.c +++ b/nandwrite.c @@ -42,9 +42,9 @@ #include "common.h" #include -static void display_help(void) +static void display_help(int status) { - printf( + fprintf(status == EXIT_SUCCESS ? stdout : stderr, "Usage: nandwrite [OPTION] MTD_DEVICE [INPUTFILE|-]\n" "Writes to the specified MTD device.\n" "\n" @@ -58,10 +58,10 @@ static void display_help(void) " -p, --pad Pad to page size\n" " -b, --blockalign=1|2|4 Set multiple of eraseblocks to align to\n" " -q, --quiet Don't display progress messages\n" -" --help Display this help and exit\n" +" -h, --help Display this help and exit\n" " --version Output version information and exit\n" ); - exit(EXIT_SUCCESS); + exit(status); } static void display_version(void) @@ -99,10 +99,10 @@ static void process_options(int argc, char * const argv[]) for (;;) { int option_index = 0; - static const char *short_options = "b:mnNoOpqs:a"; + static const char short_options[] = "hb:mnNoOpqs:a"; static const struct option long_options[] = { - {"help", no_argument, 0, 0}, {"version", no_argument, 0, 0}, + {"help", no_argument, 0, 'h'}, {"blockalign", required_argument, 0, 'b'}, {"markbad", no_argument, 0, 'm'}, {"noecc", no_argument, 0, 'n'}, @@ -124,12 +124,9 @@ static void process_options(int argc, char * const argv[]) switch (c) { case 0: switch (option_index) { - case 0: - display_help(); - break; - case 1: - display_version(); - break; + case 0: /* --version */ + display_version(); + break; } break; case 'q': @@ -163,6 +160,9 @@ static void process_options(int argc, char * const argv[]) case 'a': autoplace = true; break; + case 'h': + display_help(EXIT_SUCCESS); + break; case '?': error++; break; @@ -192,7 +192,7 @@ static void process_options(int argc, char * const argv[]) */ if (argc < 1 || argc > 2 || error) - display_help(); + display_help(EXIT_FAILURE); mtd_device = argv[0];