[U-Boot] [PATCH 6/7] getenv_f() env variable exist w/o needing a buffer

York Sun yorksun at freescale.com
Fri Jan 4 19:14:04 CET 2013


From: James Yang <James.Yang at freescale.com>

getenv_f() searches the environment for a variable name and copies the
value of the variable to a buffer pointed to by one of the function's
parameters.  However, this means that the buffer needs to exist and
needs to be of sufficient length (passed as another parameter to
getenv_f()) to hold the requested variable's value, even if all that is
desired is the mere detection of the existence of the variable itself.

This patch removes the requirement that the buffer needs to exist.  If
the pointer to the buffer is set to NULL and the requested variable is
found, getenv_f() returns 1, else it returns -1.  The buffer length
parameter is ignored if the pointer is set to NULL.  The original
functionality of getenv_f() is retained (return number of bytes copied
if variable is found, -1 if not), other than being able to copy the
variable's value to the address 0.

Signed-off-by: James Yang <James.Yang at freescale.com>
---
 common/cmd_nvedit.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/common/cmd_nvedit.c b/common/cmd_nvedit.c
index 7633f0c..caa8a36 100644
--- a/common/cmd_nvedit.c
+++ b/common/cmd_nvedit.c
@@ -587,6 +587,9 @@ char *getenv(const char *name)
 
 /*
  * Look up variable from environment for restricted C runtime env.
+ * If the variable is found, return the number of bytes copied.
+ * If buf is NULL, len is ignored, and, if the variable is found, return 1.
+ * If the variable is not found, return -1.
  */
 int getenv_f(const char *name, char *buf, unsigned len)
 {
@@ -604,7 +607,11 @@ int getenv_f(const char *name, char *buf, unsigned len)
 		if (val < 0)
 			continue;
 
-		/* found; copy out */
+		/* found */
+		if (!buf)
+			return 1;
+
+		/* copy out */
 		for (n = 0; n < len; ++n, ++buf) {
 			*buf = env_get_char(val++);
 			if (*buf == '\0')
-- 
1.7.9.5




More information about the U-Boot mailing list