[PATCH v3 next 14/17] tools/nolibc/printf: Add support for left aligning fields
From: david . laight . linux
Date: Mon Feb 23 2026 - 05:40:55 EST
From: David Laight <david.laight.linux@xxxxxxxxx>
Output the characters before or after the pad - writing the pad takes more code.
Include additional/changed tests
Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
---
Changes for v3:
- Formally part of patch 7.
tools/include/nolibc/stdio.h | 6 +++++-
tools/testing/selftests/nolibc/nolibc-test.c | 4 +++-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index c21abe085fa6..482e5b143c86 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -540,7 +540,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
/* Stop gcc back-merging this code into one of the conditionals above. */
_NOLIBC_OPTIMIZER_HIDE_VAR(len);
+ /* Output the characters on the required side of any padding. */
width -= len;
+ flags = _NOLIBC_PF_FLAGS_CONTAIN(flags, '-');
+ if (flags && cb(state, outstr, len) != 0)
+ return -1;
while (width > 0) {
/* Output pad in 16 byte blocks with the small block first. */
int pad_len = ((width - 1) & 15) + 1;
@@ -549,7 +553,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
if (cb(state, " ", pad_len) != 0)
return -1;
}
- if (cb(state, outstr, len) != 0)
+ if (!flags && cb(state, outstr, len) != 0)
return -1;
}
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index f746711af777..cc59c0116855 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1851,9 +1851,11 @@ static int run_printf(int min, int max)
CASE_TEST(truncation); EXPECT_VFPRINTF(1, "012345678901234567890123456789", "%s", "012345678901234567890123456789"); break;
CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break;
CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break;
+ CASE_TEST(number_left); EXPECT_VFPRINTF(1, "|-5 |", "|%-8d|", -5); break;
+ CASE_TEST(string_align); EXPECT_VFPRINTF(1, "|foo |", "|%-8s|", "foo"); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(1, " 1", "%30d", 1); break;
CASE_TEST(hex_alt); EXPECT_VFPRINTF(1, "|0x1| 0x2| 0|", "|%#x|%#5x|%#5x|", 1, 2, 0); break;
- CASE_TEST(errno); EXPECT_VFPRINTF(is_nolibc, "22:errno=22", "%d:%m", errno=22); break;
+ CASE_TEST(errno); EXPECT_VFPRINTF(is_nolibc, "22:errno=22 ", "%d:%-12m", errno=22); break;
CASE_TEST(errno-neg); EXPECT_VFPRINTF(is_nolibc, "-22: errno=-22", "%d:%12m", errno=-22); break;
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
--
2.39.5