]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc/printf: Add support for left aligning fields
authorDavid Laight <david.laight.linux@gmail.com>
Sun, 8 Mar 2026 11:37:39 +0000 (11:37 +0000)
committerThomas Weißschuh <linux@weissschuh.net>
Fri, 20 Mar 2026 16:57:21 +0000 (17:57 +0100)
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@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260308113742.12649-15-david.laight.linux@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/stdio.h
tools/testing/selftests/nolibc/nolibc-test.c

index db714b1ce27444ef98f65f9ad0257104db5cab9b..c8a463ed73c7e9b9deb88947ed19eb4fd94169e1 100644 (file)
@@ -545,7 +545,11 @@ do_output:
                /* 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;
@@ -554,7 +558,7 @@ do_output:
                        if (cb(state, "                ", pad_len) != 0)
                                return -1;
                }
-               if (cb(state, outstr, len) != 0)
+               if (!flags && cb(state, outstr, len) != 0)
                        return -1;
        }
 
index 9644087267d135e516c1e8e99ea6e6d66eec06e8..2a83d5ea2d46475819ce6b1230164305e4d0a48c 100644 (file)
@@ -1853,9 +1853,11 @@ static int run_printf(int min, int max)
                CASE_TEST(truncation);   EXPECT_VFPRINTF(1, "0123456789012345678901234", "%s", "0123456789012345678901234"); 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", "%25d", 1); break;
                CASE_TEST(errno);        errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break;
-               CASE_TEST(errno-neg);    errno = -22; EXPECT_VFPRINTF(is_nolibc, "   errno=-22", "%12m"); break;
+               CASE_TEST(errno-neg);    errno = -22; EXPECT_VFPRINTF(is_nolibc, "errno=-22   ", "%-12m"); break;
                CASE_TEST(scanf);        EXPECT_ZR(1, test_scanf()); break;
                CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
                case __LINE__: