]>
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 :-)
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
19 #if !defined (CONFIG_PANIC_HANG)
24 # define NUM_TYPE long long
25 #define noinline __attribute__((noinline))
27 const char hex_asc
[] = "0123456789abcdef";
28 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
29 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
31 static inline char *pack_hex_byte(char *buf
, u8 byte
)
33 *buf
++ = hex_asc_hi(byte
);
34 *buf
++ = hex_asc_lo(byte
);
38 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
40 unsigned long result
= 0,value
;
44 if ((*cp
== 'x') && isxdigit(cp
[1])) {
55 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
56 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
57 result
= result
*base
+ value
;
66 * strict_strtoul - convert a string to an unsigned long strictly
67 * @cp: The string to be converted
68 * @base: The number base to use
69 * @res: The converted result value
71 * strict_strtoul converts a string to an unsigned long only if the
72 * string is really an unsigned long string, any string containing
73 * any invalid char at the tail will be rejected and -EINVAL is returned,
74 * only a newline char at the tail is acceptible because people generally
75 * change a module parameter in the following way:
77 * echo 1024 > /sys/module/e1000/parameters/copybreak
79 * echo will append a newline to the tail.
81 * It returns 0 if conversion is successful and *res is set to the converted
82 * value, otherwise it returns -EINVAL and *res is set to 0.
84 * simple_strtoul just ignores the successive invalid characters and
85 * return the converted value of prefix part of the string.
87 * Copied this function from Linux 2.6.38 commit ID:
88 * 521cb40b0c44418a4fd36dc633f575813d59a43d
91 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
102 val
= simple_strtoul(cp
, &tail
, base
);
106 if ((*tail
== '\0') ||
107 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
115 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
118 return -simple_strtoul(cp
+1,endp
,base
);
119 return simple_strtoul(cp
,endp
,base
);
122 int ustrtoul(const char *cp
, char **endp
, unsigned int base
)
124 unsigned long result
= simple_strtoul(cp
, endp
, base
);
135 if ((*endp
)[1] == 'i') {
136 if ((*endp
)[2] == 'B')
145 unsigned long long simple_strtoull (const char *cp
, char **endp
, unsigned int base
)
147 unsigned long long result
= 0, value
;
151 if ((*cp
== 'x') && isxdigit (cp
[1])) {
162 while (isxdigit (*cp
) && (value
= isdigit (*cp
)
164 : (islower (*cp
) ? toupper (*cp
) : *cp
) - 'A' + 10) < base
) {
165 result
= result
* base
+ value
;
173 /* we use this so that we can do without the ctype library */
174 #define is_digit(c) ((c) >= '0' && (c) <= '9')
176 static int skip_atoi(const char **s
)
180 while (is_digit(**s
))
181 i
= i
*10 + *((*s
)++) - '0';
185 /* Decimal conversion is by far the most typical, and is used
186 * for /proc and /sys data. This directly impacts e.g. top performance
187 * with many processes running. We optimize it for speed
189 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
190 * (with permission from the author, Douglas W. Jones). */
192 /* Formats correctly any integer in [0,99999].
193 * Outputs from one to five digits depending on input.
194 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
195 static char* put_dec_trunc(char *buf
, unsigned q
)
197 unsigned d3
, d2
, d1
, d0
;
202 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
203 q
= (d0
* 0xcd) >> 11;
205 *buf
++ = d0
+ '0'; /* least significant digit */
206 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
208 q
= (d1
* 0xcd) >> 11;
210 *buf
++ = d1
+ '0'; /* next digit */
213 if ((d2
!= 0) || (d3
!= 0)) {
216 *buf
++ = d2
+ '0'; /* next digit */
220 q
= (d3
* 0xcd) >> 11;
222 *buf
++ = d3
+ '0'; /* next digit */
224 *buf
++ = q
+ '0'; /* most sign. digit */
230 /* Same with if's removed. Always emits five digits */
231 static char* put_dec_full(char *buf
, unsigned q
)
233 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
234 /* but anyway, gcc produces better code with full-sized ints */
235 unsigned d3
, d2
, d1
, d0
;
241 * Possible ways to approx. divide by 10
242 * gcc -O2 replaces multiply with shifts and adds
243 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
244 * (x * 0x67) >> 10: 1100111
245 * (x * 0x34) >> 9: 110100 - same
246 * (x * 0x1a) >> 8: 11010 - same
247 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
250 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
251 q
= (d0
* 0xcd) >> 11;
254 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
255 q
= (d1
* 0xcd) >> 11;
265 q
= (d3
* 0xcd) >> 11; /* - shorter code */
266 /* q = (d3 * 0x67) >> 10; - would also work */
272 /* No inlining helps gcc to use registers better */
273 static noinline
char* put_dec(char *buf
, unsigned NUM_TYPE num
)
278 return put_dec_trunc(buf
, num
);
279 rem
= do_div(num
, 100000);
280 buf
= put_dec_full(buf
, rem
);
284 #define ZEROPAD 1 /* pad with zero */
285 #define SIGN 2 /* unsigned/signed long */
286 #define PLUS 4 /* show plus */
287 #define SPACE 8 /* space if plus */
288 #define LEFT 16 /* left justified */
289 #define SMALL 32 /* Must be 32 == 0x20 */
290 #define SPECIAL 64 /* 0x */
292 static char *number(char *buf
, unsigned NUM_TYPE num
, int base
, int size
, int precision
, int type
)
294 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
295 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
300 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
303 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
304 * produces same digits or (maybe lowercased) letters */
305 locase
= (type
& SMALL
);
310 if ((signed NUM_TYPE
) num
< 0) {
312 num
= - (signed NUM_TYPE
) num
;
314 } else if (type
& PLUS
) {
317 } else if (type
& SPACE
) {
328 /* generate full string in tmp[], in reverse order */
332 /* Generic code, for any base:
334 tmp[i++] = (digits[do_div(num,base)] | locase);
337 else if (base
!= 10) { /* 8 or 16 */
340 if (base
== 16) shift
= 4;
342 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
345 } else { /* base 10 */
346 i
= put_dec(tmp
, num
) - tmp
;
349 /* printing 100 using %2d gives "100", not "00" */
352 /* leading space padding */
354 if (!(type
& (ZEROPAD
+LEFT
)))
360 /* "0x" / "0" prefix */
364 *buf
++ = ('X' | locase
);
366 /* zero or space padding */
367 if (!(type
& LEFT
)) {
368 char c
= (type
& ZEROPAD
) ? '0' : ' ';
372 /* hmm even more zero padding? */
373 while (i
<= --precision
)
375 /* actual digits of result */
378 /* trailing space padding */
384 static char *string(char *buf
, char *s
, int field_width
, int precision
, int flags
)
391 len
= strnlen(s
, precision
);
394 while (len
< field_width
--)
396 for (i
= 0; i
< len
; ++i
)
398 while (len
< field_width
--)
403 #ifdef CONFIG_CMD_NET
404 static char *mac_address_string(char *buf
, u8
*addr
, int field_width
,
405 int precision
, int flags
)
407 char mac_addr
[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
411 for (i
= 0; i
< 6; i
++) {
412 p
= pack_hex_byte(p
, addr
[i
]);
413 if (!(flags
& SPECIAL
) && i
!= 5)
418 return string(buf
, mac_addr
, field_width
, precision
, flags
& ~SPECIAL
);
421 static char *ip6_addr_string(char *buf
, u8
*addr
, int field_width
,
422 int precision
, int flags
)
424 char ip6_addr
[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
428 for (i
= 0; i
< 8; i
++) {
429 p
= pack_hex_byte(p
, addr
[2 * i
]);
430 p
= pack_hex_byte(p
, addr
[2 * i
+ 1]);
431 if (!(flags
& SPECIAL
) && i
!= 7)
436 return string(buf
, ip6_addr
, field_width
, precision
, flags
& ~SPECIAL
);
439 static char *ip4_addr_string(char *buf
, u8
*addr
, int field_width
,
440 int precision
, int flags
)
442 char ip4_addr
[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
443 char temp
[3]; /* hold each IP quad in reverse order */
447 for (i
= 0; i
< 4; i
++) {
448 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
449 /* reverse the digits in the quad */
457 return string(buf
, ip4_addr
, field_width
, precision
, flags
& ~SPECIAL
);
462 * Show a '%p' thing. A kernel extension is that the '%p' is followed
463 * by an extra set of alphanumeric characters that are extended format
466 * Right now we handle:
468 * - 'M' For a 6-byte MAC address, it prints the address in the
469 * usual colon-separated hex notation
470 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
471 * decimal for v4 and colon separated network-order 16 bit hex for v6)
472 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
475 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
476 * function pointers are really function descriptors, which contain a
477 * pointer to the real address.
479 static char *pointer(const char *fmt
, char *buf
, void *ptr
, int field_width
, int precision
, int flags
)
482 return string(buf
, "(null)", field_width
, precision
, flags
);
484 #ifdef CONFIG_CMD_NET
490 return mac_address_string(buf
, ptr
, field_width
, precision
, flags
);
496 return ip6_addr_string(buf
, ptr
, field_width
, precision
, flags
);
498 return ip4_addr_string(buf
, ptr
, field_width
, precision
, flags
);
504 if (field_width
== -1) {
505 field_width
= 2*sizeof(void *);
508 return number(buf
, (unsigned long) ptr
, 16, field_width
, precision
, flags
);
512 * vsprintf - Format a string and place it in a buffer
513 * @buf: The buffer to place the result into
514 * @fmt: The format string to use
515 * @args: Arguments for the format string
517 * This function follows C99 vsprintf, but has some extensions:
518 * %pS output the name of a text symbol
519 * %pF output the name of a function pointer
520 * %pR output the address range in a struct resource
522 * The function returns the number of characters written
525 * Call this function if you are already dealing with a va_list.
526 * You probably want sprintf() instead.
528 int vsprintf(char *buf
, const char *fmt
, va_list args
)
530 unsigned NUM_TYPE num
;
534 int flags
; /* flags to number() */
536 int field_width
; /* width of output field */
537 int precision
; /* min. # of digits for integers; max
538 number of chars for from string */
539 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
540 /* 'z' support added 23/7/1999 S.H. */
541 /* 'z' changed to 'Z' --davidm 1/25/99 */
542 /* 't' added for ptrdiff_t */
546 for (; *fmt
; ++fmt
) {
555 ++fmt
; /* this also skips first '%' */
557 case '-': flags
|= LEFT
; goto repeat
;
558 case '+': flags
|= PLUS
; goto repeat
;
559 case ' ': flags
|= SPACE
; goto repeat
;
560 case '#': flags
|= SPECIAL
; goto repeat
;
561 case '0': flags
|= ZEROPAD
; goto repeat
;
564 /* get field width */
567 field_width
= skip_atoi(&fmt
);
568 else if (*fmt
== '*') {
570 /* it's the next argument */
571 field_width
= va_arg(args
, int);
572 if (field_width
< 0) {
573 field_width
= -field_width
;
578 /* get the precision */
583 precision
= skip_atoi(&fmt
);
584 else if (*fmt
== '*') {
586 /* it's the next argument */
587 precision
= va_arg(args
, int);
593 /* get the conversion qualifier */
595 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
596 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't') {
599 if (qualifier
== 'l' && *fmt
== 'l') {
611 while (--field_width
> 0)
613 *str
++ = (unsigned char) va_arg(args
, int);
614 while (--field_width
> 0)
619 str
= string(str
, va_arg(args
, char *), field_width
, precision
, flags
);
623 str
= pointer(fmt
+1, str
,
624 va_arg(args
, void *),
625 field_width
, precision
, flags
);
626 /* Skip all alphanumeric pointer suffixes */
627 while (isalnum(fmt
[1]))
632 if (qualifier
== 'l') {
633 long * ip
= va_arg(args
, long *);
636 int * ip
= va_arg(args
, int *);
645 /* integer number formats - set up the flags and "break" */
670 if (qualifier
== 'L') /* "quad" for 64 bit variables */
671 num
= va_arg(args
, unsigned long long);
672 else if (qualifier
== 'l') {
673 num
= va_arg(args
, unsigned long);
675 num
= (signed long) num
;
676 } else if (qualifier
== 'Z' || qualifier
== 'z') {
677 num
= va_arg(args
, size_t);
678 } else if (qualifier
== 't') {
679 num
= va_arg(args
, ptrdiff_t);
680 } else if (qualifier
== 'h') {
681 num
= (unsigned short) va_arg(args
, int);
683 num
= (signed short) num
;
685 num
= va_arg(args
, unsigned int);
687 num
= (signed int) num
;
689 str
= number(str
, num
, base
, field_width
, precision
, flags
);
696 * sprintf - Format a string and place it in a buffer
697 * @buf: The buffer to place the result into
698 * @fmt: The format string to use
699 * @...: Arguments for the format string
701 * The function returns the number of characters written
704 * See the vsprintf() documentation for format string extensions over C99.
706 int sprintf(char * buf
, const char *fmt
, ...)
712 i
=vsprintf(buf
,fmt
,args
);
717 void panic(const char *fmt
, ...)
724 #if defined (CONFIG_PANIC_HANG)
727 udelay (100000); /* allow messages to go out */
728 do_reset (NULL
, 0, 0, NULL
);
734 void __assert_fail(const char *assertion
, const char *file
, unsigned line
,
735 const char *function
)
737 /* This will not return */
738 panic("%s:%u: %s: Assertion `%s' failed.", file
, line
, function
,