[PATCH v3 03/12] tools/nolibc/stdio: let perror work when NOLIBC_IGNORE_ERRNO is set

Benjamin Berg posted 12 patches 1 week ago
[PATCH v3 03/12] tools/nolibc/stdio: let perror work when NOLIBC_IGNORE_ERRNO is set
Posted by Benjamin Berg 1 week ago
From: Benjamin Berg <benjamin.berg@intel.com>

There is no errno variable when NOLIBC_IGNORE_ERRNO is defined. As such,
simply print the message with "unknown error" rather than the integer
value of errno.

Fixes: acab7bcdb1bc ("tools/nolibc/stdio: add perror() to report the errno value")
Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>

---
v3:
- Change the message instead of removing perror entirely
---
 tools/include/nolibc/stdio.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 7630234408c5..724d05ce6962 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -600,7 +600,11 @@ int sscanf(const char *str, const char *format, ...)
 static __attribute__((unused))
 void perror(const char *msg)
 {
+#ifdef NOLIBC_IGNORE_ERRNO
+	fprintf(stderr, "%s%sunknown error\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "");
+#else
 	fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
+#endif
 }
 
 static __attribute__((unused))
-- 
2.51.0
Re: [PATCH v3 03/12] tools/nolibc/stdio: let perror work when NOLIBC_IGNORE_ERRNO is set
Posted by Thomas Weißschuh 6 days, 8 hours ago
On 2025-09-24 16:20:50+0200, Benjamin Berg wrote:
> From: Benjamin Berg <benjamin.berg@intel.com>
> 
> There is no errno variable when NOLIBC_IGNORE_ERRNO is defined. As such,
> simply print the message with "unknown error" rather than the integer
> value of errno.
> 
> Fixes: acab7bcdb1bc ("tools/nolibc/stdio: add perror() to report the errno value")
> Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>

Acked-by: Thomas Weißschuh <linux@weissschuh.net>

> 
> ---
> v3:
> - Change the message instead of removing perror entirely
> ---
>  tools/include/nolibc/stdio.h | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> index 7630234408c5..724d05ce6962 100644
> --- a/tools/include/nolibc/stdio.h
> +++ b/tools/include/nolibc/stdio.h
> @@ -600,7 +600,11 @@ int sscanf(const char *str, const char *format, ...)
>  static __attribute__((unused))
>  void perror(const char *msg)
>  {
> +#ifdef NOLIBC_IGNORE_ERRNO
> +	fprintf(stderr, "%s%sunknown error\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "");
> +#else
>  	fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
> +#endif
>  }
>  
>  static __attribute__((unused))
> -- 
> 2.51.0
>