[ipxe-devel] [PATCH] printf: add support for field width as an argument

Doug Goldstein cardoe at cardoe.com
Tue Oct 11 04:25:10 UTC 2016


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 at cardoe.com>
---
 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 9d3a97c..f9a6772 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;
-- 
2.7.4




More information about the ipxe-devel mailing list