]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/vsprintf.c
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 ustrtoull(const char *cp
, char **endp
, unsigned int base
)
131 unsigned long long result
= simple_strtoull(cp
, endp
, base
);
142 if ((*endp
)[1] == 'i') {
143 if ((*endp
)[2] == 'B')
152 unsigned long long simple_strtoull(const char *cp
, char **endp
,
155 unsigned long long result
= 0, value
;
159 if ((*cp
== 'x') && isxdigit(cp
[1])) {
171 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
- '0'
172 : (islower(*cp
) ? toupper(*cp
) : *cp
) - 'A' + 10) < base
) {
173 result
= result
* base
+ value
;
183 /* we use this so that we can do without the ctype library */
184 #define is_digit(c) ((c) >= '0' && (c) <= '9')
186 static int skip_atoi(const char **s
)
190 while (is_digit(**s
))
191 i
= i
* 10 + *((*s
)++) - '0';
196 /* Decimal conversion is by far the most typical, and is used
197 * for /proc and /sys data. This directly impacts e.g. top performance
198 * with many processes running. We optimize it for speed
200 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
201 * (with permission from the author, Douglas W. Jones). */
203 /* Formats correctly any integer in [0,99999].
204 * Outputs from one to five digits depending on input.
205 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
206 static char *put_dec_trunc(char *buf
, unsigned q
)
208 unsigned d3
, d2
, d1
, d0
;
213 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
214 q
= (d0
* 0xcd) >> 11;
216 *buf
++ = d0
+ '0'; /* least significant digit */
217 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
219 q
= (d1
* 0xcd) >> 11;
221 *buf
++ = d1
+ '0'; /* next digit */
224 if ((d2
!= 0) || (d3
!= 0)) {
227 *buf
++ = d2
+ '0'; /* next digit */
231 q
= (d3
* 0xcd) >> 11;
233 *buf
++ = d3
+ '0'; /* next digit */
235 *buf
++ = q
+ '0'; /* most sign. digit */
241 /* Same with if's removed. Always emits five digits */
242 static char *put_dec_full(char *buf
, unsigned q
)
244 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
245 /* but anyway, gcc produces better code with full-sized ints */
246 unsigned d3
, d2
, d1
, d0
;
252 * Possible ways to approx. divide by 10
253 * gcc -O2 replaces multiply with shifts and adds
254 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
255 * (x * 0x67) >> 10: 1100111
256 * (x * 0x34) >> 9: 110100 - same
257 * (x * 0x1a) >> 8: 11010 - same
258 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
261 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
262 q
= (d0
* 0xcd) >> 11;
265 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
266 q
= (d1
* 0xcd) >> 11;
276 q
= (d3
* 0xcd) >> 11; /* - shorter code */
277 /* q = (d3 * 0x67) >> 10; - would also work */
283 /* No inlining helps gcc to use registers better */
284 static noinline
char *put_dec(char *buf
, u64 num
)
289 return put_dec_trunc(buf
, num
);
290 rem
= do_div(num
, 100000);
291 buf
= put_dec_full(buf
, rem
);
295 #define ZEROPAD 1 /* pad with zero */
296 #define SIGN 2 /* unsigned/signed long */
297 #define PLUS 4 /* show plus */
298 #define SPACE 8 /* space if plus */
299 #define LEFT 16 /* left justified */
300 #define SMALL 32 /* Must be 32 == 0x20 */
301 #define SPECIAL 64 /* 0x */
303 #ifdef CONFIG_SYS_VSNPRINTF
305 * Macro to add a new character to our output string, but only if it will
306 * fit. The macro moves to the next character position in the output string.
308 #define ADDCH(str, ch) do { \
314 #define ADDCH(str, ch) (*(str)++ = (ch))
317 static char *number(char *buf
, char *end
, u64 num
,
318 int base
, int size
, int precision
, int type
)
320 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
321 static const char digits
[16] = "0123456789ABCDEF";
326 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
329 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
330 * produces same digits or (maybe lowercased) letters */
331 locase
= (type
& SMALL
);
340 } else if (type
& PLUS
) {
343 } else if (type
& SPACE
) {
354 /* generate full string in tmp[], in reverse order */
358 /* Generic code, for any base:
360 tmp[i++] = (digits[do_div(num,base)] | locase);
363 else if (base
!= 10) { /* 8 or 16 */
371 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
]
375 } else { /* base 10 */
376 i
= put_dec(tmp
, num
) - tmp
;
379 /* printing 100 using %2d gives "100", not "00" */
382 /* leading space padding */
384 if (!(type
& (ZEROPAD
+ LEFT
))) {
391 /* "0x" / "0" prefix */
395 ADDCH(buf
, 'X' | locase
);
397 /* zero or space padding */
398 if (!(type
& LEFT
)) {
399 char c
= (type
& ZEROPAD
) ? '0' : ' ';
404 /* hmm even more zero padding? */
405 while (i
<= --precision
)
407 /* actual digits of result */
410 /* trailing space padding */
416 static char *string(char *buf
, char *end
, char *s
, int field_width
,
417 int precision
, int flags
)
424 len
= strnlen(s
, precision
);
427 while (len
< field_width
--)
429 for (i
= 0; i
< len
; ++i
)
431 while (len
< field_width
--)
436 #ifdef CONFIG_CMD_NET
437 static char *mac_address_string(char *buf
, char *end
, u8
*addr
, int field_width
,
438 int precision
, int flags
)
440 /* (6 * 2 hex digits), 5 colons and trailing zero */
441 char mac_addr
[6 * 3];
445 for (i
= 0; i
< 6; i
++) {
446 p
= pack_hex_byte(p
, addr
[i
]);
447 if (!(flags
& SPECIAL
) && i
!= 5)
452 return string(buf
, end
, mac_addr
, field_width
, precision
,
456 static char *ip6_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
457 int precision
, int flags
)
459 /* (8 * 4 hex digits), 7 colons and trailing zero */
460 char ip6_addr
[8 * 5];
464 for (i
= 0; i
< 8; i
++) {
465 p
= pack_hex_byte(p
, addr
[2 * i
]);
466 p
= pack_hex_byte(p
, addr
[2 * i
+ 1]);
467 if (!(flags
& SPECIAL
) && i
!= 7)
472 return string(buf
, end
, ip6_addr
, field_width
, precision
,
476 static char *ip4_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
477 int precision
, int flags
)
479 /* (4 * 3 decimal digits), 3 dots and trailing zero */
480 char ip4_addr
[4 * 4];
481 char temp
[3]; /* hold each IP quad in reverse order */
485 for (i
= 0; i
< 4; i
++) {
486 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
487 /* reverse the digits in the quad */
495 return string(buf
, end
, ip4_addr
, field_width
, precision
,
501 * Show a '%p' thing. A kernel extension is that the '%p' is followed
502 * by an extra set of alphanumeric characters that are extended format
505 * Right now we handle:
507 * - 'M' For a 6-byte MAC address, it prints the address in the
508 * usual colon-separated hex notation
509 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
510 * decimal for v4 and colon separated network-order 16 bit hex for v6)
511 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
514 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
515 * function pointers are really function descriptors, which contain a
516 * pointer to the real address.
518 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
519 int field_width
, int precision
, int flags
)
522 * Being a boot loader, we explicitly allow pointers to
523 * (physical) address null.
527 return string(buf
, end
, "(null)", field_width
, precision
,
531 #ifdef CONFIG_CMD_NET
537 return mac_address_string(buf
, end
, ptr
, field_width
,
544 return ip6_addr_string(buf
, end
, ptr
, field_width
,
547 return ip4_addr_string(buf
, end
, ptr
, field_width
,
554 if (field_width
== -1) {
555 field_width
= 2*sizeof(void *);
558 return number(buf
, end
, (unsigned long)ptr
, 16, field_width
,
562 static int vsnprintf_internal(char *buf
, size_t size
, const char *fmt
,
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 '%' */
617 /* get field width */
620 field_width
= skip_atoi(&fmt
);
621 else if (*fmt
== '*') {
623 /* it's the next argument */
624 field_width
= va_arg(args
, int);
625 if (field_width
< 0) {
626 field_width
= -field_width
;
631 /* get the precision */
636 precision
= skip_atoi(&fmt
);
637 else if (*fmt
== '*') {
639 /* it's the next argument */
640 precision
= va_arg(args
, int);
646 /* get the conversion qualifier */
648 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
649 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't') {
652 if (qualifier
== 'l' && *fmt
== 'l') {
663 if (!(flags
& LEFT
)) {
664 while (--field_width
> 0)
667 ADDCH(str
, (unsigned char) va_arg(args
, int));
668 while (--field_width
> 0)
673 str
= string(str
, end
, va_arg(args
, char *),
674 field_width
, precision
, flags
);
678 str
= pointer(fmt
+ 1, str
, end
,
679 va_arg(args
, void *),
680 field_width
, precision
, flags
);
681 /* Skip all alphanumeric pointer suffixes */
682 while (isalnum(fmt
[1]))
687 if (qualifier
== 'l') {
688 long *ip
= va_arg(args
, long *);
691 int *ip
= va_arg(args
, int *);
700 /* integer number formats - set up the flags and "break" */
725 if (qualifier
== 'L') /* "quad" for 64 bit variables */
726 num
= va_arg(args
, unsigned long long);
727 else if (qualifier
== 'l') {
728 num
= va_arg(args
, unsigned long);
730 num
= (signed long) num
;
731 } else if (qualifier
== 'Z' || qualifier
== 'z') {
732 num
= va_arg(args
, size_t);
733 } else if (qualifier
== 't') {
734 num
= va_arg(args
, ptrdiff_t);
735 } else if (qualifier
== 'h') {
736 num
= (unsigned short) va_arg(args
, int);
738 num
= (signed short) num
;
740 num
= va_arg(args
, unsigned int);
742 num
= (signed int) num
;
744 str
= number(str
, end
, num
, base
, field_width
, precision
,
748 #ifdef CONFIG_SYS_VSNPRINTF
758 /* the trailing null byte doesn't count towards the total */
762 #ifdef CONFIG_SYS_VSNPRINTF
763 int vsnprintf(char *buf
, size_t size
, const char *fmt
,
766 return vsnprintf_internal(buf
, size
, fmt
, args
);
769 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
773 i
= vsnprintf(buf
, size
, fmt
, args
);
775 if (likely(i
< size
))
782 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
788 i
= vsnprintf(buf
, size
, fmt
, args
);
794 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
800 i
= vscnprintf(buf
, size
, fmt
, args
);
805 #endif /* CONFIG_SYS_VSNPRINT */
808 * Format a string and place it in a buffer (va_list version)
810 * @param buf The buffer to place the result into
811 * @param fmt The format string to use
812 * @param args Arguments for the format string
814 * The function returns the number of characters written
815 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
818 * If you're not already dealing with a va_list consider using sprintf().
820 int vsprintf(char *buf
, const char *fmt
, va_list args
)
822 return vsnprintf_internal(buf
, INT_MAX
, fmt
, args
);
825 int sprintf(char *buf
, const char *fmt
, ...)
831 i
= vsprintf(buf
, fmt
, args
);
836 void panic(const char *fmt
, ...)
843 #if defined(CONFIG_PANIC_HANG)
846 udelay(100000); /* allow messages to go out */
847 do_reset(NULL
, 0, 0, NULL
);
853 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
854 const char *function
)
856 /* This will not return */
857 panic("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,
861 char *simple_itoa(ulong i
)
863 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
864 static char local
[22];
865 char *p
= &local
[21];
875 /* We don't seem to have %'d in U-Boot */
876 void print_grouped_ull(unsigned long long int_val
, int digits
)
881 digits
= (digits
+ 2) / 3;
882 sprintf(str
, "%*llu", digits
* 3, int_val
);
883 for (s
= str
; *s
; s
+= grab
) {
885 putc(s
[-1] != ' ' ? ',' : ' ');
886 printf("%.*s", grab
, s
);