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
26 # define NUM_TYPE long long
28 # define NUM_TYPE long
29 #define do_div(n, base) ({ \
31 __res = ((unsigned NUM_TYPE) n) % base; \
32 n = ((unsigned NUM_TYPE) n) / base; \
36 #define noinline __attribute__((noinline))
39 const char hex_asc[] = "0123456789abcdef";
40 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
41 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
43 static inline char *pack_hex_byte(char *buf, u8 byte)
45 *buf++ = hex_asc_hi(byte);
46 *buf++ = hex_asc_lo(byte);
50 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
52 unsigned long result = 0,value;
56 if ((*cp == 'x') && isxdigit(cp[1])) {
67 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
68 ? toupper(*cp) : *cp)-'A'+10) < base) {
69 result = result*base + value;
77 long simple_strtol(const char *cp,char **endp,unsigned int base)
80 return -simple_strtoul(cp+1,endp,base);
81 return simple_strtoul(cp,endp,base);
84 int ustrtoul(const char *cp, char **endp, unsigned int base)
86 unsigned long result = simple_strtoul(cp, endp, base);
97 if ((*endp)[1] == 'i') {
98 if ((*endp)[2] == 'B')
107 #ifdef CONFIG_SYS_64BIT_STRTOUL
108 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
110 unsigned long long result = 0, value;
114 if ((*cp == 'x') && isxdigit (cp[1])) {
125 while (isxdigit (*cp) && (value = isdigit (*cp)
127 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
128 result = result * base + value;
135 #endif /* CONFIG_SYS_64BIT_STRTOUL */
137 /* we use this so that we can do without the ctype library */
138 #define is_digit(c) ((c) >= '0' && (c) <= '9')
140 static int skip_atoi(const char **s)
144 while (is_digit(**s))
145 i = i*10 + *((*s)++) - '0';
149 /* Decimal conversion is by far the most typical, and is used
150 * for /proc and /sys data. This directly impacts e.g. top performance
151 * with many processes running. We optimize it for speed
153 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
154 * (with permission from the author, Douglas W. Jones). */
156 /* Formats correctly any integer in [0,99999].
157 * Outputs from one to five digits depending on input.
158 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
159 static char* put_dec_trunc(char *buf, unsigned q)
161 unsigned d3, d2, d1, d0;
166 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
167 q = (d0 * 0xcd) >> 11;
169 *buf++ = d0 + '0'; /* least significant digit */
170 d1 = q + 9*d3 + 5*d2 + d1;
172 q = (d1 * 0xcd) >> 11;
174 *buf++ = d1 + '0'; /* next digit */
177 if ((d2 != 0) || (d3 != 0)) {
180 *buf++ = d2 + '0'; /* next digit */
184 q = (d3 * 0xcd) >> 11;
186 *buf++ = d3 + '0'; /* next digit */
188 *buf++ = q + '0'; /* most sign. digit */
194 /* Same with if's removed. Always emits five digits */
195 static char* put_dec_full(char *buf, unsigned q)
197 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
198 /* but anyway, gcc produces better code with full-sized ints */
199 unsigned d3, d2, d1, d0;
205 * Possible ways to approx. divide by 10
206 * gcc -O2 replaces multiply with shifts and adds
207 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
208 * (x * 0x67) >> 10: 1100111
209 * (x * 0x34) >> 9: 110100 - same
210 * (x * 0x1a) >> 8: 11010 - same
211 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
214 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
215 q = (d0 * 0xcd) >> 11;
218 d1 = q + 9*d3 + 5*d2 + d1;
219 q = (d1 * 0xcd) >> 11;
229 q = (d3 * 0xcd) >> 11; /* - shorter code */
230 /* q = (d3 * 0x67) >> 10; - would also work */
236 /* No inlining helps gcc to use registers better */
237 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
242 return put_dec_trunc(buf, num);
243 rem = do_div(num, 100000);
244 buf = put_dec_full(buf, rem);
248 #define ZEROPAD 1 /* pad with zero */
249 #define SIGN 2 /* unsigned/signed long */
250 #define PLUS 4 /* show plus */
251 #define SPACE 8 /* space if plus */
252 #define LEFT 16 /* left justified */
253 #define SMALL 32 /* Must be 32 == 0x20 */
254 #define SPECIAL 64 /* 0x */
256 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
258 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
259 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
264 int need_pfx = ((type & SPECIAL) && base != 10);
267 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
268 * produces same digits or (maybe lowercased) letters */
269 locase = (type & SMALL);
274 if ((signed NUM_TYPE) num < 0) {
276 num = - (signed NUM_TYPE) num;
278 } else if (type & PLUS) {
281 } else if (type & SPACE) {
292 /* generate full string in tmp[], in reverse order */
296 /* Generic code, for any base:
298 tmp[i++] = (digits[do_div(num,base)] | locase);
301 else if (base != 10) { /* 8 or 16 */
304 if (base == 16) shift = 4;
306 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
309 } else { /* base 10 */
310 i = put_dec(tmp, num) - tmp;
313 /* printing 100 using %2d gives "100", not "00" */
316 /* leading space padding */
318 if (!(type & (ZEROPAD+LEFT)))
324 /* "0x" / "0" prefix */
328 *buf++ = ('X' | locase);
330 /* zero or space padding */
331 if (!(type & LEFT)) {
332 char c = (type & ZEROPAD) ? '0' : ' ';
336 /* hmm even more zero padding? */
337 while (i <= --precision)
339 /* actual digits of result */
342 /* trailing space padding */
348 static char *string(char *buf, char *s, int field_width, int precision, int flags)
355 len = strnlen(s, precision);
358 while (len < field_width--)
360 for (i = 0; i < len; ++i)
362 while (len < field_width--)
367 #ifdef CONFIG_CMD_NET
368 static char *mac_address_string(char *buf, u8 *addr, int field_width,
369 int precision, int flags)
371 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
375 for (i = 0; i < 6; i++) {
376 p = pack_hex_byte(p, addr[i]);
377 if (!(flags & SPECIAL) && i != 5)
382 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
385 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
386 int precision, int flags)
388 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
392 for (i = 0; i < 8; i++) {
393 p = pack_hex_byte(p, addr[2 * i]);
394 p = pack_hex_byte(p, addr[2 * i + 1]);
395 if (!(flags & SPECIAL) && i != 7)
400 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
403 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
404 int precision, int flags)
406 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
407 char temp[3]; /* hold each IP quad in reverse order */
411 for (i = 0; i < 4; i++) {
412 digits = put_dec_trunc(temp, addr[i]) - temp;
413 /* reverse the digits in the quad */
421 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
426 * Show a '%p' thing. A kernel extension is that the '%p' is followed
427 * by an extra set of alphanumeric characters that are extended format
430 * Right now we handle:
432 * - 'M' For a 6-byte MAC address, it prints the address in the
433 * usual colon-separated hex notation
434 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
435 * decimal for v4 and colon separated network-order 16 bit hex for v6)
436 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
439 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
440 * function pointers are really function descriptors, which contain a
441 * pointer to the real address.
443 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
446 return string(buf, "(null)", field_width, precision, flags);
448 #ifdef CONFIG_CMD_NET
454 return mac_address_string(buf, ptr, field_width, precision, flags);
460 return ip6_addr_string(buf, ptr, field_width, precision, flags);
462 return ip4_addr_string(buf, ptr, field_width, precision, flags);
468 if (field_width == -1) {
469 field_width = 2*sizeof(void *);
472 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
476 * vsprintf - Format a string and place it in a buffer
477 * @buf: The buffer to place the result into
478 * @fmt: The format string to use
479 * @args: Arguments for the format string
481 * This function follows C99 vsprintf, but has some extensions:
482 * %pS output the name of a text symbol
483 * %pF output the name of a function pointer
484 * %pR output the address range in a struct resource
486 * The function returns the number of characters written
489 * Call this function if you are already dealing with a va_list.
490 * You probably want sprintf() instead.
492 int vsprintf(char *buf, const char *fmt, va_list args)
494 unsigned NUM_TYPE num;
498 int flags; /* flags to number() */
500 int field_width; /* width of output field */
501 int precision; /* min. # of digits for integers; max
502 number of chars for from string */
503 int qualifier; /* 'h', 'l', or 'L' for integer fields */
504 /* 'z' support added 23/7/1999 S.H. */
505 /* 'z' changed to 'Z' --davidm 1/25/99 */
506 /* 't' added for ptrdiff_t */
510 for (; *fmt ; ++fmt) {
519 ++fmt; /* this also skips first '%' */
521 case '-': flags |= LEFT; goto repeat;
522 case '+': flags |= PLUS; goto repeat;
523 case ' ': flags |= SPACE; goto repeat;
524 case '#': flags |= SPECIAL; goto repeat;
525 case '0': flags |= ZEROPAD; goto repeat;
528 /* get field width */
531 field_width = skip_atoi(&fmt);
532 else if (*fmt == '*') {
534 /* it's the next argument */
535 field_width = va_arg(args, int);
536 if (field_width < 0) {
537 field_width = -field_width;
542 /* get the precision */
547 precision = skip_atoi(&fmt);
548 else if (*fmt == '*') {
550 /* it's the next argument */
551 precision = va_arg(args, int);
557 /* get the conversion qualifier */
559 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
560 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
563 if (qualifier == 'l' && *fmt == 'l') {
575 while (--field_width > 0)
577 *str++ = (unsigned char) va_arg(args, int);
578 while (--field_width > 0)
583 str = string(str, va_arg(args, char *), field_width, precision, flags);
587 str = pointer(fmt+1, str,
588 va_arg(args, void *),
589 field_width, precision, flags);
590 /* Skip all alphanumeric pointer suffixes */
591 while (isalnum(fmt[1]))
596 if (qualifier == 'l') {
597 long * ip = va_arg(args, long *);
600 int * ip = va_arg(args, int *);
609 /* integer number formats - set up the flags and "break" */
634 #ifdef CONFIG_SYS_64BIT_VSPRINTF
635 if (qualifier == 'L') /* "quad" for 64 bit variables */
636 num = va_arg(args, unsigned long long);
639 if (qualifier == 'l') {
640 num = va_arg(args, unsigned long);
642 num = (signed long) num;
643 } else if (qualifier == 'Z' || qualifier == 'z') {
644 num = va_arg(args, size_t);
645 } else if (qualifier == 't') {
646 num = va_arg(args, ptrdiff_t);
647 } else if (qualifier == 'h') {
648 num = (unsigned short) va_arg(args, int);
650 num = (signed short) num;
652 num = va_arg(args, unsigned int);
654 num = (signed int) num;
656 str = number(str, num, base, field_width, precision, flags);
663 * sprintf - Format a string and place it in a buffer
664 * @buf: The buffer to place the result into
665 * @fmt: The format string to use
666 * @...: Arguments for the format string
668 * The function returns the number of characters written
671 * See the vsprintf() documentation for format string extensions over C99.
673 int sprintf(char * buf, const char *fmt, ...)
679 i=vsprintf(buf,fmt,args);
684 void panic(const char *fmt, ...)
691 #if defined (CONFIG_PANIC_HANG)
694 udelay (100000); /* allow messages to go out */
695 do_reset (NULL, 0, 0, NULL);