aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Norris <computersforpeace@gmail.com>2015-08-27 14:45:48 -0700
committerBrian Norris <computersforpeace@gmail.com>2015-11-11 14:05:36 -0800
commit5b906ca06bd08fffb50afb64c68e161556bbfcbd (patch)
tree37d3545abce7e26ba9a208382749837bb84aa987
parentc446c257406f51691dd289fa58e476c81c8d6ddd (diff)
flash_{un,}lock: abstract the argument positions
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>
-rw-r--r--flash_unlock.c37
1 files changed, 29 insertions, 8 deletions
diff --git a/flash_unlock.c b/flash_unlock.c
index 27e7df1..ce72e49 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