4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 * from hush: simple_itoa() was lifted from boa-0.93.15
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
22 #define noinline __attribute__((noinline))
24 /* we use this so that we can do without the ctype library */
25 #define is_digit(c) ((c) >= '0' && (c) <= '9')
27 static int skip_atoi(const char **s)
32 i = i * 10 + *((*s)++) - '0';
37 /* Decimal conversion is by far the most typical, and is used
38 * for /proc and /sys data. This directly impacts e.g. top performance
39 * with many processes running. We optimize it for speed
41 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
42 * (with permission from the author, Douglas W. Jones). */
44 /* Formats correctly any integer in [0,99999].
45 * Outputs from one to five digits depending on input.
46 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
47 static char *put_dec_trunc(char *buf, unsigned q)
49 unsigned d3, d2, d1, d0;
54 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
55 q = (d0 * 0xcd) >> 11;
57 *buf++ = d0 + '0'; /* least significant digit */
58 d1 = q + 9*d3 + 5*d2 + d1;
60 q = (d1 * 0xcd) >> 11;
62 *buf++ = d1 + '0'; /* next digit */
65 if ((d2 != 0) || (d3 != 0)) {
68 *buf++ = d2 + '0'; /* next digit */
72 q = (d3 * 0xcd) >> 11;
74 *buf++ = d3 + '0'; /* next digit */
76 *buf++ = q + '0'; /* most sign. digit */
82 /* Same with if's removed. Always emits five digits */
83 static char *put_dec_full(char *buf, unsigned q)
85 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
86 /* but anyway, gcc produces better code with full-sized ints */
87 unsigned d3, d2, d1, d0;
93 * Possible ways to approx. divide by 10
94 * gcc -O2 replaces multiply with shifts and adds
95 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
96 * (x * 0x67) >> 10: 1100111
97 * (x * 0x34) >> 9: 110100 - same
98 * (x * 0x1a) >> 8: 11010 - same
99 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
102 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
103 q = (d0 * 0xcd) >> 11;
106 d1 = q + 9*d3 + 5*d2 + d1;
107 q = (d1 * 0xcd) >> 11;
117 q = (d3 * 0xcd) >> 11; /* - shorter code */
118 /* q = (d3 * 0x67) >> 10; - would also work */
124 /* No inlining helps gcc to use registers better */
125 static noinline char *put_dec(char *buf, uint64_t num)
130 return put_dec_trunc(buf, num);
131 rem = do_div(num, 100000);
132 buf = put_dec_full(buf, rem);
136 #define ZEROPAD 1 /* pad with zero */
137 #define SIGN 2 /* unsigned/signed long */
138 #define PLUS 4 /* show plus */
139 #define SPACE 8 /* space if plus */
140 #define LEFT 16 /* left justified */
141 #define SMALL 32 /* Must be 32 == 0x20 */
142 #define SPECIAL 64 /* 0x */
145 * Macro to add a new character to our output string, but only if it will
146 * fit. The macro moves to the next character position in the output string.
148 #define ADDCH(str, ch) do { \
154 static char *number(char *buf, char *end, u64 num,
155 int base, int size, int precision, int type)
157 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
158 static const char digits[16] = "0123456789ABCDEF";
163 int need_pfx = ((type & SPECIAL) && base != 10);
166 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
167 * produces same digits or (maybe lowercased) letters */
168 locase = (type & SMALL);
177 } else if (type & PLUS) {
180 } else if (type & SPACE) {
191 /* generate full string in tmp[], in reverse order */
195 /* Generic code, for any base:
197 tmp[i++] = (digits[do_div(num,base)] | locase);
200 else if (base != 10) { /* 8 or 16 */
208 tmp[i++] = (digits[((unsigned char)num) & mask]
212 } else { /* base 10 */
213 i = put_dec(tmp, num) - tmp;
216 /* printing 100 using %2d gives "100", not "00" */
219 /* leading space padding */
221 if (!(type & (ZEROPAD + LEFT))) {
228 /* "0x" / "0" prefix */
232 ADDCH(buf, 'X' | locase);
234 /* zero or space padding */
235 if (!(type & LEFT)) {
236 char c = (type & ZEROPAD) ? '0' : ' ';
241 /* hmm even more zero padding? */
242 while (i <= --precision)
244 /* actual digits of result */
247 /* trailing space padding */
253 static char *string(char *buf, char *end, char *s, int field_width,
254 int precision, int flags)
261 len = strnlen(s, precision);
264 while (len < field_width--)
266 for (i = 0; i < len; ++i)
268 while (len < field_width--)
273 #ifdef CONFIG_CMD_NET
274 static const char hex_asc[] = "0123456789abcdef";
275 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
276 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
278 static inline char *pack_hex_byte(char *buf, u8 byte)
280 *buf++ = hex_asc_hi(byte);
281 *buf++ = hex_asc_lo(byte);
285 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
286 int precision, int flags)
288 /* (6 * 2 hex digits), 5 colons and trailing zero */
289 char mac_addr[6 * 3];
293 for (i = 0; i < 6; i++) {
294 p = pack_hex_byte(p, addr[i]);
295 if (!(flags & SPECIAL) && i != 5)
300 return string(buf, end, mac_addr, field_width, precision,
304 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
305 int precision, int flags)
307 /* (8 * 4 hex digits), 7 colons and trailing zero */
308 char ip6_addr[8 * 5];
312 for (i = 0; i < 8; i++) {
313 p = pack_hex_byte(p, addr[2 * i]);
314 p = pack_hex_byte(p, addr[2 * i + 1]);
315 if (!(flags & SPECIAL) && i != 7)
320 return string(buf, end, ip6_addr, field_width, precision,
324 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
325 int precision, int flags)
327 /* (4 * 3 decimal digits), 3 dots and trailing zero */
328 char ip4_addr[4 * 4];
329 char temp[3]; /* hold each IP quad in reverse order */
333 for (i = 0; i < 4; i++) {
334 digits = put_dec_trunc(temp, addr[i]) - temp;
335 /* reverse the digits in the quad */
343 return string(buf, end, ip4_addr, field_width, precision,
349 * Show a '%p' thing. A kernel extension is that the '%p' is followed
350 * by an extra set of alphanumeric characters that are extended format
353 * Right now we handle:
355 * - 'M' For a 6-byte MAC address, it prints the address in the
356 * usual colon-separated hex notation
357 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
358 * decimal for v4 and colon separated network-order 16 bit hex for v6)
359 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
362 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
363 * function pointers are really function descriptors, which contain a
364 * pointer to the real address.
366 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
367 int field_width, int precision, int flags)
369 u64 num = (uintptr_t)ptr;
372 * Being a boot loader, we explicitly allow pointers to
373 * (physical) address null.
377 return string(buf, end, "(null)", field_width, precision,
381 #ifdef CONFIG_CMD_NET
384 flags |= SPECIAL | ZEROPAD;
389 field_width = sizeof(phys_addr_t) * 2 + 2;
390 num = *(phys_addr_t *)ptr;
398 return mac_address_string(buf, end, ptr, field_width,
405 return ip6_addr_string(buf, end, ptr, field_width,
408 return ip4_addr_string(buf, end, ptr, field_width,
415 if (field_width == -1) {
416 field_width = 2*sizeof(void *);
419 return number(buf, end, num, 16, field_width, precision, flags);
422 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
429 int flags; /* flags to number() */
431 int field_width; /* width of output field */
432 int precision; /* min. # of digits for integers; max
433 number of chars for from string */
434 int qualifier; /* 'h', 'l', or 'L' for integer fields */
435 /* 'z' support added 23/7/1999 S.H. */
436 /* 'z' changed to 'Z' --davidm 1/25/99 */
437 /* 't' added for ptrdiff_t */
438 char *end = buf + size;
440 /* Make sure end is always >= buf - do we want this in U-Boot? */
447 for (; *fmt ; ++fmt) {
456 ++fmt; /* this also skips first '%' */
475 /* get field width */
478 field_width = skip_atoi(&fmt);
479 else if (*fmt == '*') {
481 /* it's the next argument */
482 field_width = va_arg(args, int);
483 if (field_width < 0) {
484 field_width = -field_width;
489 /* get the precision */
494 precision = skip_atoi(&fmt);
495 else if (*fmt == '*') {
497 /* it's the next argument */
498 precision = va_arg(args, int);
504 /* get the conversion qualifier */
506 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
507 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
510 if (qualifier == 'l' && *fmt == 'l') {
521 if (!(flags & LEFT)) {
522 while (--field_width > 0)
525 ADDCH(str, (unsigned char) va_arg(args, int));
526 while (--field_width > 0)
531 str = string(str, end, va_arg(args, char *),
532 field_width, precision, flags);
536 str = pointer(fmt + 1, str, end,
537 va_arg(args, void *),
538 field_width, precision, flags);
539 /* Skip all alphanumeric pointer suffixes */
540 while (isalnum(fmt[1]))
545 if (qualifier == 'l') {
546 long *ip = va_arg(args, long *);
549 int *ip = va_arg(args, int *);
558 /* integer number formats - set up the flags and "break" */
583 if (qualifier == 'L') /* "quad" for 64 bit variables */
584 num = va_arg(args, unsigned long long);
585 else if (qualifier == 'l') {
586 num = va_arg(args, unsigned long);
588 num = (signed long) num;
589 } else if (qualifier == 'Z' || qualifier == 'z') {
590 num = va_arg(args, size_t);
591 } else if (qualifier == 't') {
592 num = va_arg(args, ptrdiff_t);
593 } else if (qualifier == 'h') {
594 num = (unsigned short) va_arg(args, int);
596 num = (signed short) num;
598 num = va_arg(args, unsigned int);
600 num = (signed int) num;
602 str = number(str, end, num, base, field_width, precision,
612 /* the trailing null byte doesn't count towards the total */
616 int vsnprintf(char *buf, size_t size, const char *fmt,
619 return vsnprintf_internal(buf, size, fmt, args);
622 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
626 i = vsnprintf(buf, size, fmt, args);
628 if (likely(i < size))
635 int snprintf(char *buf, size_t size, const char *fmt, ...)
641 i = vsnprintf(buf, size, fmt, args);
647 int scnprintf(char *buf, size_t size, const char *fmt, ...)
653 i = vscnprintf(buf, size, fmt, args);
660 * Format a string and place it in a buffer (va_list version)
662 * @param buf The buffer to place the result into
663 * @param fmt The format string to use
664 * @param args Arguments for the format string
666 * The function returns the number of characters written
667 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
670 * If you're not already dealing with a va_list consider using sprintf().
672 int vsprintf(char *buf, const char *fmt, va_list args)
674 return vsnprintf_internal(buf, INT_MAX, fmt, args);
677 int sprintf(char *buf, const char *fmt, ...)
683 i = vsprintf(buf, fmt, args);
688 int printf(const char *fmt, ...)
692 char printbuffer[CONFIG_SYS_PBSIZE];
697 * For this to work, printbuffer must be larger than
698 * anything we ever want to print.
700 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
703 /* Print the string */
708 int vprintf(const char *fmt, va_list args)
711 char printbuffer[CONFIG_SYS_PBSIZE];
714 * For this to work, printbuffer must be larger than
715 * anything we ever want to print.
717 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
719 /* Print the string */
725 void __assert_fail(const char *assertion, const char *file, unsigned line,
726 const char *function)
728 /* This will not return */
729 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
733 char *simple_itoa(ulong i)
735 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
736 static char local[22];
737 char *p = &local[21];
747 /* We don't seem to have %'d in U-Boot */
748 void print_grouped_ull(unsigned long long int_val, int digits)
753 digits = (digits + 2) / 3;
754 sprintf(str, "%*llu", digits * 3, int_val);
755 for (s = str; *s; s += grab) {
757 putc(s[-1] != ' ' ? ',' : ' ');
758 printf("%.*s", grab, s);
763 bool str2off(const char *p, loff_t *num)
767 *num = simple_strtoull(p, &endptr, 16);
768 return *p != '\0' && *endptr == '\0';
771 bool str2long(const char *p, ulong *num)
775 *num = simple_strtoul(p, &endptr, 16);
776 return *p != '\0' && *endptr == '\0';