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>
18 #if !defined (CONFIG_PANIC_HANG)
21 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
24 #ifdef CONFIG_SYS_64BIT_VSPRINTF
25 # define NUM_TYPE long long
27 # define NUM_TYPE long
29 #define noinline __attribute__((noinline))
31 #define do_div(n, base) ({ \
33 __res = ((unsigned NUM_TYPE) n) % base; \
34 n = ((unsigned NUM_TYPE) n) / base; \
38 const char hex_asc[] = "0123456789abcdef";
39 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
40 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
42 static inline char *pack_hex_byte(char *buf, u8 byte)
44 *buf++ = hex_asc_hi(byte);
45 *buf++ = hex_asc_lo(byte);
49 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
51 unsigned long result = 0,value;
55 if ((*cp == 'x') && isxdigit(cp[1])) {
66 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
67 ? toupper(*cp) : *cp)-'A'+10) < base) {
68 result = result*base + value;
76 long simple_strtol(const char *cp,char **endp,unsigned int base)
79 return -simple_strtoul(cp+1,endp,base);
80 return simple_strtoul(cp,endp,base);
83 int ustrtoul(const char *cp, char **endp, unsigned int base)
85 unsigned long result = simple_strtoul(cp, endp, base);
96 if ((*endp)[1] == 'i') {
97 if ((*endp)[2] == 'B')
106 #ifdef CONFIG_SYS_64BIT_STRTOUL
107 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
109 unsigned long long result = 0, value;
113 if ((*cp == 'x') && isxdigit (cp[1])) {
124 while (isxdigit (*cp) && (value = isdigit (*cp)
126 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
127 result = result * base + value;
134 #endif /* CONFIG_SYS_64BIT_STRTOUL */
136 /* we use this so that we can do without the ctype library */
137 #define is_digit(c) ((c) >= '0' && (c) <= '9')
139 static int skip_atoi(const char **s)
143 while (is_digit(**s))
144 i = i*10 + *((*s)++) - '0';
148 /* Decimal conversion is by far the most typical, and is used
149 * for /proc and /sys data. This directly impacts e.g. top performance
150 * with many processes running. We optimize it for speed
152 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
153 * (with permission from the author, Douglas W. Jones). */
155 /* Formats correctly any integer in [0,99999].
156 * Outputs from one to five digits depending on input.
157 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
158 static char* put_dec_trunc(char *buf, unsigned q)
160 unsigned d3, d2, d1, d0;
165 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
166 q = (d0 * 0xcd) >> 11;
168 *buf++ = d0 + '0'; /* least significant digit */
169 d1 = q + 9*d3 + 5*d2 + d1;
171 q = (d1 * 0xcd) >> 11;
173 *buf++ = d1 + '0'; /* next digit */
176 if ((d2 != 0) || (d3 != 0)) {
179 *buf++ = d2 + '0'; /* next digit */
183 q = (d3 * 0xcd) >> 11;
185 *buf++ = d3 + '0'; /* next digit */
187 *buf++ = q + '0'; /* most sign. digit */
193 /* Same with if's removed. Always emits five digits */
194 static char* put_dec_full(char *buf, unsigned q)
196 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
197 /* but anyway, gcc produces better code with full-sized ints */
198 unsigned d3, d2, d1, d0;
204 * Possible ways to approx. divide by 10
205 * gcc -O2 replaces multiply with shifts and adds
206 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
207 * (x * 0x67) >> 10: 1100111
208 * (x * 0x34) >> 9: 110100 - same
209 * (x * 0x1a) >> 8: 11010 - same
210 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
213 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
214 q = (d0 * 0xcd) >> 11;
217 d1 = q + 9*d3 + 5*d2 + d1;
218 q = (d1 * 0xcd) >> 11;
228 q = (d3 * 0xcd) >> 11; /* - shorter code */
229 /* q = (d3 * 0x67) >> 10; - would also work */
235 /* No inlining helps gcc to use registers better */
236 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
241 return put_dec_trunc(buf, num);
242 rem = do_div(num, 100000);
243 buf = put_dec_full(buf, rem);
247 #define ZEROPAD 1 /* pad with zero */
248 #define SIGN 2 /* unsigned/signed long */
249 #define PLUS 4 /* show plus */
250 #define SPACE 8 /* space if plus */
251 #define LEFT 16 /* left justified */
252 #define SMALL 32 /* Must be 32 == 0x20 */
253 #define SPECIAL 64 /* 0x */
255 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
257 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
258 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
263 int need_pfx = ((type & SPECIAL) && base != 10);
266 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
267 * produces same digits or (maybe lowercased) letters */
268 locase = (type & SMALL);
273 if ((signed NUM_TYPE) num < 0) {
275 num = - (signed NUM_TYPE) num;
277 } else if (type & PLUS) {
280 } else if (type & SPACE) {
291 /* generate full string in tmp[], in reverse order */
295 /* Generic code, for any base:
297 tmp[i++] = (digits[do_div(num,base)] | locase);
300 else if (base != 10) { /* 8 or 16 */
303 if (base == 16) shift = 4;
305 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
308 } else { /* base 10 */
309 i = put_dec(tmp, num) - tmp;
312 /* printing 100 using %2d gives "100", not "00" */
315 /* leading space padding */
317 if (!(type & (ZEROPAD+LEFT)))
323 /* "0x" / "0" prefix */
327 *buf++ = ('X' | locase);
329 /* zero or space padding */
330 if (!(type & LEFT)) {
331 char c = (type & ZEROPAD) ? '0' : ' ';
335 /* hmm even more zero padding? */
336 while (i <= --precision)
338 /* actual digits of result */
341 /* trailing space padding */
347 static char *string(char *buf, char *s, int field_width, int precision, int flags)
354 len = strnlen(s, precision);
357 while (len < field_width--)
359 for (i = 0; i < len; ++i)
361 while (len < field_width--)
366 #ifdef CONFIG_CMD_NET
367 static char *mac_address_string(char *buf, u8 *addr, int field_width,
368 int precision, int flags)
370 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
374 for (i = 0; i < 6; i++) {
375 p = pack_hex_byte(p, addr[i]);
376 if (!(flags & SPECIAL) && i != 5)
381 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
384 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
385 int precision, int flags)
387 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
391 for (i = 0; i < 8; i++) {
392 p = pack_hex_byte(p, addr[2 * i]);
393 p = pack_hex_byte(p, addr[2 * i + 1]);
394 if (!(flags & SPECIAL) && i != 7)
399 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
402 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
403 int precision, int flags)
405 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
406 char temp[3]; /* hold each IP quad in reverse order */
410 for (i = 0; i < 4; i++) {
411 digits = put_dec_trunc(temp, addr[i]) - temp;
412 /* reverse the digits in the quad */
420 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
425 * Show a '%p' thing. A kernel extension is that the '%p' is followed
426 * by an extra set of alphanumeric characters that are extended format
429 * Right now we handle:
431 * - 'M' For a 6-byte MAC address, it prints the address in the
432 * usual colon-separated hex notation
433 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
434 * decimal for v4 and colon separated network-order 16 bit hex for v6)
435 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
438 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
439 * function pointers are really function descriptors, which contain a
440 * pointer to the real address.
442 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
445 return string(buf, "(null)", field_width, precision, flags);
447 #ifdef CONFIG_CMD_NET
453 return mac_address_string(buf, ptr, field_width, precision, flags);
459 return ip6_addr_string(buf, ptr, field_width, precision, flags);
461 return ip4_addr_string(buf, ptr, field_width, precision, flags);
467 if (field_width == -1) {
468 field_width = 2*sizeof(void *);
471 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
475 * vsprintf - Format a string and place it in a buffer
476 * @buf: The buffer to place the result into
477 * @fmt: The format string to use
478 * @args: Arguments for the format string
480 * This function follows C99 vsprintf, but has some extensions:
481 * %pS output the name of a text symbol
482 * %pF output the name of a function pointer
483 * %pR output the address range in a struct resource
485 * The function returns the number of characters written
488 * Call this function if you are already dealing with a va_list.
489 * You probably want sprintf() instead.
491 int vsprintf(char *buf, const char *fmt, va_list args)
493 unsigned NUM_TYPE num;
497 int flags; /* flags to number() */
499 int field_width; /* width of output field */
500 int precision; /* min. # of digits for integers; max
501 number of chars for from string */
502 int qualifier; /* 'h', 'l', or 'L' for integer fields */
503 /* 'z' support added 23/7/1999 S.H. */
504 /* 'z' changed to 'Z' --davidm 1/25/99 */
505 /* 't' added for ptrdiff_t */
509 for (; *fmt ; ++fmt) {
518 ++fmt; /* this also skips first '%' */
520 case '-': flags |= LEFT; goto repeat;
521 case '+': flags |= PLUS; goto repeat;
522 case ' ': flags |= SPACE; goto repeat;
523 case '#': flags |= SPECIAL; goto repeat;
524 case '0': flags |= ZEROPAD; goto repeat;
527 /* get field width */
530 field_width = skip_atoi(&fmt);
531 else if (*fmt == '*') {
533 /* it's the next argument */
534 field_width = va_arg(args, int);
535 if (field_width < 0) {
536 field_width = -field_width;
541 /* get the precision */
546 precision = skip_atoi(&fmt);
547 else if (*fmt == '*') {
549 /* it's the next argument */
550 precision = va_arg(args, int);
556 /* get the conversion qualifier */
558 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
559 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
562 if (qualifier == 'l' && *fmt == 'l') {
574 while (--field_width > 0)
576 *str++ = (unsigned char) va_arg(args, int);
577 while (--field_width > 0)
582 str = string(str, va_arg(args, char *), field_width, precision, flags);
586 str = pointer(fmt+1, str,
587 va_arg(args, void *),
588 field_width, precision, flags);
589 /* Skip all alphanumeric pointer suffixes */
590 while (isalnum(fmt[1]))
595 if (qualifier == 'l') {
596 long * ip = va_arg(args, long *);
599 int * ip = va_arg(args, int *);
608 /* integer number formats - set up the flags and "break" */
633 #ifdef CONFIG_SYS_64BIT_VSPRINTF
634 if (qualifier == 'L') /* "quad" for 64 bit variables */
635 num = va_arg(args, unsigned long long);
638 if (qualifier == 'l') {
639 num = va_arg(args, unsigned long);
641 num = (signed long) num;
642 } else if (qualifier == 'Z' || qualifier == 'z') {
643 num = va_arg(args, size_t);
644 } else if (qualifier == 't') {
645 num = va_arg(args, ptrdiff_t);
646 } else if (qualifier == 'h') {
647 num = (unsigned short) va_arg(args, int);
649 num = (signed short) num;
651 num = va_arg(args, unsigned int);
653 num = (signed int) num;
655 str = number(str, num, base, field_width, precision, flags);
662 * sprintf - Format a string and place it in a buffer
663 * @buf: The buffer to place the result into
664 * @fmt: The format string to use
665 * @...: Arguments for the format string
667 * The function returns the number of characters written
670 * See the vsprintf() documentation for format string extensions over C99.
672 int sprintf(char * buf, const char *fmt, ...)
678 i=vsprintf(buf,fmt,args);
683 void panic(const char *fmt, ...)
690 #if defined (CONFIG_PANIC_HANG)
693 udelay (100000); /* allow messages to go out */
694 do_reset (NULL, 0, 0, NULL);