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,unsigned int base)
45 unsigned long result = 0,value;
49 if ((*cp == 'x') && isxdigit(cp[1])) {
60 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
61 ? toupper(*cp) : *cp)-'A'+10) < base) {
62 result = result*base + value;
71 * strict_strtoul - convert a string to an unsigned long strictly
72 * @cp: The string to be converted
73 * @base: The number base to use
74 * @res: The converted result value
76 * strict_strtoul converts a string to an unsigned long only if the
77 * string is really an unsigned long string, any string containing
78 * any invalid char at the tail will be rejected and -EINVAL is returned,
79 * only a newline char at the tail is acceptible because people generally
80 * change a module parameter in the following way:
82 * echo 1024 > /sys/module/e1000/parameters/copybreak
84 * echo will append a newline to the tail.
86 * It returns 0 if conversion is successful and *res is set to the converted
87 * value, otherwise it returns -EINVAL and *res is set to 0.
89 * simple_strtoul just ignores the successive invalid characters and
90 * return the converted value of prefix part of the string.
92 * Copied this function from Linux 2.6.38 commit ID:
93 * 521cb40b0c44418a4fd36dc633f575813d59a43d
96 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
107 val = simple_strtoul(cp, &tail, base);
111 if ((*tail == '\0') ||
112 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
120 long simple_strtol(const char *cp,char **endp,unsigned int base)
123 return -simple_strtoul(cp+1,endp,base);
124 return simple_strtoul(cp,endp,base);
127 int ustrtoul(const char *cp, char **endp, unsigned int base)
129 unsigned long result = simple_strtoul(cp, endp, base);
140 if ((*endp)[1] == 'i') {
141 if ((*endp)[2] == 'B')
150 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
152 unsigned long long result = 0, value;
156 if ((*cp == 'x') && isxdigit (cp[1])) {
167 while (isxdigit (*cp) && (value = isdigit (*cp)
169 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
170 result = result * base + value;
178 /* we use this so that we can do without the ctype library */
179 #define is_digit(c) ((c) >= '0' && (c) <= '9')
181 static int skip_atoi(const char **s)
185 while (is_digit(**s))
186 i = i*10 + *((*s)++) - '0';
190 /* Decimal conversion is by far the most typical, and is used
191 * for /proc and /sys data. This directly impacts e.g. top performance
192 * with many processes running. We optimize it for speed
194 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
195 * (with permission from the author, Douglas W. Jones). */
197 /* Formats correctly any integer in [0,99999].
198 * Outputs from one to five digits depending on input.
199 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
200 static char* put_dec_trunc(char *buf, unsigned q)
202 unsigned d3, d2, d1, d0;
207 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
208 q = (d0 * 0xcd) >> 11;
210 *buf++ = d0 + '0'; /* least significant digit */
211 d1 = q + 9*d3 + 5*d2 + d1;
213 q = (d1 * 0xcd) >> 11;
215 *buf++ = d1 + '0'; /* next digit */
218 if ((d2 != 0) || (d3 != 0)) {
221 *buf++ = d2 + '0'; /* next digit */
225 q = (d3 * 0xcd) >> 11;
227 *buf++ = d3 + '0'; /* next digit */
229 *buf++ = q + '0'; /* most sign. digit */
235 /* Same with if's removed. Always emits five digits */
236 static char* put_dec_full(char *buf, unsigned q)
238 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
239 /* but anyway, gcc produces better code with full-sized ints */
240 unsigned d3, d2, d1, d0;
246 * Possible ways to approx. divide by 10
247 * gcc -O2 replaces multiply with shifts and adds
248 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
249 * (x * 0x67) >> 10: 1100111
250 * (x * 0x34) >> 9: 110100 - same
251 * (x * 0x1a) >> 8: 11010 - same
252 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
255 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
256 q = (d0 * 0xcd) >> 11;
259 d1 = q + 9*d3 + 5*d2 + d1;
260 q = (d1 * 0xcd) >> 11;
270 q = (d3 * 0xcd) >> 11; /* - shorter code */
271 /* q = (d3 * 0x67) >> 10; - would also work */
277 /* No inlining helps gcc to use registers better */
278 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
283 return put_dec_trunc(buf, num);
284 rem = do_div(num, 100000);
285 buf = put_dec_full(buf, rem);
289 #define ZEROPAD 1 /* pad with zero */
290 #define SIGN 2 /* unsigned/signed long */
291 #define PLUS 4 /* show plus */
292 #define SPACE 8 /* space if plus */
293 #define LEFT 16 /* left justified */
294 #define SMALL 32 /* Must be 32 == 0x20 */
295 #define SPECIAL 64 /* 0x */
297 #ifdef CONFIG_SYS_VSNPRINTF
299 * Macro to add a new character to our output string, but only if it will
300 * fit. The macro moves to the next character position in the output string.
302 #define ADDCH(str, ch) do { \
308 #define ADDCH(str, ch) (*(str)++ = (ch))
311 static char *number(char *buf, char *end, unsigned NUM_TYPE num,
312 int base, int size, int precision, int type)
314 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
315 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
320 int need_pfx = ((type & SPECIAL) && base != 10);
323 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
324 * produces same digits or (maybe lowercased) letters */
325 locase = (type & SMALL);
330 if ((signed NUM_TYPE) num < 0) {
332 num = - (signed NUM_TYPE) num;
334 } else if (type & PLUS) {
337 } else if (type & SPACE) {
348 /* generate full string in tmp[], in reverse order */
352 /* Generic code, for any base:
354 tmp[i++] = (digits[do_div(num,base)] | locase);
357 else if (base != 10) { /* 8 or 16 */
360 if (base == 16) shift = 4;
362 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
365 } else { /* base 10 */
366 i = put_dec(tmp, num) - tmp;
369 /* printing 100 using %2d gives "100", not "00" */
372 /* leading space padding */
374 if (!(type & (ZEROPAD + LEFT))) {
381 /* "0x" / "0" prefix */
385 ADDCH(buf, 'X' | locase);
387 /* zero or space padding */
388 if (!(type & LEFT)) {
389 char c = (type & ZEROPAD) ? '0' : ' ';
394 /* hmm even more zero padding? */
395 while (i <= --precision)
397 /* actual digits of result */
400 /* trailing space padding */
406 static char *string(char *buf, char *end, char *s, int field_width,
407 int precision, int flags)
414 len = strnlen(s, precision);
417 while (len < field_width--)
419 for (i = 0; i < len; ++i)
421 while (len < field_width--)
426 #ifdef CONFIG_CMD_NET
427 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
428 int precision, int flags)
430 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
434 for (i = 0; i < 6; i++) {
435 p = pack_hex_byte(p, addr[i]);
436 if (!(flags & SPECIAL) && i != 5)
441 return string(buf, end, mac_addr, field_width, precision,
445 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
446 int precision, int flags)
448 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
452 for (i = 0; i < 8; i++) {
453 p = pack_hex_byte(p, addr[2 * i]);
454 p = pack_hex_byte(p, addr[2 * i + 1]);
455 if (!(flags & SPECIAL) && i != 7)
460 return string(buf, end, ip6_addr, field_width, precision,
464 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
465 int precision, int flags)
467 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
468 char temp[3]; /* hold each IP quad in reverse order */
472 for (i = 0; i < 4; i++) {
473 digits = put_dec_trunc(temp, addr[i]) - temp;
474 /* reverse the digits in the quad */
482 return string(buf, end, ip4_addr, field_width, precision,
488 * Show a '%p' thing. A kernel extension is that the '%p' is followed
489 * by an extra set of alphanumeric characters that are extended format
492 * Right now we handle:
494 * - 'M' For a 6-byte MAC address, it prints the address in the
495 * usual colon-separated hex notation
496 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
497 * decimal for v4 and colon separated network-order 16 bit hex for v6)
498 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
501 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
502 * function pointers are really function descriptors, which contain a
503 * pointer to the real address.
505 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
506 int field_width, int precision, int flags)
509 return string(buf, end, "(null)", field_width, precision,
512 #ifdef CONFIG_CMD_NET
518 return mac_address_string(buf, end, ptr, field_width,
525 return ip6_addr_string(buf, end, ptr, field_width,
528 return ip4_addr_string(buf, end, ptr, field_width,
535 if (field_width == -1) {
536 field_width = 2*sizeof(void *);
539 return number(buf, end, (unsigned long)ptr, 16, field_width,
544 * Format a string and place it in a buffer (base function)
546 * @param buf The buffer to place the result into
547 * @param size The size of the buffer, including the trailing null space
548 * @param fmt The format string to use
549 * @param args Arguments for the format string
550 * @return The number characters which would be generated for the given
551 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
552 * characters may be written if this number of characters is >= size.
554 * This function follows C99 vsnprintf, but has some extensions:
555 * %pS output the name of a text symbol
556 * %pF output the name of a function pointer
557 * %pR output the address range in a struct resource
559 * Call this function if you are already dealing with a va_list.
560 * You probably want snprintf() instead.
562 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
565 unsigned NUM_TYPE num;
569 int flags; /* flags to number() */
571 int field_width; /* width of output field */
572 int precision; /* min. # of digits for integers; max
573 number of chars for from string */
574 int qualifier; /* 'h', 'l', or 'L' for integer fields */
575 /* 'z' support added 23/7/1999 S.H. */
576 /* 'z' changed to 'Z' --davidm 1/25/99 */
577 /* 't' added for ptrdiff_t */
578 char *end = buf + size;
580 #ifdef CONFIG_SYS_VSNPRINTF
581 /* Make sure end is always >= buf - do we want this in U-Boot? */
589 for (; *fmt ; ++fmt) {
598 ++fmt; /* this also skips first '%' */
600 case '-': flags |= LEFT; goto repeat;
601 case '+': flags |= PLUS; goto repeat;
602 case ' ': flags |= SPACE; goto repeat;
603 case '#': flags |= SPECIAL; goto repeat;
604 case '0': flags |= ZEROPAD; goto repeat;
607 /* get field width */
610 field_width = skip_atoi(&fmt);
611 else if (*fmt == '*') {
613 /* it's the next argument */
614 field_width = va_arg(args, int);
615 if (field_width < 0) {
616 field_width = -field_width;
621 /* get the precision */
626 precision = skip_atoi(&fmt);
627 else if (*fmt == '*') {
629 /* it's the next argument */
630 precision = va_arg(args, int);
636 /* get the conversion qualifier */
638 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
639 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
642 if (qualifier == 'l' && *fmt == 'l') {
653 if (!(flags & LEFT)) {
654 while (--field_width > 0)
657 ADDCH(str, (unsigned char) va_arg(args, int));
658 while (--field_width > 0)
663 str = string(str, end, va_arg(args, char *),
664 field_width, precision, flags);
668 str = pointer(fmt+1, str, end,
669 va_arg(args, void *),
670 field_width, precision, flags);
671 /* Skip all alphanumeric pointer suffixes */
672 while (isalnum(fmt[1]))
677 if (qualifier == 'l') {
678 long * ip = va_arg(args, long *);
681 int * ip = va_arg(args, int *);
690 /* integer number formats - set up the flags and "break" */
715 if (qualifier == 'L') /* "quad" for 64 bit variables */
716 num = va_arg(args, unsigned long long);
717 else if (qualifier == 'l') {
718 num = va_arg(args, unsigned long);
720 num = (signed long) num;
721 } else if (qualifier == 'Z' || qualifier == 'z') {
722 num = va_arg(args, size_t);
723 } else if (qualifier == 't') {
724 num = va_arg(args, ptrdiff_t);
725 } else if (qualifier == 'h') {
726 num = (unsigned short) va_arg(args, int);
728 num = (signed short) num;
730 num = va_arg(args, unsigned int);
732 num = (signed int) num;
734 str = number(str, end, num, base, field_width, precision,
738 #ifdef CONFIG_SYS_VSNPRINTF
747 /* the trailing null byte doesn't count towards the total */
751 #ifdef CONFIG_SYS_VSNPRINTF
752 int vsnprintf(char *buf, size_t size, const char *fmt,
755 return vsnprintf_internal(buf, size, fmt, args);
759 * Format a string and place it in a buffer (va_list version)
761 * @param buf The buffer to place the result into
762 * @param size The size of the buffer, including the trailing null space
763 * @param fmt The format string to use
764 * @param args Arguments for the format string
765 * @return the number of characters which have been written into
766 * the @buf not including the trailing '\0'. If @size is == 0 the function
769 * If you're not already dealing with a va_list consider using scnprintf().
771 * See the vsprintf() documentation for format string extensions over C99.
773 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
777 i = vsnprintf(buf, size, fmt, args);
779 if (likely(i < size))
787 * Format a string and place it in a buffer
789 * @param buf The buffer to place the result into
790 * @param size The size of the buffer, including the trailing null space
791 * @param fmt The format string to use
792 * @param ... Arguments for the format string
793 * @return the number of characters which would be
794 * generated for the given input, excluding the trailing null,
795 * as per ISO C99. If the return is greater than or equal to
796 * @size, the resulting string is truncated.
798 * See the vsprintf() documentation for format string extensions over C99.
800 int snprintf(char *buf, size_t size, const char *fmt, ...)
806 i = vsnprintf(buf, size, fmt, args);
813 * Format a string and place it in a buffer
815 * @param buf The buffer to place the result into
816 * @param size The size of the buffer, including the trailing null space
817 * @param fmt The format string to use
818 * @param ... Arguments for the format string
820 * The return value is the number of characters written into @buf not including
821 * the trailing '\0'. If @size is == 0 the function returns 0.
823 * See the vsprintf() documentation for format string extensions over C99.
826 int scnprintf(char *buf, size_t size, const char *fmt, ...)
832 i = vscnprintf(buf, size, fmt, args);
837 #endif /* CONFIG_SYS_VSNPRINT */
840 * Format a string and place it in a buffer (va_list version)
842 * @param buf The buffer to place the result into
843 * @param fmt The format string to use
844 * @param args Arguments for the format string
846 * The function returns the number of characters written
847 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
850 * If you're not already dealing with a va_list consider using sprintf().
852 int vsprintf(char *buf, const char *fmt, va_list args)
854 return vsnprintf_internal(buf, INT_MAX, fmt, args);
858 * Format a string and place it in a buffer
860 * @param buf The buffer to place the result into
861 * @param fmt The format string to use
862 * @param ... Arguments for the format string
864 * The function returns the number of characters written
867 * See the vsprintf() documentation for format string extensions over C99.
869 int sprintf(char * buf, const char *fmt, ...)
875 i=vsprintf(buf,fmt,args);
880 void panic(const char *fmt, ...)
887 #if defined (CONFIG_PANIC_HANG)
890 udelay (100000); /* allow messages to go out */
891 do_reset (NULL, 0, 0, NULL);
897 void __assert_fail(const char *assertion, const char *file, unsigned line,
898 const char *function)
900 /* This will not return */
901 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
905 char *simple_itoa(ulong i)
907 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
908 static char local[22];
909 char *p = &local[21];