Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: implement width padding in printf()

printf can pad each argument to a certain width.
Implement this for compatibility with the kselftest harness.
Currently only padding with spaces is supported.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>

authored by

Thomas Weißschuh and committed by
Thomas Weißschuh
e90ce42e b0bd7760

+19 -1
+16 -1
tools/include/nolibc/stdio.h
··· 220 220 { 221 221 char escape, lpref, c; 222 222 unsigned long long v; 223 - unsigned int written; 223 + unsigned int written, width; 224 224 size_t len, ofs, w; 225 225 char tmpbuf[21]; 226 226 const char *outstr; ··· 228 228 written = ofs = escape = lpref = 0; 229 229 while (1) { 230 230 c = fmt[ofs++]; 231 + width = 0; 231 232 232 233 if (escape) { 233 234 /* we're in an escape sequence, ofs == 1 */ 234 235 escape = 0; 236 + 237 + /* width */ 238 + while (c >= '0' && c <= '9') { 239 + width *= 10; 240 + width += c - '0'; 241 + 242 + c = fmt[ofs++]; 243 + } 244 + 235 245 if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') { 236 246 char *out = tmpbuf; 237 247 ··· 319 309 if (n) { 320 310 w = len < n ? len : n; 321 311 n -= w; 312 + while (width-- > w) { 313 + if (cb(state, " ", 1) != 0) 314 + break; 315 + written += 1; 316 + } 322 317 if (cb(state, outstr, w) != 0) 323 318 break; 324 319 }
+3
tools/testing/selftests/nolibc/nolibc-test.c
··· 1414 1414 CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break; 1415 1415 CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break; 1416 1416 CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break; 1417 + CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break; 1418 + CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break; 1419 + CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break; 1417 1420 CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; 1418 1421 case __LINE__: 1419 1422 return ret; /* must be last */