]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/vsprintf.c
dd8380b418c932ca0e0d2e8b80248d213ee047da
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 unsigned long simple_strtoul(const char *cp
, char **endp
,
31 unsigned long result
= 0;
36 if ((*cp
== 'x') && isxdigit(cp
[1])) {
48 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
49 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
50 result
= result
*base
+ value
;
60 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
71 val
= simple_strtoul(cp
, &tail
, base
);
75 if ((*tail
== '\0') ||
76 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
84 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
87 return -simple_strtoul(cp
+ 1, endp
, base
);
89 return simple_strtoul(cp
, endp
, base
);
92 unsigned long ustrtoul(const char *cp
, char **endp
, unsigned int base
)
94 unsigned long result
= simple_strtoul(cp
, endp
, base
);
105 if ((*endp
)[1] == 'i') {
106 if ((*endp
)[2] == 'B')
115 unsigned long long ustrtoull(const char *cp
, char **endp
, unsigned int base
)
117 unsigned long long result
= simple_strtoull(cp
, endp
, base
);
128 if ((*endp
)[1] == 'i') {
129 if ((*endp
)[2] == 'B')
138 unsigned long long simple_strtoull(const char *cp
, char **endp
,
141 unsigned long long result
= 0, value
;
145 if ((*cp
== 'x') && isxdigit(cp
[1])) {
157 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
- '0'
158 : (islower(*cp
) ? toupper(*cp
) : *cp
) - 'A' + 10) < base
) {
159 result
= result
* base
+ value
;
169 long trailing_strtoln(const char *str
, const char *end
)
174 end
= str
+ strlen(str
);
175 for (p
= end
- 1; p
> str
; p
--) {
177 return simple_strtoul(p
+ 1, NULL
, 10);
183 long trailing_strtol(const char *str
)
185 return trailing_strtoln(str
, NULL
);
188 /* we use this so that we can do without the ctype library */
189 #define is_digit(c) ((c) >= '0' && (c) <= '9')
191 static int skip_atoi(const char **s
)
195 while (is_digit(**s
))
196 i
= i
* 10 + *((*s
)++) - '0';
201 /* Decimal conversion is by far the most typical, and is used
202 * for /proc and /sys data. This directly impacts e.g. top performance
203 * with many processes running. We optimize it for speed
205 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
206 * (with permission from the author, Douglas W. Jones). */
208 /* Formats correctly any integer in [0,99999].
209 * Outputs from one to five digits depending on input.
210 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
211 static char *put_dec_trunc(char *buf
, unsigned q
)
213 unsigned d3
, d2
, d1
, d0
;
218 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
219 q
= (d0
* 0xcd) >> 11;
221 *buf
++ = d0
+ '0'; /* least significant digit */
222 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
224 q
= (d1
* 0xcd) >> 11;
226 *buf
++ = d1
+ '0'; /* next digit */
229 if ((d2
!= 0) || (d3
!= 0)) {
232 *buf
++ = d2
+ '0'; /* next digit */
236 q
= (d3
* 0xcd) >> 11;
238 *buf
++ = d3
+ '0'; /* next digit */
240 *buf
++ = q
+ '0'; /* most sign. digit */
246 /* Same with if's removed. Always emits five digits */
247 static char *put_dec_full(char *buf
, unsigned q
)
249 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
250 /* but anyway, gcc produces better code with full-sized ints */
251 unsigned d3
, d2
, d1
, d0
;
257 * Possible ways to approx. divide by 10
258 * gcc -O2 replaces multiply with shifts and adds
259 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
260 * (x * 0x67) >> 10: 1100111
261 * (x * 0x34) >> 9: 110100 - same
262 * (x * 0x1a) >> 8: 11010 - same
263 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
266 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
267 q
= (d0
* 0xcd) >> 11;
270 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
271 q
= (d1
* 0xcd) >> 11;
281 q
= (d3
* 0xcd) >> 11; /* - shorter code */
282 /* q = (d3 * 0x67) >> 10; - would also work */
288 /* No inlining helps gcc to use registers better */
289 static noinline
char *put_dec(char *buf
, uint64_t num
)
294 return put_dec_trunc(buf
, num
);
295 rem
= do_div(num
, 100000);
296 buf
= put_dec_full(buf
, rem
);
300 #define ZEROPAD 1 /* pad with zero */
301 #define SIGN 2 /* unsigned/signed long */
302 #define PLUS 4 /* show plus */
303 #define SPACE 8 /* space if plus */
304 #define LEFT 16 /* left justified */
305 #define SMALL 32 /* Must be 32 == 0x20 */
306 #define SPECIAL 64 /* 0x */
308 #ifdef CONFIG_SYS_VSNPRINTF
310 * Macro to add a new character to our output string, but only if it will
311 * fit. The macro moves to the next character position in the output string.
313 #define ADDCH(str, ch) do { \
319 #define ADDCH(str, ch) (*(str)++ = (ch))
322 static char *number(char *buf
, char *end
, u64 num
,
323 int base
, int size
, int precision
, int type
)
325 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
326 static const char digits
[16] = "0123456789ABCDEF";
331 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
334 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
335 * produces same digits or (maybe lowercased) letters */
336 locase
= (type
& SMALL
);
345 } else if (type
& PLUS
) {
348 } else if (type
& SPACE
) {
359 /* generate full string in tmp[], in reverse order */
363 /* Generic code, for any base:
365 tmp[i++] = (digits[do_div(num,base)] | locase);
368 else if (base
!= 10) { /* 8 or 16 */
376 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
]
380 } else { /* base 10 */
381 i
= put_dec(tmp
, num
) - tmp
;
384 /* printing 100 using %2d gives "100", not "00" */
387 /* leading space padding */
389 if (!(type
& (ZEROPAD
+ LEFT
))) {
396 /* "0x" / "0" prefix */
400 ADDCH(buf
, 'X' | locase
);
402 /* zero or space padding */
403 if (!(type
& LEFT
)) {
404 char c
= (type
& ZEROPAD
) ? '0' : ' ';
409 /* hmm even more zero padding? */
410 while (i
<= --precision
)
412 /* actual digits of result */
415 /* trailing space padding */
421 static char *string(char *buf
, char *end
, char *s
, int field_width
,
422 int precision
, int flags
)
429 len
= strnlen(s
, precision
);
432 while (len
< field_width
--)
434 for (i
= 0; i
< len
; ++i
)
436 while (len
< field_width
--)
441 #ifdef CONFIG_CMD_NET
442 static const char hex_asc
[] = "0123456789abcdef";
443 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
444 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
446 static inline char *pack_hex_byte(char *buf
, u8 byte
)
448 *buf
++ = hex_asc_hi(byte
);
449 *buf
++ = hex_asc_lo(byte
);
453 static char *mac_address_string(char *buf
, char *end
, u8
*addr
, int field_width
,
454 int precision
, int flags
)
456 /* (6 * 2 hex digits), 5 colons and trailing zero */
457 char mac_addr
[6 * 3];
461 for (i
= 0; i
< 6; i
++) {
462 p
= pack_hex_byte(p
, addr
[i
]);
463 if (!(flags
& SPECIAL
) && i
!= 5)
468 return string(buf
, end
, mac_addr
, field_width
, precision
,
472 static char *ip6_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
473 int precision
, int flags
)
475 /* (8 * 4 hex digits), 7 colons and trailing zero */
476 char ip6_addr
[8 * 5];
480 for (i
= 0; i
< 8; i
++) {
481 p
= pack_hex_byte(p
, addr
[2 * i
]);
482 p
= pack_hex_byte(p
, addr
[2 * i
+ 1]);
483 if (!(flags
& SPECIAL
) && i
!= 7)
488 return string(buf
, end
, ip6_addr
, field_width
, precision
,
492 static char *ip4_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
493 int precision
, int flags
)
495 /* (4 * 3 decimal digits), 3 dots and trailing zero */
496 char ip4_addr
[4 * 4];
497 char temp
[3]; /* hold each IP quad in reverse order */
501 for (i
= 0; i
< 4; i
++) {
502 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
503 /* reverse the digits in the quad */
511 return string(buf
, end
, ip4_addr
, field_width
, precision
,
517 * Show a '%p' thing. A kernel extension is that the '%p' is followed
518 * by an extra set of alphanumeric characters that are extended format
521 * Right now we handle:
523 * - 'M' For a 6-byte MAC address, it prints the address in the
524 * usual colon-separated hex notation
525 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
526 * decimal for v4 and colon separated network-order 16 bit hex for v6)
527 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
530 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
531 * function pointers are really function descriptors, which contain a
532 * pointer to the real address.
534 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
535 int field_width
, int precision
, int flags
)
537 u64 num
= (uintptr_t)ptr
;
540 * Being a boot loader, we explicitly allow pointers to
541 * (physical) address null.
545 return string(buf
, end
, "(null)", field_width
, precision
,
549 #ifdef CONFIG_CMD_NET
552 flags
|= SPECIAL
| ZEROPAD
;
557 field_width
= sizeof(phys_addr_t
) * 2 + 2;
558 num
= *(phys_addr_t
*)ptr
;
566 return mac_address_string(buf
, end
, ptr
, field_width
,
573 return ip6_addr_string(buf
, end
, ptr
, field_width
,
576 return ip4_addr_string(buf
, end
, ptr
, field_width
,
583 if (field_width
== -1) {
584 field_width
= 2*sizeof(void *);
587 return number(buf
, end
, num
, 16, field_width
, precision
, flags
);
590 static int vsnprintf_internal(char *buf
, size_t size
, const char *fmt
,
597 int flags
; /* flags to number() */
599 int field_width
; /* width of output field */
600 int precision
; /* min. # of digits for integers; max
601 number of chars for from string */
602 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
603 /* 'z' support added 23/7/1999 S.H. */
604 /* 'z' changed to 'Z' --davidm 1/25/99 */
605 /* 't' added for ptrdiff_t */
606 char *end
= buf
+ size
;
608 #ifdef CONFIG_SYS_VSNPRINTF
609 /* Make sure end is always >= buf - do we want this in U-Boot? */
617 for (; *fmt
; ++fmt
) {
626 ++fmt
; /* this also skips first '%' */
645 /* get field width */
648 field_width
= skip_atoi(&fmt
);
649 else if (*fmt
== '*') {
651 /* it's the next argument */
652 field_width
= va_arg(args
, int);
653 if (field_width
< 0) {
654 field_width
= -field_width
;
659 /* get the precision */
664 precision
= skip_atoi(&fmt
);
665 else if (*fmt
== '*') {
667 /* it's the next argument */
668 precision
= va_arg(args
, int);
674 /* get the conversion qualifier */
676 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
677 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't') {
680 if (qualifier
== 'l' && *fmt
== 'l') {
691 if (!(flags
& LEFT
)) {
692 while (--field_width
> 0)
695 ADDCH(str
, (unsigned char) va_arg(args
, int));
696 while (--field_width
> 0)
701 str
= string(str
, end
, va_arg(args
, char *),
702 field_width
, precision
, flags
);
706 str
= pointer(fmt
+ 1, str
, end
,
707 va_arg(args
, void *),
708 field_width
, precision
, flags
);
709 /* Skip all alphanumeric pointer suffixes */
710 while (isalnum(fmt
[1]))
715 if (qualifier
== 'l') {
716 long *ip
= va_arg(args
, long *);
719 int *ip
= va_arg(args
, int *);
728 /* integer number formats - set up the flags and "break" */
753 if (qualifier
== 'L') /* "quad" for 64 bit variables */
754 num
= va_arg(args
, unsigned long long);
755 else if (qualifier
== 'l') {
756 num
= va_arg(args
, unsigned long);
758 num
= (signed long) num
;
759 } else if (qualifier
== 'Z' || qualifier
== 'z') {
760 num
= va_arg(args
, size_t);
761 } else if (qualifier
== 't') {
762 num
= va_arg(args
, ptrdiff_t);
763 } else if (qualifier
== 'h') {
764 num
= (unsigned short) va_arg(args
, int);
766 num
= (signed short) num
;
768 num
= va_arg(args
, unsigned int);
770 num
= (signed int) num
;
772 str
= number(str
, end
, num
, base
, field_width
, precision
,
776 #ifdef CONFIG_SYS_VSNPRINTF
786 /* the trailing null byte doesn't count towards the total */
790 #ifdef CONFIG_SYS_VSNPRINTF
791 int vsnprintf(char *buf
, size_t size
, const char *fmt
,
794 return vsnprintf_internal(buf
, size
, fmt
, args
);
797 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
801 i
= vsnprintf(buf
, size
, fmt
, args
);
803 if (likely(i
< size
))
810 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
816 i
= vsnprintf(buf
, size
, fmt
, args
);
822 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
828 i
= vscnprintf(buf
, size
, fmt
, args
);
833 #endif /* CONFIG_SYS_VSNPRINT */
836 * Format a string and place it in a buffer (va_list version)
838 * @param buf The buffer to place the result into
839 * @param fmt The format string to use
840 * @param args Arguments for the format string
842 * The function returns the number of characters written
843 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
846 * If you're not already dealing with a va_list consider using sprintf().
848 int vsprintf(char *buf
, const char *fmt
, va_list args
)
850 return vsnprintf_internal(buf
, INT_MAX
, fmt
, args
);
853 int sprintf(char *buf
, const char *fmt
, ...)
859 i
= vsprintf(buf
, fmt
, args
);
864 int printf(const char *fmt
, ...)
868 char printbuffer
[CONFIG_SYS_PBSIZE
];
873 * For this to work, printbuffer must be larger than
874 * anything we ever want to print.
876 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
879 /* Print the string */
884 int vprintf(const char *fmt
, va_list args
)
887 char printbuffer
[CONFIG_SYS_PBSIZE
];
890 * For this to work, printbuffer must be larger than
891 * anything we ever want to print.
893 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
895 /* Print the string */
900 static void panic_finish(void) __attribute__ ((noreturn
));
902 static void panic_finish(void)
905 #if defined(CONFIG_PANIC_HANG)
908 udelay(100000); /* allow messages to go out */
909 do_reset(NULL
, 0, 0, NULL
);
915 void panic_str(const char *str
)
921 void panic(const char *fmt
, ...)
930 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
931 const char *function
)
933 /* This will not return */
934 panic("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,
938 char *simple_itoa(ulong i
)
940 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
941 static char local
[22];
942 char *p
= &local
[21];
952 /* We don't seem to have %'d in U-Boot */
953 void print_grouped_ull(unsigned long long int_val
, int digits
)
958 digits
= (digits
+ 2) / 3;
959 sprintf(str
, "%*llu", digits
* 3, int_val
);
960 for (s
= str
; *s
; s
+= grab
) {
962 putc(s
[-1] != ' ' ? ',' : ' ');
963 printf("%.*s", grab
, s
);
968 bool str2off(const char *p
, loff_t
*num
)
972 *num
= simple_strtoull(p
, &endptr
, 16);
973 return *p
!= '\0' && *endptr
== '\0';
976 bool str2long(const char *p
, ulong
*num
)
980 *num
= simple_strtoul(p
, &endptr
, 16);
981 return *p
!= '\0' && *endptr
== '\0';