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 NUM_TYPE long long
27 #define noinline __attribute__((noinline))
29 /* some reluctance to put this into a new limits.h, so it is here */
30 #define INT_MAX ((int)(~0U>>1))
32 const char hex_asc[] = "0123456789abcdef";
33 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
34 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
36 static inline char *pack_hex_byte(char *buf, u8 byte)
38 *buf++ = hex_asc_hi(byte);
39 *buf++ = hex_asc_lo(byte);
43 unsigned long simple_strtoul(const char *cp, char **endp,
46 unsigned long result = 0;
51 if ((*cp == 'x') && isxdigit(cp[1])) {
63 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
64 ? toupper(*cp) : *cp)-'A'+10) < base) {
65 result = result*base + value;
75 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
86 val = simple_strtoul(cp, &tail, base);
90 if ((*tail == '\0') ||
91 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
99 long simple_strtol(const char *cp, char **endp, unsigned int base)
102 return -simple_strtoul(cp + 1, endp, base);
104 return simple_strtoul(cp, endp, base);
107 int ustrtoul(const char *cp, char **endp, unsigned int base)
109 unsigned long result = simple_strtoul(cp, endp, base);
120 if ((*endp)[1] == 'i') {
121 if ((*endp)[2] == 'B')
130 unsigned long long simple_strtoull(const char *cp, char **endp,
133 unsigned long long result = 0, value;
137 if ((*cp == 'x') && isxdigit(cp[1])) {
149 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
150 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
151 result = result * base + value;
161 /* we use this so that we can do without the ctype library */
162 #define is_digit(c) ((c) >= '0' && (c) <= '9')
164 static int skip_atoi(const char **s)
168 while (is_digit(**s))
169 i = i * 10 + *((*s)++) - '0';
174 /* Decimal conversion is by far the most typical, and is used
175 * for /proc and /sys data. This directly impacts e.g. top performance
176 * with many processes running. We optimize it for speed
178 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
179 * (with permission from the author, Douglas W. Jones). */
181 /* Formats correctly any integer in [0,99999].
182 * Outputs from one to five digits depending on input.
183 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
184 static char *put_dec_trunc(char *buf, unsigned q)
186 unsigned d3, d2, d1, d0;
191 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
192 q = (d0 * 0xcd) >> 11;
194 *buf++ = d0 + '0'; /* least significant digit */
195 d1 = q + 9*d3 + 5*d2 + d1;
197 q = (d1 * 0xcd) >> 11;
199 *buf++ = d1 + '0'; /* next digit */
202 if ((d2 != 0) || (d3 != 0)) {
205 *buf++ = d2 + '0'; /* next digit */
209 q = (d3 * 0xcd) >> 11;
211 *buf++ = d3 + '0'; /* next digit */
213 *buf++ = q + '0'; /* most sign. digit */
219 /* Same with if's removed. Always emits five digits */
220 static char *put_dec_full(char *buf, unsigned q)
222 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
223 /* but anyway, gcc produces better code with full-sized ints */
224 unsigned d3, d2, d1, d0;
230 * Possible ways to approx. divide by 10
231 * gcc -O2 replaces multiply with shifts and adds
232 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
233 * (x * 0x67) >> 10: 1100111
234 * (x * 0x34) >> 9: 110100 - same
235 * (x * 0x1a) >> 8: 11010 - same
236 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
239 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
240 q = (d0 * 0xcd) >> 11;
243 d1 = q + 9*d3 + 5*d2 + d1;
244 q = (d1 * 0xcd) >> 11;
254 q = (d3 * 0xcd) >> 11; /* - shorter code */
255 /* q = (d3 * 0x67) >> 10; - would also work */
261 /* No inlining helps gcc to use registers better */
262 static noinline char *put_dec(char *buf, unsigned NUM_TYPE num)
267 return put_dec_trunc(buf, num);
268 rem = do_div(num, 100000);
269 buf = put_dec_full(buf, rem);
273 #define ZEROPAD 1 /* pad with zero */
274 #define SIGN 2 /* unsigned/signed long */
275 #define PLUS 4 /* show plus */
276 #define SPACE 8 /* space if plus */
277 #define LEFT 16 /* left justified */
278 #define SMALL 32 /* Must be 32 == 0x20 */
279 #define SPECIAL 64 /* 0x */
281 #ifdef CONFIG_SYS_VSNPRINTF
283 * Macro to add a new character to our output string, but only if it will
284 * fit. The macro moves to the next character position in the output string.
286 #define ADDCH(str, ch) do { \
292 #define ADDCH(str, ch) (*(str)++ = (ch))
295 static char *number(char *buf, char *end, unsigned NUM_TYPE num,
296 int base, int size, int precision, int type)
298 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
299 static const char digits[16] = "0123456789ABCDEF";
304 int need_pfx = ((type & SPECIAL) && base != 10);
307 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
308 * produces same digits or (maybe lowercased) letters */
309 locase = (type & SMALL);
314 if ((signed NUM_TYPE) num < 0) {
316 num = -(signed NUM_TYPE) num;
318 } else if (type & PLUS) {
321 } else if (type & SPACE) {
332 /* generate full string in tmp[], in reverse order */
336 /* Generic code, for any base:
338 tmp[i++] = (digits[do_div(num,base)] | locase);
341 else if (base != 10) { /* 8 or 16 */
349 tmp[i++] = (digits[((unsigned char)num) & mask]
353 } else { /* base 10 */
354 i = put_dec(tmp, num) - tmp;
357 /* printing 100 using %2d gives "100", not "00" */
360 /* leading space padding */
362 if (!(type & (ZEROPAD + LEFT))) {
369 /* "0x" / "0" prefix */
373 ADDCH(buf, 'X' | locase);
375 /* zero or space padding */
376 if (!(type & LEFT)) {
377 char c = (type & ZEROPAD) ? '0' : ' ';
382 /* hmm even more zero padding? */
383 while (i <= --precision)
385 /* actual digits of result */
388 /* trailing space padding */
394 static char *string(char *buf, char *end, char *s, int field_width,
395 int precision, int flags)
402 len = strnlen(s, precision);
405 while (len < field_width--)
407 for (i = 0; i < len; ++i)
409 while (len < field_width--)
414 #ifdef CONFIG_CMD_NET
415 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
416 int precision, int flags)
418 /* (6 * 2 hex digits), 5 colons and trailing zero */
419 char mac_addr[6 * 3];
423 for (i = 0; i < 6; i++) {
424 p = pack_hex_byte(p, addr[i]);
425 if (!(flags & SPECIAL) && i != 5)
430 return string(buf, end, mac_addr, field_width, precision,
434 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
435 int precision, int flags)
437 /* (8 * 4 hex digits), 7 colons and trailing zero */
438 char ip6_addr[8 * 5];
442 for (i = 0; i < 8; i++) {
443 p = pack_hex_byte(p, addr[2 * i]);
444 p = pack_hex_byte(p, addr[2 * i + 1]);
445 if (!(flags & SPECIAL) && i != 7)
450 return string(buf, end, ip6_addr, field_width, precision,
454 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
455 int precision, int flags)
457 /* (4 * 3 decimal digits), 3 dots and trailing zero */
458 char ip4_addr[4 * 4];
459 char temp[3]; /* hold each IP quad in reverse order */
463 for (i = 0; i < 4; i++) {
464 digits = put_dec_trunc(temp, addr[i]) - temp;
465 /* reverse the digits in the quad */
473 return string(buf, end, ip4_addr, field_width, precision,
479 * Show a '%p' thing. A kernel extension is that the '%p' is followed
480 * by an extra set of alphanumeric characters that are extended format
483 * Right now we handle:
485 * - 'M' For a 6-byte MAC address, it prints the address in the
486 * usual colon-separated hex notation
487 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
488 * decimal for v4 and colon separated network-order 16 bit hex for v6)
489 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
492 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
493 * function pointers are really function descriptors, which contain a
494 * pointer to the real address.
496 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
497 int field_width, int precision, int flags)
500 return string(buf, end, "(null)", field_width, precision,
503 #ifdef CONFIG_CMD_NET
509 return mac_address_string(buf, end, ptr, field_width,
516 return ip6_addr_string(buf, end, ptr, field_width,
519 return ip4_addr_string(buf, end, ptr, field_width,
526 if (field_width == -1) {
527 field_width = 2*sizeof(void *);
530 return number(buf, end, (unsigned long)ptr, 16, field_width,
534 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
537 unsigned NUM_TYPE num;
541 int flags; /* flags to number() */
543 int field_width; /* width of output field */
544 int precision; /* min. # of digits for integers; max
545 number of chars for from string */
546 int qualifier; /* 'h', 'l', or 'L' for integer fields */
547 /* 'z' support added 23/7/1999 S.H. */
548 /* 'z' changed to 'Z' --davidm 1/25/99 */
549 /* 't' added for ptrdiff_t */
550 char *end = buf + size;
552 #ifdef CONFIG_SYS_VSNPRINTF
553 /* Make sure end is always >= buf - do we want this in U-Boot? */
561 for (; *fmt ; ++fmt) {
570 ++fmt; /* this also skips first '%' */
589 /* get field width */
592 field_width = skip_atoi(&fmt);
593 else if (*fmt == '*') {
595 /* it's the next argument */
596 field_width = va_arg(args, int);
597 if (field_width < 0) {
598 field_width = -field_width;
603 /* get the precision */
608 precision = skip_atoi(&fmt);
609 else if (*fmt == '*') {
611 /* it's the next argument */
612 precision = va_arg(args, int);
618 /* get the conversion qualifier */
620 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
621 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
624 if (qualifier == 'l' && *fmt == 'l') {
635 if (!(flags & LEFT)) {
636 while (--field_width > 0)
639 ADDCH(str, (unsigned char) va_arg(args, int));
640 while (--field_width > 0)
645 str = string(str, end, va_arg(args, char *),
646 field_width, precision, flags);
650 str = pointer(fmt + 1, str, end,
651 va_arg(args, void *),
652 field_width, precision, flags);
653 /* Skip all alphanumeric pointer suffixes */
654 while (isalnum(fmt[1]))
659 if (qualifier == 'l') {
660 long *ip = va_arg(args, long *);
663 int *ip = va_arg(args, int *);
672 /* integer number formats - set up the flags and "break" */
697 if (qualifier == 'L') /* "quad" for 64 bit variables */
698 num = va_arg(args, unsigned long long);
699 else if (qualifier == 'l') {
700 num = va_arg(args, unsigned long);
702 num = (signed long) num;
703 } else if (qualifier == 'Z' || qualifier == 'z') {
704 num = va_arg(args, size_t);
705 } else if (qualifier == 't') {
706 num = va_arg(args, ptrdiff_t);
707 } else if (qualifier == 'h') {
708 num = (unsigned short) va_arg(args, int);
710 num = (signed short) num;
712 num = va_arg(args, unsigned int);
714 num = (signed int) num;
716 str = number(str, end, num, base, field_width, precision,
720 #ifdef CONFIG_SYS_VSNPRINTF
729 /* the trailing null byte doesn't count towards the total */
733 #ifdef CONFIG_SYS_VSNPRINTF
734 int vsnprintf(char *buf, size_t size, const char *fmt,
737 return vsnprintf_internal(buf, size, fmt, args);
740 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
744 i = vsnprintf(buf, size, fmt, args);
746 if (likely(i < size))
753 int snprintf(char *buf, size_t size, const char *fmt, ...)
759 i = vsnprintf(buf, size, fmt, args);
765 int scnprintf(char *buf, size_t size, const char *fmt, ...)
771 i = vscnprintf(buf, size, fmt, args);
776 #endif /* CONFIG_SYS_VSNPRINT */
779 * Format a string and place it in a buffer (va_list version)
781 * @param buf The buffer to place the result into
782 * @param fmt The format string to use
783 * @param args Arguments for the format string
785 * The function returns the number of characters written
786 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
789 * If you're not already dealing with a va_list consider using sprintf().
791 int vsprintf(char *buf, const char *fmt, va_list args)
793 return vsnprintf_internal(buf, INT_MAX, fmt, args);
796 int sprintf(char *buf, const char *fmt, ...)
802 i = vsprintf(buf, fmt, args);
807 void panic(const char *fmt, ...)
814 #if defined(CONFIG_PANIC_HANG)
817 udelay(100000); /* allow messages to go out */
818 do_reset(NULL, 0, 0, NULL);
824 void __assert_fail(const char *assertion, const char *file, unsigned line,
825 const char *function)
827 /* This will not return */
828 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
832 char *simple_itoa(ulong i)
834 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
835 static char local[22];
836 char *p = &local[21];