This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 49b39c2f4cb1ee3c6162885cfffb8b193087fe8b
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 5 12:06:49 2026 +0100

    libs/libc/stream: Add an option for the - flag in printf().
    
    With LIBC_PRINTF_LEFT_JUSTIFY off, the flag is parsed and ignored, and the
    output is padded on the left.
    
    The option is on in all profiles.
    
    Part of https://github.com/apache/nuttx/issues/21467
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/stdio/Kconfig            | 7 +++++++
 libs/libc/stream/lib_libvsprintf.c | 8 ++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/libs/libc/stdio/Kconfig b/libs/libc/stdio/Kconfig
index 53141430d3f..6727c0dbb97 100644
--- a/libs/libc/stdio/Kconfig
+++ b/libs/libc/stdio/Kconfig
@@ -108,6 +108,13 @@ config LIBC_PRINTF_OCTAL
        ---help---
                Without it, printf() reads the argument of %o and prints "?".
 
+config LIBC_PRINTF_LEFT_JUSTIFY
+       bool "Enable the - flag in printf"
+       default y
+       ---help---
+               Enables left-justified output, as in %-5d.  Without it, printf()
+               ignores the flag and pads on the left.
+
 config LIBC_FLOATINGPOINT
        bool "Enable floating point in printf"
        default LIBC_PRINTF_FULL || (!DEFAULT_SMALL && ARCH_FPU && 
!LIBC_PRINTF_MINIMAL)
diff --git a/libs/libc/stream/lib_libvsprintf.c 
b/libs/libc/stream/lib_libvsprintf.c
index aabe98e0beb..3572073a8dd 100644
--- a/libs/libc/stream/lib_libvsprintf.c
+++ b/libs/libc/stream/lib_libvsprintf.c
@@ -73,7 +73,11 @@
 #define FL_ZFILL           0x0001
 #define FL_PLUS            0x0002
 #define FL_SPACE           0x0004
-#define FL_LPAD            0x0008
+#ifdef CONFIG_LIBC_PRINTF_LEFT_JUSTIFY
+#  define FL_LPAD          0x0008
+#else
+#  define FL_LPAD          0      /* The compiler removes its code */
+#endif
 #define FL_ALT             0x0010
 
 #define FL_ARGNUMBER       0x0020
@@ -1723,7 +1727,7 @@ tail:
 
       /* Tail is possible.  */
 
-      if (width > 0)
+      if (FL_LPAD != 0 && width > 0)
         {
           stream_repeat(' ', width, stream);
         }

Reply via email to