diff options
| author | David Laight <david.laight.linux@gmail.com> | 2026-03-08 14:37:34 +0300 |
|---|---|---|
| committer | Thomas Weißschuh <linux@weissschuh.net> | 2026-03-20 19:55:28 +0300 |
| commit | 125632871929e9bf5b0bc907c8fef2d326e4623a (patch) | |
| tree | 24fd554509943c5d478d766db7c91d2dbd8fcc7f /tools/include | |
| parent | 85f1152778f8cdc563ada12a3fc48c962b408d94 (diff) | |
| download | linux-125632871929e9bf5b0bc907c8fef2d326e4623a.tar.xz | |
tools/nolibc/printf: Add support for length modifiers tzqL and formats iX
Length modifiers t (ptrdiff_t) and z (size_t) are aliases for l (long),
q and L are 64bit the same as j (intmax).
Format i is an alias for d and X similar to x but upper case.
Supporting them is mostly just adding the relevant bit to the bit
pattern used for matching characters.
Although %X is detected the output will be lower case.
Change/add tests to use conversions i and X, and length modifiers L and ll.
Use the correct minimum value for "%Li".
Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260308113742.12649-10-david.laight.linux@gmail.com
[Thomas: Fix up testcases for musl libc]
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Diffstat (limited to 'tools/include')
| -rw-r--r-- | tools/include/nolibc/stdio.h | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index f5a176bffc93..7382b00707fb 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -291,10 +291,11 @@ int fseek(FILE *stream, long offset, int whence) } -/* printf(). Supports the following integer and string formats. - * - %[#-+ 0][width][{l,ll,j}]{c,d,u,x,p,s,m,%} +/* printf(). Supports most of the normal integer and string formats. + * - %[#-+ 0][width][{l,t,z,ll,L,j,q}]{c,d,i,u,x,X,p,s,m,%} * - %% generates a single % * - %m outputs strerror(errno). + * - %X outputs a..f the same as %x. * - The modifiers [#-+ 0] are currently ignored. * - No support for precision or variable widths. * - No support for floating point or wide characters. @@ -386,9 +387,11 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list /* Length modifier. * They miss the conversion flags characters " #+-0" so can go into flags. - * Change ll to j (both always 64bits). + * Change both L and ll to j (all always 64bit). */ - ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 'j'); + if (ch == 'L') + ch = 'j'; + ch_flag = _NOLIBC_PF_CHAR_IS_ONE_OF(ch, 'l', 't', 'z', 'j', 'q'); if (ch_flag != 0) { if (ch == 'l' && fmt[0] == 'l') { fmt++; @@ -403,20 +406,22 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list /* Numeric and pointer conversion specifiers. * * Use an explicit bound check (rather than _NOLIBC_PF_CHAR_IS_ONE_OF()) - * so ch_flag can be used later. + * so that 'X' can be allowed through. + * 'X' gets treated and 'x' because _NOLIBC_PF_FLAG() returns the same + * value for both. */ ch_flag = _NOLIBC_PF_FLAG(ch); - if ((ch >= 'a' && ch <= 'z') && - _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'u', 'x', 'p')) { + if (((ch >= 'a' && ch <= 'z') || ch == 'X') && + _NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'c', 'd', 'i', 'u', 'x', 'p')) { /* 'long' is needed for pointer conversions and ltz lengths. * A single test can be used provided 'p' (the same bit as '0') * is masked from flags. */ if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag | (flags & ~_NOLIBC_PF_FLAG('p')), - 'p', 'l')) { + 'p', 'l', 't', 'z')) { v = va_arg(args, unsigned long); signed_v = (long)v; - } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j')) { + } else if (_NOLIBC_PF_FLAGS_CONTAIN(flags, 'j', 'q')) { v = va_arg(args, unsigned long long); signed_v = v; } else { @@ -434,7 +439,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list out = outbuf; - if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd')) { + if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i')) { /* "%d" and "%i" - signed decimal numbers. */ if (signed_v < 0) { *out++ = '-'; @@ -444,7 +449,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, void *state, const char *fmt, va_list } /* Convert the number to ascii in the required base. */ - if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'u')) { + if (_NOLIBC_PF_FLAGS_CONTAIN(ch_flag, 'd', 'i', 'u')) { /* Base 10 */ u64toa_r(v, out); } else { |
