]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc/printf: Output pad characters in 16 byte chunks
authorDavid Laight <david.laight.linux@gmail.com>
Sun, 8 Mar 2026 11:37:30 +0000 (11:37 +0000)
committerThomas Weißschuh <linux@weissschuh.net>
Fri, 20 Mar 2026 16:46:09 +0000 (17:46 +0100)
Simple to do and saves calls to the callback function.

Change variables written, width and len to 'signed int' to get
better code.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260308113742.12649-6-david.laight.linux@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/stdio.h

index 985e7f0834bf13ddbbf55203a331928a8363fbc9..bdecc703511c2af26e8e4735b348916d75c23436 100644 (file)
@@ -312,8 +312,8 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
 {
        char escape, lpref, ch;
        unsigned long long v;
-       unsigned int written, width;
-       size_t len, ofs;
+       int written, width, len;
+       size_t ofs;
        char outbuf[21];
        const char *outstr;
 
@@ -415,10 +415,14 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
                        outstr = fmt;
                        len = ofs - 1;
                flush_str:
-                       while (width-- > len) {
-                               if (cb(state, " ", 1) != 0)
+                       width -= len;
+                       while (width > 0) {
+                               /* Output pad in 16 byte blocks with the small block first. */
+                               int pad_len = ((width - 1) & 15) + 1;
+                               width -= pad_len;
+                               written += pad_len;
+                               if (cb(state, "                ", pad_len) != 0)
                                        return -1;
-                               written += 1;
                        }
                        if (cb(state, outstr, len) != 0)
                                return -1;