All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Fenkart <afenkart@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 5/5] tools: env: shift optind arguments and fix argument indices
Date: Tue, 24 Nov 2015 14:21:15 +0100	[thread overview]
Message-ID: <1448371275-900-6-git-send-email-andreas.fenkart@dev.digitalstrom.org> (raw)
In-Reply-To: <1448371275-900-1-git-send-email-andreas.fenkart@dev.digitalstrom.org>

Signed-off-by: Andreas Fenkart <andreas.fenkart@dev.digitalstrom.org>
---
 tools/env/fw_env.c      | 40 +++++++++++-----------------------------
 tools/env/fw_env_main.c |  4 ++++
 2 files changed, 15 insertions(+), 29 deletions(-)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 32bb3aa..a89bde5 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -236,15 +236,10 @@ int fw_printenv (int argc, char *argv[])
 	char *env, *nxt;
 	int i, rc = 0;
 
-	if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
-		argv += 2;
-		argc -= 2;
-	}
-
 	if (fw_env_open())
 		return -1;
 
-	if (argc == 1) {		/* Print all env variables  */
+	if (argc == 0) {		/* Print all env variables  */
 		for (env = environment.data; *env; env = nxt + 1) {
 			for (nxt = env; *nxt; ++nxt) {
 				if (nxt >= &environment.data[ENV_SIZE]) {
@@ -259,17 +254,13 @@ int fw_printenv (int argc, char *argv[])
 		return 0;
 	}
 
-	if (strcmp (argv[1], "-n") == 0) {
-		++argv;
-		--argc;
-		if (argc != 2) {
-			fprintf (stderr, "## Error: "
-				"`-n' option requires exactly one argument\n");
-			return -1;
-		}
+	if (printenv_args.name_suppress && argc != 1) {
+		fprintf(stderr,
+			"## Error: `-n' option requires exactly one argument\n");
+		return -1;
 	}
 
-	for (i = 1; i < argc; ++i) {	/* print single env variables   */
+	for (i = 0; i < argc; ++i) {	/* print single env variables   */
 		char *name = argv[i];
 		char *val = NULL;
 
@@ -466,17 +457,8 @@ int fw_setenv(int argc, char *argv[])
 	char *value = NULL;
 	int valc;
 
-	if (argc < 2) {
-		errno = EINVAL;
-		return -1;
-	}
-
-	if (strcmp(argv[1], "-a") == 0) {
-		argv += 2;
-		argc -= 2;
-	}
-
-	if (argc < 2) {
+	if (argc < 1) {
+		fprintf(stderr, "## Error: variable name missing\n");
 		errno = EINVAL;
 		return -1;
 	}
@@ -486,9 +468,9 @@ int fw_setenv(int argc, char *argv[])
 		return -1;
 	}
 
-	name = argv[1];
-	valv = argv + 2;
-	valc = argc - 2;
+	name = argv[0];
+	valv = argv + 1;
+	valc = argc - 1;
 
 	if (env_flags_validate_env_set_params(name, valv, valc) < 0)
 		return 1;
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index c828fd0..c20325f 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -158,6 +158,10 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
+	/* shift parsed flags, jump to non-option arguments */
+	argc -= optind;
+	argv += optind;
+
 	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
 	if (-1 == lockfd) {
 		fprintf(stderr, "Error opening lock file %s\n", lockname);
-- 
2.6.2

  parent reply	other threads:[~2015-11-24 13:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 13:21 [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 1/5] tools: env validate: pass values as 0-based array Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 2/5] tools: env: make parse_aes_key stateless Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 3/5] tools: env: introduce setenv/printenv argument structs Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 4/5] tools: env: parse aes key / suppress flag into argument struct Andreas Fenkart
2015-11-24 13:21 ` Andreas Fenkart [this message]
2015-11-24 19:26 ` [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Michael Heimpold

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=1448371275-900-6-git-send-email-andreas.fenkart@dev.digitalstrom.org \
    --to=afenkart@gmail.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.