From a803c72c116580583c2688927164c499d54d553d Mon Sep 17 00:00:00 2001 From: Doug Goldstein Date: Wed, 5 Oct 2016 10:51:34 -0500 Subject: [PATCH] [libc] printf: support field width as an argument Add support for the field width to be specified as an argument by using the '*' modifier. See the POSIX.1 specification for more details on conformance at http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html This change make the following calls equivalent: printf("%6s\n", "test"); printf("%*s\n", 6, "test"); Signed-off-by: Doug Goldstein --- src/core/vsprintf.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/vsprintf.c b/src/core/vsprintf.c index 9d3a97c2d..f9a6772b4 100644 --- a/src/core/vsprintf.c +++ b/src/core/vsprintf.c @@ -218,7 +218,9 @@ size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) { /* Process field width */ width = 0; for ( ; ; fmt++ ) { - if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) { + if ( *fmt == '*' ) { + width = va_arg(args, int); + } else if ( ( ( unsigned ) ( *fmt - '0' ) ) < 10 ) { width = ( width * 10 ) + ( *fmt - '0' ); } else { break;