All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 01/10] Add getenv_ulong() to read an integer from an environment variable
Date: Fri, 14 Oct 2011 09:48:27 -0700	[thread overview]
Message-ID: <1318610916-6975-2-git-send-email-sjg@chromium.org> (raw)

This is not an uncommon operation in U-Boot, so let's put it in a common
function.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v2:
- Fix commit title from getenv_int() to getenv_ulong()

 common/cmd_nvedit.c |    7 +++++++
 include/common.h    |   12 ++++++++++++
 2 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 101bc49..337ae9a 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -540,6 +540,13 @@ int getenv_f(const char *name, char *buf, unsigned len)
 	return -1;
 }
 
+ulong getenv_ulong(const char *name, int base, ulong default_val)
+{
+	const char *str = getenv(name);
+
+	return str ? simple_strtoul(str, NULL, base) : default_val;
+}
+
 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
 
 int do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
diff --git a/include/common.h b/include/common.h
index eb19a44..c2796e2 100644
--- a/include/common.h
+++ b/include/common.h
@@ -288,6 +288,18 @@ void	env_relocate (void);
 int	envmatch     (uchar *, int);
 char	*getenv	     (const char *);
 int	getenv_f     (const char *name, char *buf, unsigned len);
+
+/**
+ * Decode the value of an environment variable and return it.
+ *
+ * @param name		Name of environemnt variable
+ * @param base		Number base to use (normally 10, or 16 for hex)
+ * @param default_val	Default value to return if the variable is not
+ *			found
+ * @return the decoded value, or default_val if not found
+ */
+ulong getenv_ulong(const char *name, int base, ulong default_val);
+
 int	saveenv	     (void);
 #ifdef CONFIG_PPC		/* ARM version to be fixed! */
 int inline setenv    (const char *, const char *);
-- 
1.7.3.1

             reply	other threads:[~2011-10-14 16:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-14 16:48 Simon Glass [this message]
2011-10-14 17:09 ` [U-Boot] [PATCH v2 01/10] Add getenv_ulong() to read an integer from an environment variable Mike Frysinger
2011-10-14 19:36 ` Wolfgang Denk
2011-10-14 21:04 ` [U-Boot] [PATCH v3 " Simon Glass
2011-10-14 21:42   ` Wolfgang Denk
2011-10-14 23:27     ` Simon Glass
2011-10-14 23:25 ` [U-Boot] [PATCH v4 " Simon Glass
2011-10-15 17:21   ` Mike Frysinger
2011-10-23 20:49   ` Wolfgang Denk

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=1318610916-6975-2-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --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.