Re: [PATCH v2 next 07/11] tools/nolibc/printf: Add support for conversion flags "#- +" and format "%X"
From: Thomas Weißschuh
Date: Mon Feb 16 2026 - 15:11:27 EST
On 2026-02-06 19:11:17+0000, david.laight.linux@xxxxxxxxx wrote:
> From: David Laight <david.laight.linux@xxxxxxxxx>
>
> Add support for all the normal flag chacacters except '0' (zero pad).
> '-' left alignment.
> '+' and ' ' Sign characters for non-negative numbers.
> '#' adds 0x to hex numbers.
> Partially support "%X", outputs lower case a..f the same as "%x".
>
> Move the "%s" code in with the numeric formats to save a va_arg() call.
>
> Prepend the sign (or "0x") after conversion to ascii and use the length
> returned by u64toh_r() and u64toa_r().
> Both needed for precision and zero-padding in the next patch.
>
> Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
> ---
>
> Changes for v2:
> - Add support for left justifying fields (removed from patch 6).
> - Merge in the changes from the old patch 8.
> - Add support for "%#x" (formally part of patch 9).
>
> tools/include/nolibc/stdio.h | 97 ++++++++++++++++++++++++++----------
> 1 file changed, 71 insertions(+), 26 deletions(-)
(...)
> @@ -365,40 +384,62 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list
> goto do_output;
> }
>
> + if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 's')) {
> + /* "%s" - character string. */
> + if (!v) {
> + outstr = "(null)";
> + len = 6;
> + goto do_output;
> + }
> + outstr = (void *)v;
When building for i386:
In file included from ./nolibc.h:123,
from ./ctype.h:8,
from <command-line>:
./stdio.h: In function '__nolibc_printf':
./stdio.h:445:50: error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]
445 | outstr = (void *)v;
|
Needs a cast through (uintptr_t). Also instead of casting through
(void *), please specify the actual target type that is expected.
> +do_strnlen_output:
> + len = strnlen(outstr, INT_MAX);
> + goto do_output;
> + }
(...)