[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 <cardoe@cardoe.com>
pull/60/head
Doug Goldstein 2016-10-05 10:51:34 -05:00
parent 26050fd4c8
commit a803c72c11
No known key found for this signature in database
GPG Key ID: A2BC03DC87ED1BD4
1 changed files with 3 additions and 1 deletions

View File

@ -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;