[PATCH v2 11/13] env: Make return value of env_get_f() behave like sprintf() on success

2021-10-13 Thread Marek Behún
From: Marek Behún 

Currently the env_get_f() function's return value behaves weirdly: it
returns the number of bytes written into `buf`, but whether this is
excluding the terminating NULL-byte or including it depends on whether
there was enough space in `buf`.

Change the function to always return the actual length of the value of
the environment variable (excluding the terminating NULL-byte) on
success. This makes it behave like sprintf().

All users of this function in U-Boot are compatible with this change.

Signed-off-by: Marek Behún 
---
 cmd/nvedit.c  | 8 +---
 include/env.h | 6 ++
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 08288fad10..8989c85d20 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -736,7 +736,7 @@ int env_get_f(const char *name, char *buf, unsigned len)
 
for (p = env; *p != '\0'; p = end + 1) {
const char *value;
-   int n;
+   int n, res;
 
for (end = p; *end != '\0'; ++end)
if (end - env >= CONFIG_ENV_SIZE)
@@ -746,11 +746,13 @@ int env_get_f(const char *name, char *buf, unsigned len)
if (value == NULL)
continue;
 
+   res = end - value;
+
/* found; copy out */
for (n = 0; n < len; ++n, ++buf) {
*buf = *value++;
if (*buf == '\0')
-   return n;
+   return res;
}
 
if (n)
@@ -759,7 +761,7 @@ int env_get_f(const char *name, char *buf, unsigned len)
printf("env_buf [%u bytes] too small for value of \"%s\"\n",
   len, name);
 
-   return n;
+   return res;
}
 
return -1;
diff --git a/include/env.h b/include/env.h
index 220ab979d9..ee5e30d036 100644
--- a/include/env.h
+++ b/include/env.h
@@ -120,10 +120,8 @@ char *from_env(const char *envvar);
  * support reading the value (slowly) and some will not.
  *
  * @varname:   Variable to look up
- * @return number of bytes written into @buf, excluding the terminating
- * NULL-byte if there was enough space in @buf, and including the
- * terminating NULL-byte if there wasn't enough space, or -1 if the
- * variable is not found
+ * @return actual length of the variable value excluding the terminating
+ * NULL-byte, or -1 if the variable is not found
  */
 int env_get_f(const char *name, char *buf, unsigned int len);
 
-- 
2.32.0



Re: [PATCH v2 11/13] env: Make return value of env_get_f() behave like sprintf() on success

2021-10-14 Thread Simon Glass
On Wed, 13 Oct 2021 at 09:46, Marek Behún  wrote:
>
> From: Marek Behún 
>
> Currently the env_get_f() function's return value behaves weirdly: it
> returns the number of bytes written into `buf`, but whether this is
> excluding the terminating NULL-byte or including it depends on whether
> there was enough space in `buf`.
>
> Change the function to always return the actual length of the value of
> the environment variable (excluding the terminating NULL-byte) on
> success. This makes it behave like sprintf().
>
> All users of this function in U-Boot are compatible with this change.
>
> Signed-off-by: Marek Behún 
> ---
>  cmd/nvedit.c  | 8 +---
>  include/env.h | 6 ++
>  2 files changed, 7 insertions(+), 7 deletions(-)

Reviewed-by: Simon Glass