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>
21 #if !defined(CONFIG_PANIC_HANG)
26 #define noinline __attribute__((noinline))
28 /* some reluctance to put this into a new limits.h, so it is here */
29 #define INT_MAX ((int)(~0U>>1))
31 static const char hex_asc[] = "0123456789abcdef";
32 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
33 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
35 static inline char *pack_hex_byte(char *buf, u8 byte)
37 *buf++ = hex_asc_hi(byte);
38 *buf++ = hex_asc_lo(byte);
42 unsigned long simple_strtoul(const char *cp, char **endp,
45 unsigned long result = 0;
50 if ((*cp == 'x') && isxdigit(cp[1])) {
62 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
63 ? toupper(*cp) : *cp)-'A'+10) < base) {
64 result = result*base + value;
74 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
85 val = simple_strtoul(cp, &tail, base);
89 if ((*tail == '\0') ||
90 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
98 long simple_strtol(const char *cp, char **endp, unsigned int base)
101 return -simple_strtoul(cp + 1, endp, base);
103 return simple_strtoul(cp, endp, base);
106 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
108 unsigned long result = simple_strtoul(cp, endp, base);
119 if ((*endp)[1] == 'i') {
120 if ((*endp)[2] == 'B')
129 unsigned long long simple_strtoull(const char *cp, char **endp,
132 unsigned long long result = 0, value;
136 if ((*cp == 'x') && isxdigit(cp[1])) {
148 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
149 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
150 result = result * base + value;
160 /* we use this so that we can do without the ctype library */
161 #define is_digit(c) ((c) >= '0' && (c) <= '9')
163 static int skip_atoi(const char **s)
167 while (is_digit(**s))
168 i = i * 10 + *((*s)++) - '0';
173 /* Decimal conversion is by far the most typical, and is used
174 * for /proc and /sys data. This directly impacts e.g. top performance
175 * with many processes running. We optimize it for speed
177 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
178 * (with permission from the author, Douglas W. Jones). */
180 /* Formats correctly any integer in [0,99999].
181 * Outputs from one to five digits depending on input.
182 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
183 static char *put_dec_trunc(char *buf, unsigned q)
185 unsigned d3, d2, d1, d0;
190 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
191 q = (d0 * 0xcd) >> 11;
193 *buf++ = d0 + '0'; /* least significant digit */
194 d1 = q + 9*d3 + 5*d2 + d1;
196 q = (d1 * 0xcd) >> 11;
198 *buf++ = d1 + '0'; /* next digit */
201 if ((d2 != 0) || (d3 != 0)) {
204 *buf++ = d2 + '0'; /* next digit */
208 q = (d3 * 0xcd) >> 11;
210 *buf++ = d3 + '0'; /* next digit */
212 *buf++ = q + '0'; /* most sign. digit */
218 /* Same with if's removed. Always emits five digits */
219 static char *put_dec_full(char *buf, unsigned q)
221 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
222 /* but anyway, gcc produces better code with full-sized ints */
223 unsigned d3, d2, d1, d0;
229 * Possible ways to approx. divide by 10
230 * gcc -O2 replaces multiply with shifts and adds
231 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
232 * (x * 0x67) >> 10: 1100111
233 * (x * 0x34) >> 9: 110100 - same
234 * (x * 0x1a) >> 8: 11010 - same
235 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
238 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
239 q = (d0 * 0xcd) >> 11;
242 d1 = q + 9*d3 + 5*d2 + d1;
243 q = (d1 * 0xcd) >> 11;
253 q = (d3 * 0xcd) >> 11; /* - shorter code */
254 /* q = (d3 * 0x67) >> 10; - would also work */
260 /* No inlining helps gcc to use registers better */
261 static noinline char *put_dec(char *buf, u64 num)
266 return put_dec_trunc(buf, num);
267 rem = do_div(num, 100000);
268 buf = put_dec_full(buf, rem);
272 #define ZEROPAD 1 /* pad with zero */
273 #define SIGN 2 /* unsigned/signed long */
274 #define PLUS 4 /* show plus */
275 #define SPACE 8 /* space if plus */
276 #define LEFT 16 /* left justified */
277 #define SMALL 32 /* Must be 32 == 0x20 */
278 #define SPECIAL 64 /* 0x */
280 #ifdef CONFIG_SYS_VSNPRINTF
282 * Macro to add a new character to our output string, but only if it will
283 * fit. The macro moves to the next character position in the output string.
285 #define ADDCH(str, ch) do { \
291 #define ADDCH(str, ch) (*(str)++ = (ch))
294 static char *number(char *buf, char *end, u64 num,
295 int base, int size, int precision, int type)
297 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
298 static const char digits[16] = "0123456789ABCDEF";
303 int need_pfx = ((type & SPECIAL) && base != 10);
306 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
307 * produces same digits or (maybe lowercased) letters */
308 locase = (type & SMALL);
317 } else if (type & PLUS) {
320 } else if (type & SPACE) {
331 /* generate full string in tmp[], in reverse order */
335 /* Generic code, for any base:
337 tmp[i++] = (digits[do_div(num,base)] | locase);
340 else if (base != 10) { /* 8 or 16 */
348 tmp[i++] = (digits[((unsigned char)num) & mask]
352 } else { /* base 10 */
353 i = put_dec(tmp, num) - tmp;
356 /* printing 100 using %2d gives "100", not "00" */
359 /* leading space padding */
361 if (!(type & (ZEROPAD + LEFT))) {
368 /* "0x" / "0" prefix */
372 ADDCH(buf, 'X' | locase);
374 /* zero or space padding */
375 if (!(type & LEFT)) {
376 char c = (type & ZEROPAD) ? '0' : ' ';
381 /* hmm even more zero padding? */
382 while (i <= --precision)
384 /* actual digits of result */
387 /* trailing space padding */
393 static char *string(char *buf, char *end, char *s, int field_width,
394 int precision, int flags)
401 len = strnlen(s, precision);
404 while (len < field_width--)
406 for (i = 0; i < len; ++i)
408 while (len < field_width--)
413 #ifdef CONFIG_CMD_NET
414 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
415 int precision, int flags)
417 /* (6 * 2 hex digits), 5 colons and trailing zero */
418 char mac_addr[6 * 3];
422 for (i = 0; i < 6; i++) {
423 p = pack_hex_byte(p, addr[i]);
424 if (!(flags & SPECIAL) && i != 5)
429 return string(buf, end, mac_addr, field_width, precision,
433 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
434 int precision, int flags)
436 /* (8 * 4 hex digits), 7 colons and trailing zero */
437 char ip6_addr[8 * 5];
441 for (i = 0; i < 8; i++) {
442 p = pack_hex_byte(p, addr[2 * i]);
443 p = pack_hex_byte(p, addr[2 * i + 1]);
444 if (!(flags & SPECIAL) && i != 7)
449 return string(buf, end, ip6_addr, field_width, precision,
453 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
454 int precision, int flags)
456 /* (4 * 3 decimal digits), 3 dots and trailing zero */
457 char ip4_addr[4 * 4];
458 char temp[3]; /* hold each IP quad in reverse order */
462 for (i = 0; i < 4; i++) {
463 digits = put_dec_trunc(temp, addr[i]) - temp;
464 /* reverse the digits in the quad */
472 return string(buf, end, ip4_addr, field_width, precision,
478 * Show a '%p' thing. A kernel extension is that the '%p' is followed
479 * by an extra set of alphanumeric characters that are extended format
482 * Right now we handle:
484 * - 'M' For a 6-byte MAC address, it prints the address in the
485 * usual colon-separated hex notation
486 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
487 * decimal for v4 and colon separated network-order 16 bit hex for v6)
488 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
491 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
492 * function pointers are really function descriptors, which contain a
493 * pointer to the real address.
495 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
496 int field_width, int precision, int flags)
499 * Being a boot loader, we explicitly allow pointers to
500 * (physical) address null.
504 return string(buf, end, "(null)", field_width, precision,
508 #ifdef CONFIG_CMD_NET
514 return mac_address_string(buf, end, ptr, field_width,
521 return ip6_addr_string(buf, end, ptr, field_width,
524 return ip4_addr_string(buf, end, ptr, field_width,
531 if (field_width == -1) {
532 field_width = 2*sizeof(void *);
535 return number(buf, end, (unsigned long)ptr, 16, field_width,
539 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
546 int flags; /* flags to number() */
548 int field_width; /* width of output field */
549 int precision; /* min. # of digits for integers; max
550 number of chars for from string */
551 int qualifier; /* 'h', 'l', or 'L' for integer fields */
552 /* 'z' support added 23/7/1999 S.H. */
553 /* 'z' changed to 'Z' --davidm 1/25/99 */
554 /* 't' added for ptrdiff_t */
555 char *end = buf + size;
557 #ifdef CONFIG_SYS_VSNPRINTF
558 /* Make sure end is always >= buf - do we want this in U-Boot? */
566 for (; *fmt ; ++fmt) {
575 ++fmt; /* this also skips first '%' */
594 /* get field width */
597 field_width = skip_atoi(&fmt);
598 else if (*fmt == '*') {
600 /* it's the next argument */
601 field_width = va_arg(args, int);
602 if (field_width < 0) {
603 field_width = -field_width;
608 /* get the precision */
613 precision = skip_atoi(&fmt);
614 else if (*fmt == '*') {
616 /* it's the next argument */
617 precision = va_arg(args, int);
623 /* get the conversion qualifier */
625 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
626 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
629 if (qualifier == 'l' && *fmt == 'l') {
640 if (!(flags & LEFT)) {
641 while (--field_width > 0)
644 ADDCH(str, (unsigned char) va_arg(args, int));
645 while (--field_width > 0)
650 str = string(str, end, va_arg(args, char *),
651 field_width, precision, flags);
655 str = pointer(fmt + 1, str, end,
656 va_arg(args, void *),
657 field_width, precision, flags);
658 /* Skip all alphanumeric pointer suffixes */
659 while (isalnum(fmt[1]))
664 if (qualifier == 'l') {
665 long *ip = va_arg(args, long *);
668 int *ip = va_arg(args, int *);
677 /* integer number formats - set up the flags and "break" */
702 if (qualifier == 'L') /* "quad" for 64 bit variables */
703 num = va_arg(args, unsigned long long);
704 else if (qualifier == 'l') {
705 num = va_arg(args, unsigned long);
707 num = (signed long) num;
708 } else if (qualifier == 'Z' || qualifier == 'z') {
709 num = va_arg(args, size_t);
710 } else if (qualifier == 't') {
711 num = va_arg(args, ptrdiff_t);
712 } else if (qualifier == 'h') {
713 num = (unsigned short) va_arg(args, int);
715 num = (signed short) num;
717 num = va_arg(args, unsigned int);
719 num = (signed int) num;
721 str = number(str, end, num, base, field_width, precision,
725 #ifdef CONFIG_SYS_VSNPRINTF
734 /* the trailing null byte doesn't count towards the total */
738 #ifdef CONFIG_SYS_VSNPRINTF
739 int vsnprintf(char *buf, size_t size, const char *fmt,
742 return vsnprintf_internal(buf, size, fmt, args);
745 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
749 i = vsnprintf(buf, size, fmt, args);
751 if (likely(i < size))
758 int snprintf(char *buf, size_t size, const char *fmt, ...)
764 i = vsnprintf(buf, size, fmt, args);
770 int scnprintf(char *buf, size_t size, const char *fmt, ...)
776 i = vscnprintf(buf, size, fmt, args);
781 #endif /* CONFIG_SYS_VSNPRINT */
784 * Format a string and place it in a buffer (va_list version)
786 * @param buf The buffer to place the result into
787 * @param fmt The format string to use
788 * @param args Arguments for the format string
790 * The function returns the number of characters written
791 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
794 * If you're not already dealing with a va_list consider using sprintf().
796 int vsprintf(char *buf, const char *fmt, va_list args)
798 return vsnprintf_internal(buf, INT_MAX, fmt, args);
801 int sprintf(char *buf, const char *fmt, ...)
807 i = vsprintf(buf, fmt, args);
812 void panic(const char *fmt, ...)
819 #if defined(CONFIG_PANIC_HANG)
822 udelay(100000); /* allow messages to go out */
823 do_reset(NULL, 0, 0, NULL);
829 void __assert_fail(const char *assertion, const char *file, unsigned line,
830 const char *function)
832 /* This will not return */
833 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
837 char *simple_itoa(ulong i)
839 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
840 static char local[22];
841 char *p = &local[21];