linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Brian Norris <computersforpeace@gmail.com>
To: <linux-mtd@lists.infradead.org>
Cc: Brian Norris <computersforpeace@gmail.com>
Subject: [PATCH mtd-utils 05/11] flash_{un, }lock: abstract the argument positions
Date: Mon, 31 Aug 2015 15:34:26 -0700	[thread overview]
Message-ID: <1441060472-82169-6-git-send-email-computersforpeace@gmail.com> (raw)
In-Reply-To: <1441060472-82169-1-git-send-email-computersforpeace@gmail.com>

Previously, there were no options (besides stand-alone --help and
--version), so we just used fixed-position argv indexes. Let's change
that.

Also clean up the sanity checks a bit to make them more verbose and
specific.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
 flash_unlock.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/flash_unlock.c b/flash_unlock.c
index 27e7df1eac3f..ce72e49d4ba6 100644
--- a/flash_unlock.c
+++ b/flash_unlock.c
@@ -54,7 +54,8 @@ int main(int argc, char *argv[])
 	struct mtd_info_user mtdInfo;
 	struct erase_info_user mtdLockInfo;
 	int count;
-	const char *dev;
+	const char *dev, *offs_s, *count_s;
+	int arg_idx;
 
 	for (;;) {
 		int c;
@@ -76,11 +77,31 @@ int main(int argc, char *argv[])
 		}
 	}
 
-	/* Parse command line options */
-	if (argc < 2 || argc > 4)
+	arg_idx = optind;
+
+	/* Sanity checks */
+	if (argc - arg_idx < 1) {
+		errmsg("too few arguments");
+		usage(1);
+	} else if (argc - arg_idx > 3) {
+		errmsg("too many arguments");
 		usage(1);
+	}
+
+	/* First non-option argument */
+	dev = argv[arg_idx++];
 
-	dev = argv[1];
+	/* Second non-option argument */
+	if (arg_idx < argc)
+		offs_s = argv[arg_idx++];
+	else
+		offs_s = NULL;
+
+	/* Third non-option argument */
+	if (arg_idx < argc)
+		count_s = argv[arg_idx++];
+	else
+		count_s = NULL;
 
 	/* Get the device info to compare to command line sizes */
 	fd = open(dev, O_RDWR);
@@ -91,16 +112,16 @@ int main(int argc, char *argv[])
 		sys_errmsg_die("could not get mtd info: %s", dev);
 
 	/* Make sure user options are valid */
-	if (argc > 2)
-		mtdLockInfo.start = strtol(argv[2], NULL, 0);
+	if (offs_s)
+		mtdLockInfo.start = strtol(offs_s, NULL, 0);
 	else
 		mtdLockInfo.start = 0;
 	if (mtdLockInfo.start > mtdInfo.size)
 		errmsg_die("%#x is beyond device size %#x",
 			mtdLockInfo.start, mtdInfo.size);
 
-	if (argc > 3) {
-		count = strtol(argv[3], NULL, 0);
+	if (count_s) {
+		count = strtol(count_s, NULL, 0);
 		if (count == -1)
 			mtdLockInfo.length = mtdInfo.size;
 		else
-- 
2.5.0.457.gab17608

  parent reply	other threads:[~2015-08-31 22:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-31 22:34 [PATCH mtd-utils 00/11] flash_{un,}lock upgrades Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 01/11] flash_{un, }lock: nest optional parameters in help message Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 02/11] flash_{un,}lock: switch to getopt library Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 03/11] flash_{un,}lock: support --version flag Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 04/11] flash_{un,}lock: document option flags Brian Norris
2015-08-31 22:34 ` Brian Norris [this message]
2015-08-31 22:34 ` [PATCH mtd-utils 06/11] flash_{un, }lock: move args processing to its own function Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 07/11] flash_{un, }lock: support both lock/unlock in the same binary Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 08/11] flash_{un,}lock: add MEMISLOCKED support Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 09/11] flash_{un, }lock: improve strtol() error handling Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 10/11] flash_{un,}lock: don't allow "last byte + 1" Brian Norris
2015-08-31 22:34 ` [PATCH mtd-utils 11/11] flash_{un,}lock: document block count == -1 Brian Norris
2015-11-11 22:13 ` [PATCH mtd-utils 00/11] flash_{un,}lock upgrades Brian Norris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1441060472-82169-6-git-send-email-computersforpeace@gmail.com \
    --to=computersforpeace@gmail.com \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).