[ipxe-devel] Patch: Support %u in printf() et al

Earl Chew earl_chew at yahoo.com
Thu Nov 5 15:00:32 UTC 2015



commit 0eca98e03b943e34a1322e0f64069d8d325b610c
Author: Earl Chew <earl_chew at yahoo.com>
Date:   Sun Nov 1 15:58:35 2015 -0800

     [vsprintf] Support %u in printf() et al

     Signed-off-by: Earl Chew <earl_chew at yahoo.com>

diff --git a/src/ipxe/vsprintf.c b/src/ipxe/vsprintf.c
index 5cc7231..357ac07 100644
--- a/src/ipxe/vsprintf.c
+++ b/src/ipxe/vsprintf.c
@@ -159,6 +159,39 @@ static char * format_decimal ( char *end, signed 
long num, int width,
  }

  /**
+ * Format an unsigned decimal number
+ *
+ * @v end		End of buffer to contain number
+ * @v num		Number to format
+ * @v width		Minimum field width
+ * @v flags		Format flags
+ * @ret ptr		End of buffer
+ *
+ * Fills a buffer in reverse order with a formatted decimal number.
+ * The number will be space-padded to the specified width.
+ *
+ * There must be enough space in the buffer to contain the largest
+ * number that this function can format.
+ */
+static char * format_udecimal ( char *end, signed long num, int width,
+			        int flags ) {
+	char *ptr = end;
+	int zpad = ( flags & ZPAD );
+	int pad = ( zpad | ' ' );
+
+	do {
+		*(--ptr) = '0' + ( num % 10 );
+		num /= 10;
+	} while ( num );
+
+	/* Pad to width */
+	while ( ( end - ptr ) < width )
+		*(--ptr) = pad;
+
+	return ptr;
+}
+
+/**
   * Print character via a printf context
   *
   * @v ctx		Context
@@ -285,6 +318,15 @@ size_t vcprintf ( struct printf_context *ctx, const 
char *fmt, va_list args ) {
  				decimal = va_arg ( args, signed int );
  			}
  			ptr = format_decimal ( ptr, decimal, width, flags );
+		} else if ( *fmt == 'u' ) {
+			unsigned long decimal;
+
+			if ( *length >= sizeof ( unsigned long ) ) {
+				decimal = va_arg ( args, unsigned long );
+			} else {
+				decimal = va_arg ( args, unsigned int );
+			}
+			ptr = format_udecimal ( ptr, decimal, width, flags );
  		} else {
  			*(--ptr) = *fmt;
  		}




More information about the ipxe-devel mailing list