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;
203 /* Possible ways to approx. divide by 10 */
204 /* gcc -O2 replaces multiply with shifts and adds */
205 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
206 // (x * 0x67) >> 10: 1100111
207 // (x * 0x34) >> 9: 110100 - same
208 // (x * 0x1a) >> 8: 11010 - same
209 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
211 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
212 q = (d0 * 0xcd) >> 11;
215 d1 = q + 9*d3 + 5*d2 + d1;
216 q = (d1 * 0xcd) >> 11;
226 q = (d3 * 0xcd) >> 11; /* - shorter code */
227 /* q = (d3 * 0x67) >> 10; - would also work */
233 /* No inlining helps gcc to use registers better */
234 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
239 return put_dec_trunc(buf, num);
240 rem = do_div(num, 100000);
241 buf = put_dec_full(buf, rem);
245 #define ZEROPAD 1 /* pad with zero */
246 #define SIGN 2 /* unsigned/signed long */
247 #define PLUS 4 /* show plus */
248 #define SPACE 8 /* space if plus */
249 #define LEFT 16 /* left justified */
250 #define SMALL 32 /* Must be 32 == 0x20 */
251 #define SPECIAL 64 /* 0x */
253 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
255 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
256 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
261 int need_pfx = ((type & SPECIAL) && base != 10);
264 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
265 * produces same digits or (maybe lowercased) letters */
266 locase = (type & SMALL);
271 if ((signed NUM_TYPE) num < 0) {
273 num = - (signed NUM_TYPE) num;
275 } else if (type & PLUS) {
278 } else if (type & SPACE) {
289 /* generate full string in tmp[], in reverse order */
293 /* Generic code, for any base:
295 tmp[i++] = (digits[do_div(num,base)] | locase);
298 else if (base != 10) { /* 8 or 16 */
301 if (base == 16) shift = 4;
303 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
306 } else { /* base 10 */
307 i = put_dec(tmp, num) - tmp;
310 /* printing 100 using %2d gives "100", not "00" */
313 /* leading space padding */
315 if (!(type & (ZEROPAD+LEFT)))
321 /* "0x" / "0" prefix */
325 *buf++ = ('X' | locase);
327 /* zero or space padding */
328 if (!(type & LEFT)) {
329 char c = (type & ZEROPAD) ? '0' : ' ';
333 /* hmm even more zero padding? */
334 while (i <= --precision)
336 /* actual digits of result */
339 /* trailing space padding */
345 static char *string(char *buf, char *s, int field_width, int precision, int flags)
352 len = strnlen(s, precision);
355 while (len < field_width--)
357 for (i = 0; i < len; ++i)
359 while (len < field_width--)
364 #ifdef CONFIG_CMD_NET
365 static char *mac_address_string(char *buf, u8 *addr, int field_width,
366 int precision, int flags)
368 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
372 for (i = 0; i < 6; i++) {
373 p = pack_hex_byte(p, addr[i]);
374 if (!(flags & SPECIAL) && i != 5)
379 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
382 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
383 int precision, int flags)
385 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
389 for (i = 0; i < 8; i++) {
390 p = pack_hex_byte(p, addr[2 * i]);
391 p = pack_hex_byte(p, addr[2 * i + 1]);
392 if (!(flags & SPECIAL) && i != 7)
397 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
400 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
401 int precision, int flags)
403 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
404 char temp[3]; /* hold each IP quad in reverse order */
408 for (i = 0; i < 4; i++) {
409 digits = put_dec_trunc(temp, addr[i]) - temp;
410 /* reverse the digits in the quad */
418 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
423 * Show a '%p' thing. A kernel extension is that the '%p' is followed
424 * by an extra set of alphanumeric characters that are extended format
427 * Right now we handle:
429 * - 'M' For a 6-byte MAC address, it prints the address in the
430 * usual colon-separated hex notation
431 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
432 * decimal for v4 and colon separated network-order 16 bit hex for v6)
433 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
436 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
437 * function pointers are really function descriptors, which contain a
438 * pointer to the real address.
440 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
443 return string(buf, "(null)", field_width, precision, flags);
445 #ifdef CONFIG_CMD_NET
451 return mac_address_string(buf, ptr, field_width, precision, flags);
457 return ip6_addr_string(buf, ptr, field_width, precision, flags);
459 return ip4_addr_string(buf, ptr, field_width, precision, flags);
465 if (field_width == -1) {
466 field_width = 2*sizeof(void *);
469 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
473 * vsprintf - Format a string and place it in a buffer
474 * @buf: The buffer to place the result into
475 * @fmt: The format string to use
476 * @args: Arguments for the format string
478 * This function follows C99 vsprintf, but has some extensions:
479 * %pS output the name of a text symbol
480 * %pF output the name of a function pointer
481 * %pR output the address range in a struct resource
483 * The function returns the number of characters written
486 * Call this function if you are already dealing with a va_list.
487 * You probably want sprintf() instead.
489 int vsprintf(char *buf, const char *fmt, va_list args)
491 unsigned NUM_TYPE num;
495 int flags; /* flags to number() */
497 int field_width; /* width of output field */
498 int precision; /* min. # of digits for integers; max
499 number of chars for from string */
500 int qualifier; /* 'h', 'l', or 'L' for integer fields */
501 /* 'z' support added 23/7/1999 S.H. */
502 /* 'z' changed to 'Z' --davidm 1/25/99 */
503 /* 't' added for ptrdiff_t */
507 for (; *fmt ; ++fmt) {
516 ++fmt; /* this also skips first '%' */
518 case '-': flags |= LEFT; goto repeat;
519 case '+': flags |= PLUS; goto repeat;
520 case ' ': flags |= SPACE; goto repeat;
521 case '#': flags |= SPECIAL; goto repeat;
522 case '0': flags |= ZEROPAD; goto repeat;
525 /* get field width */
528 field_width = skip_atoi(&fmt);
529 else if (*fmt == '*') {
531 /* it's the next argument */
532 field_width = va_arg(args, int);
533 if (field_width < 0) {
534 field_width = -field_width;
539 /* get the precision */
544 precision = skip_atoi(&fmt);
545 else if (*fmt == '*') {
547 /* it's the next argument */
548 precision = va_arg(args, int);
554 /* get the conversion qualifier */
556 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
557 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
560 if (qualifier == 'l' && *fmt == 'l') {
572 while (--field_width > 0)
574 *str++ = (unsigned char) va_arg(args, int);
575 while (--field_width > 0)
580 str = string(str, va_arg(args, char *), field_width, precision, flags);
584 str = pointer(fmt+1, str,
585 va_arg(args, void *),
586 field_width, precision, flags);
587 /* Skip all alphanumeric pointer suffixes */
588 while (isalnum(fmt[1]))
593 if (qualifier == 'l') {
594 long * ip = va_arg(args, long *);
597 int * ip = va_arg(args, int *);
606 /* integer number formats - set up the flags and "break" */
631 #ifdef CONFIG_SYS_64BIT_VSPRINTF
632 if (qualifier == 'L') /* "quad" for 64 bit variables */
633 num = va_arg(args, unsigned long long);
636 if (qualifier == 'l') {
637 num = va_arg(args, unsigned long);
639 num = (signed long) num;
640 } else if (qualifier == 'Z' || qualifier == 'z') {
641 num = va_arg(args, size_t);
642 } else if (qualifier == 't') {
643 num = va_arg(args, ptrdiff_t);
644 } else if (qualifier == 'h') {
645 num = (unsigned short) va_arg(args, int);
647 num = (signed short) num;
649 num = va_arg(args, unsigned int);
651 num = (signed int) num;
653 str = number(str, num, base, field_width, precision, flags);
660 * sprintf - Format a string and place it in a buffer
661 * @buf: The buffer to place the result into
662 * @fmt: The format string to use
663 * @...: Arguments for the format string
665 * The function returns the number of characters written
668 * See the vsprintf() documentation for format string extensions over C99.
670 int sprintf(char * buf, const char *fmt, ...)
676 i=vsprintf(buf,fmt,args);
681 void panic(const char *fmt, ...)
688 #if defined (CONFIG_PANIC_HANG)
691 udelay (100000); /* allow messages to go out */
692 do_reset (NULL, 0, 0, NULL);