]>
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>
22 #define noinline __attribute__((noinline))
24 /* we use this so that we can do without the ctype library */
25 #define is_digit(c) ((c) >= '0' && (c) <= '9')
27 static int skip_atoi(const char **s
)
32 i
= i
* 10 + *((*s
)++) - '0';
37 /* Decimal conversion is by far the most typical, and is used
38 * for /proc and /sys data. This directly impacts e.g. top performance
39 * with many processes running. We optimize it for speed
41 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
42 * (with permission from the author, Douglas W. Jones). */
44 /* Formats correctly any integer in [0,99999].
45 * Outputs from one to five digits depending on input.
46 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
47 static char *put_dec_trunc(char *buf
, unsigned q
)
49 unsigned d3
, d2
, d1
, d0
;
54 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
55 q
= (d0
* 0xcd) >> 11;
57 *buf
++ = d0
+ '0'; /* least significant digit */
58 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
60 q
= (d1
* 0xcd) >> 11;
62 *buf
++ = d1
+ '0'; /* next digit */
65 if ((d2
!= 0) || (d3
!= 0)) {
68 *buf
++ = d2
+ '0'; /* next digit */
72 q
= (d3
* 0xcd) >> 11;
74 *buf
++ = d3
+ '0'; /* next digit */
76 *buf
++ = q
+ '0'; /* most sign. digit */
82 /* Same with if's removed. Always emits five digits */
83 static char *put_dec_full(char *buf
, unsigned q
)
85 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
86 /* but anyway, gcc produces better code with full-sized ints */
87 unsigned d3
, d2
, d1
, d0
;
93 * Possible ways to approx. divide by 10
94 * gcc -O2 replaces multiply with shifts and adds
95 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
96 * (x * 0x67) >> 10: 1100111
97 * (x * 0x34) >> 9: 110100 - same
98 * (x * 0x1a) >> 8: 11010 - same
99 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
102 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
103 q
= (d0
* 0xcd) >> 11;
106 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
107 q
= (d1
* 0xcd) >> 11;
117 q
= (d3
* 0xcd) >> 11; /* - shorter code */
118 /* q = (d3 * 0x67) >> 10; - would also work */
124 /* No inlining helps gcc to use registers better */
125 static noinline
char *put_dec(char *buf
, uint64_t num
)
130 return put_dec_trunc(buf
, num
);
131 rem
= do_div(num
, 100000);
132 buf
= put_dec_full(buf
, rem
);
136 #define ZEROPAD 1 /* pad with zero */
137 #define SIGN 2 /* unsigned/signed long */
138 #define PLUS 4 /* show plus */
139 #define SPACE 8 /* space if plus */
140 #define LEFT 16 /* left justified */
141 #define SMALL 32 /* Must be 32 == 0x20 */
142 #define SPECIAL 64 /* 0x */
144 #ifdef CONFIG_SYS_VSNPRINTF
146 * Macro to add a new character to our output string, but only if it will
147 * fit. The macro moves to the next character position in the output string.
149 #define ADDCH(str, ch) do { \
155 #define ADDCH(str, ch) (*(str)++ = (ch))
158 static char *number(char *buf
, char *end
, u64 num
,
159 int base
, int size
, int precision
, int type
)
161 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
162 static const char digits
[16] = "0123456789ABCDEF";
167 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
170 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
171 * produces same digits or (maybe lowercased) letters */
172 locase
= (type
& SMALL
);
181 } else if (type
& PLUS
) {
184 } else if (type
& SPACE
) {
195 /* generate full string in tmp[], in reverse order */
199 /* Generic code, for any base:
201 tmp[i++] = (digits[do_div(num,base)] | locase);
204 else if (base
!= 10) { /* 8 or 16 */
212 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
]
216 } else { /* base 10 */
217 i
= put_dec(tmp
, num
) - tmp
;
220 /* printing 100 using %2d gives "100", not "00" */
223 /* leading space padding */
225 if (!(type
& (ZEROPAD
+ LEFT
))) {
232 /* "0x" / "0" prefix */
236 ADDCH(buf
, 'X' | locase
);
238 /* zero or space padding */
239 if (!(type
& LEFT
)) {
240 char c
= (type
& ZEROPAD
) ? '0' : ' ';
245 /* hmm even more zero padding? */
246 while (i
<= --precision
)
248 /* actual digits of result */
251 /* trailing space padding */
257 static char *string(char *buf
, char *end
, char *s
, int field_width
,
258 int precision
, int flags
)
265 len
= strnlen(s
, precision
);
268 while (len
< field_width
--)
270 for (i
= 0; i
< len
; ++i
)
272 while (len
< field_width
--)
277 #ifdef CONFIG_CMD_NET
278 static const char hex_asc
[] = "0123456789abcdef";
279 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
280 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
282 static inline char *pack_hex_byte(char *buf
, u8 byte
)
284 *buf
++ = hex_asc_hi(byte
);
285 *buf
++ = hex_asc_lo(byte
);
289 static char *mac_address_string(char *buf
, char *end
, u8
*addr
, int field_width
,
290 int precision
, int flags
)
292 /* (6 * 2 hex digits), 5 colons and trailing zero */
293 char mac_addr
[6 * 3];
297 for (i
= 0; i
< 6; i
++) {
298 p
= pack_hex_byte(p
, addr
[i
]);
299 if (!(flags
& SPECIAL
) && i
!= 5)
304 return string(buf
, end
, mac_addr
, field_width
, precision
,
308 static char *ip6_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
309 int precision
, int flags
)
311 /* (8 * 4 hex digits), 7 colons and trailing zero */
312 char ip6_addr
[8 * 5];
316 for (i
= 0; i
< 8; i
++) {
317 p
= pack_hex_byte(p
, addr
[2 * i
]);
318 p
= pack_hex_byte(p
, addr
[2 * i
+ 1]);
319 if (!(flags
& SPECIAL
) && i
!= 7)
324 return string(buf
, end
, ip6_addr
, field_width
, precision
,
328 static char *ip4_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
329 int precision
, int flags
)
331 /* (4 * 3 decimal digits), 3 dots and trailing zero */
332 char ip4_addr
[4 * 4];
333 char temp
[3]; /* hold each IP quad in reverse order */
337 for (i
= 0; i
< 4; i
++) {
338 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
339 /* reverse the digits in the quad */
347 return string(buf
, end
, ip4_addr
, field_width
, precision
,
353 * Show a '%p' thing. A kernel extension is that the '%p' is followed
354 * by an extra set of alphanumeric characters that are extended format
357 * Right now we handle:
359 * - 'M' For a 6-byte MAC address, it prints the address in the
360 * usual colon-separated hex notation
361 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
362 * decimal for v4 and colon separated network-order 16 bit hex for v6)
363 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
366 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
367 * function pointers are really function descriptors, which contain a
368 * pointer to the real address.
370 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
371 int field_width
, int precision
, int flags
)
373 u64 num
= (uintptr_t)ptr
;
376 * Being a boot loader, we explicitly allow pointers to
377 * (physical) address null.
381 return string(buf
, end
, "(null)", field_width
, precision
,
385 #ifdef CONFIG_CMD_NET
388 flags
|= SPECIAL
| ZEROPAD
;
393 field_width
= sizeof(phys_addr_t
) * 2 + 2;
394 num
= *(phys_addr_t
*)ptr
;
402 return mac_address_string(buf
, end
, ptr
, field_width
,
409 return ip6_addr_string(buf
, end
, ptr
, field_width
,
412 return ip4_addr_string(buf
, end
, ptr
, field_width
,
419 if (field_width
== -1) {
420 field_width
= 2*sizeof(void *);
423 return number(buf
, end
, num
, 16, field_width
, precision
, flags
);
426 static int vsnprintf_internal(char *buf
, size_t size
, const char *fmt
,
433 int flags
; /* flags to number() */
435 int field_width
; /* width of output field */
436 int precision
; /* min. # of digits for integers; max
437 number of chars for from string */
438 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
439 /* 'z' support added 23/7/1999 S.H. */
440 /* 'z' changed to 'Z' --davidm 1/25/99 */
441 /* 't' added for ptrdiff_t */
442 char *end
= buf
+ size
;
444 #ifdef CONFIG_SYS_VSNPRINTF
445 /* Make sure end is always >= buf - do we want this in U-Boot? */
453 for (; *fmt
; ++fmt
) {
462 ++fmt
; /* this also skips first '%' */
481 /* get field width */
484 field_width
= skip_atoi(&fmt
);
485 else if (*fmt
== '*') {
487 /* it's the next argument */
488 field_width
= va_arg(args
, int);
489 if (field_width
< 0) {
490 field_width
= -field_width
;
495 /* get the precision */
500 precision
= skip_atoi(&fmt
);
501 else if (*fmt
== '*') {
503 /* it's the next argument */
504 precision
= va_arg(args
, int);
510 /* get the conversion qualifier */
512 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
513 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't') {
516 if (qualifier
== 'l' && *fmt
== 'l') {
527 if (!(flags
& LEFT
)) {
528 while (--field_width
> 0)
531 ADDCH(str
, (unsigned char) va_arg(args
, int));
532 while (--field_width
> 0)
537 str
= string(str
, end
, va_arg(args
, char *),
538 field_width
, precision
, flags
);
542 str
= pointer(fmt
+ 1, str
, end
,
543 va_arg(args
, void *),
544 field_width
, precision
, flags
);
545 /* Skip all alphanumeric pointer suffixes */
546 while (isalnum(fmt
[1]))
551 if (qualifier
== 'l') {
552 long *ip
= va_arg(args
, long *);
555 int *ip
= va_arg(args
, int *);
564 /* integer number formats - set up the flags and "break" */
589 if (qualifier
== 'L') /* "quad" for 64 bit variables */
590 num
= va_arg(args
, unsigned long long);
591 else if (qualifier
== 'l') {
592 num
= va_arg(args
, unsigned long);
594 num
= (signed long) num
;
595 } else if (qualifier
== 'Z' || qualifier
== 'z') {
596 num
= va_arg(args
, size_t);
597 } else if (qualifier
== 't') {
598 num
= va_arg(args
, ptrdiff_t);
599 } else if (qualifier
== 'h') {
600 num
= (unsigned short) va_arg(args
, int);
602 num
= (signed short) num
;
604 num
= va_arg(args
, unsigned int);
606 num
= (signed int) num
;
608 str
= number(str
, end
, num
, base
, field_width
, precision
,
612 #ifdef CONFIG_SYS_VSNPRINTF
622 /* the trailing null byte doesn't count towards the total */
626 #ifdef CONFIG_SYS_VSNPRINTF
627 int vsnprintf(char *buf
, size_t size
, const char *fmt
,
630 return vsnprintf_internal(buf
, size
, fmt
, args
);
633 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
637 i
= vsnprintf(buf
, size
, fmt
, args
);
639 if (likely(i
< size
))
646 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
652 i
= vsnprintf(buf
, size
, fmt
, args
);
658 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
664 i
= vscnprintf(buf
, size
, fmt
, args
);
669 #endif /* CONFIG_SYS_VSNPRINT */
672 * Format a string and place it in a buffer (va_list version)
674 * @param buf The buffer to place the result into
675 * @param fmt The format string to use
676 * @param args Arguments for the format string
678 * The function returns the number of characters written
679 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
682 * If you're not already dealing with a va_list consider using sprintf().
684 int vsprintf(char *buf
, const char *fmt
, va_list args
)
686 return vsnprintf_internal(buf
, INT_MAX
, fmt
, args
);
689 int sprintf(char *buf
, const char *fmt
, ...)
695 i
= vsprintf(buf
, fmt
, args
);
700 int printf(const char *fmt
, ...)
704 char printbuffer
[CONFIG_SYS_PBSIZE
];
709 * For this to work, printbuffer must be larger than
710 * anything we ever want to print.
712 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
715 /* Print the string */
720 int vprintf(const char *fmt
, va_list args
)
723 char printbuffer
[CONFIG_SYS_PBSIZE
];
726 * For this to work, printbuffer must be larger than
727 * anything we ever want to print.
729 i
= vscnprintf(printbuffer
, sizeof(printbuffer
), fmt
, args
);
731 /* Print the string */
737 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
738 const char *function
)
740 /* This will not return */
741 panic("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,
745 char *simple_itoa(ulong i
)
747 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
748 static char local
[22];
749 char *p
= &local
[21];
759 /* We don't seem to have %'d in U-Boot */
760 void print_grouped_ull(unsigned long long int_val
, int digits
)
765 digits
= (digits
+ 2) / 3;
766 sprintf(str
, "%*llu", digits
* 3, int_val
);
767 for (s
= str
; *s
; s
+= grab
) {
769 putc(s
[-1] != ' ' ? ',' : ' ');
770 printf("%.*s", grab
, s
);
775 bool str2off(const char *p
, loff_t
*num
)
779 *num
= simple_strtoull(p
, &endptr
, 16);
780 return *p
!= '\0' && *endptr
== '\0';
783 bool str2long(const char *p
, ulong
*num
)
787 *num
= simple_strtoul(p
, &endptr
, 16);
788 return *p
!= '\0' && *endptr
== '\0';