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 * const argv[]);
25 # define NUM_TYPE long long
26 #define noinline __attribute__((noinline))
28 const char hex_asc[] = "0123456789abcdef";
29 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
30 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
32 static inline char *pack_hex_byte(char *buf, u8 byte)
34 *buf++ = hex_asc_hi(byte);
35 *buf++ = hex_asc_lo(byte);
39 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
41 unsigned long result = 0,value;
45 if ((*cp == 'x') && isxdigit(cp[1])) {
56 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
57 ? toupper(*cp) : *cp)-'A'+10) < base) {
58 result = result*base + value;
66 long simple_strtol(const char *cp,char **endp,unsigned int base)
69 return -simple_strtoul(cp+1,endp,base);
70 return simple_strtoul(cp,endp,base);
73 int ustrtoul(const char *cp, char **endp, unsigned int base)
75 unsigned long result = simple_strtoul(cp, endp, base);
86 if ((*endp)[1] == 'i') {
87 if ((*endp)[2] == 'B')
96 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
98 unsigned long long result = 0, value;
102 if ((*cp == 'x') && isxdigit (cp[1])) {
113 while (isxdigit (*cp) && (value = isdigit (*cp)
115 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
116 result = result * base + value;
124 /* we use this so that we can do without the ctype library */
125 #define is_digit(c) ((c) >= '0' && (c) <= '9')
127 static int skip_atoi(const char **s)
131 while (is_digit(**s))
132 i = i*10 + *((*s)++) - '0';
136 /* Decimal conversion is by far the most typical, and is used
137 * for /proc and /sys data. This directly impacts e.g. top performance
138 * with many processes running. We optimize it for speed
140 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
141 * (with permission from the author, Douglas W. Jones). */
143 /* Formats correctly any integer in [0,99999].
144 * Outputs from one to five digits depending on input.
145 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
146 static char* put_dec_trunc(char *buf, unsigned q)
148 unsigned d3, d2, d1, d0;
153 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
154 q = (d0 * 0xcd) >> 11;
156 *buf++ = d0 + '0'; /* least significant digit */
157 d1 = q + 9*d3 + 5*d2 + d1;
159 q = (d1 * 0xcd) >> 11;
161 *buf++ = d1 + '0'; /* next digit */
164 if ((d2 != 0) || (d3 != 0)) {
167 *buf++ = d2 + '0'; /* next digit */
171 q = (d3 * 0xcd) >> 11;
173 *buf++ = d3 + '0'; /* next digit */
175 *buf++ = q + '0'; /* most sign. digit */
181 /* Same with if's removed. Always emits five digits */
182 static char* put_dec_full(char *buf, unsigned q)
184 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
185 /* but anyway, gcc produces better code with full-sized ints */
186 unsigned d3, d2, d1, d0;
192 * Possible ways to approx. divide by 10
193 * gcc -O2 replaces multiply with shifts and adds
194 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
195 * (x * 0x67) >> 10: 1100111
196 * (x * 0x34) >> 9: 110100 - same
197 * (x * 0x1a) >> 8: 11010 - same
198 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
201 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
202 q = (d0 * 0xcd) >> 11;
205 d1 = q + 9*d3 + 5*d2 + d1;
206 q = (d1 * 0xcd) >> 11;
216 q = (d3 * 0xcd) >> 11; /* - shorter code */
217 /* q = (d3 * 0x67) >> 10; - would also work */
223 /* No inlining helps gcc to use registers better */
224 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
229 return put_dec_trunc(buf, num);
230 rem = do_div(num, 100000);
231 buf = put_dec_full(buf, rem);
235 #define ZEROPAD 1 /* pad with zero */
236 #define SIGN 2 /* unsigned/signed long */
237 #define PLUS 4 /* show plus */
238 #define SPACE 8 /* space if plus */
239 #define LEFT 16 /* left justified */
240 #define SMALL 32 /* Must be 32 == 0x20 */
241 #define SPECIAL 64 /* 0x */
243 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
245 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
246 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
251 int need_pfx = ((type & SPECIAL) && base != 10);
254 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
255 * produces same digits or (maybe lowercased) letters */
256 locase = (type & SMALL);
261 if ((signed NUM_TYPE) num < 0) {
263 num = - (signed NUM_TYPE) num;
265 } else if (type & PLUS) {
268 } else if (type & SPACE) {
279 /* generate full string in tmp[], in reverse order */
283 /* Generic code, for any base:
285 tmp[i++] = (digits[do_div(num,base)] | locase);
288 else if (base != 10) { /* 8 or 16 */
291 if (base == 16) shift = 4;
293 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
296 } else { /* base 10 */
297 i = put_dec(tmp, num) - tmp;
300 /* printing 100 using %2d gives "100", not "00" */
303 /* leading space padding */
305 if (!(type & (ZEROPAD+LEFT)))
311 /* "0x" / "0" prefix */
315 *buf++ = ('X' | locase);
317 /* zero or space padding */
318 if (!(type & LEFT)) {
319 char c = (type & ZEROPAD) ? '0' : ' ';
323 /* hmm even more zero padding? */
324 while (i <= --precision)
326 /* actual digits of result */
329 /* trailing space padding */
335 static char *string(char *buf, char *s, int field_width, int precision, int flags)
342 len = strnlen(s, precision);
345 while (len < field_width--)
347 for (i = 0; i < len; ++i)
349 while (len < field_width--)
354 #ifdef CONFIG_CMD_NET
355 static char *mac_address_string(char *buf, u8 *addr, int field_width,
356 int precision, int flags)
358 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
362 for (i = 0; i < 6; i++) {
363 p = pack_hex_byte(p, addr[i]);
364 if (!(flags & SPECIAL) && i != 5)
369 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
372 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
373 int precision, int flags)
375 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
379 for (i = 0; i < 8; i++) {
380 p = pack_hex_byte(p, addr[2 * i]);
381 p = pack_hex_byte(p, addr[2 * i + 1]);
382 if (!(flags & SPECIAL) && i != 7)
387 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
390 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
391 int precision, int flags)
393 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
394 char temp[3]; /* hold each IP quad in reverse order */
398 for (i = 0; i < 4; i++) {
399 digits = put_dec_trunc(temp, addr[i]) - temp;
400 /* reverse the digits in the quad */
408 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
413 * Show a '%p' thing. A kernel extension is that the '%p' is followed
414 * by an extra set of alphanumeric characters that are extended format
417 * Right now we handle:
419 * - 'M' For a 6-byte MAC address, it prints the address in the
420 * usual colon-separated hex notation
421 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
422 * decimal for v4 and colon separated network-order 16 bit hex for v6)
423 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
426 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
427 * function pointers are really function descriptors, which contain a
428 * pointer to the real address.
430 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
433 return string(buf, "(null)", field_width, precision, flags);
435 #ifdef CONFIG_CMD_NET
441 return mac_address_string(buf, ptr, field_width, precision, flags);
447 return ip6_addr_string(buf, ptr, field_width, precision, flags);
449 return ip4_addr_string(buf, ptr, field_width, precision, flags);
455 if (field_width == -1) {
456 field_width = 2*sizeof(void *);
459 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
463 * vsprintf - Format a string and place it in a buffer
464 * @buf: The buffer to place the result into
465 * @fmt: The format string to use
466 * @args: Arguments for the format string
468 * This function follows C99 vsprintf, but has some extensions:
469 * %pS output the name of a text symbol
470 * %pF output the name of a function pointer
471 * %pR output the address range in a struct resource
473 * The function returns the number of characters written
476 * Call this function if you are already dealing with a va_list.
477 * You probably want sprintf() instead.
479 int vsprintf(char *buf, const char *fmt, va_list args)
481 unsigned NUM_TYPE num;
485 int flags; /* flags to number() */
487 int field_width; /* width of output field */
488 int precision; /* min. # of digits for integers; max
489 number of chars for from string */
490 int qualifier; /* 'h', 'l', or 'L' for integer fields */
491 /* 'z' support added 23/7/1999 S.H. */
492 /* 'z' changed to 'Z' --davidm 1/25/99 */
493 /* 't' added for ptrdiff_t */
497 for (; *fmt ; ++fmt) {
506 ++fmt; /* this also skips first '%' */
508 case '-': flags |= LEFT; goto repeat;
509 case '+': flags |= PLUS; goto repeat;
510 case ' ': flags |= SPACE; goto repeat;
511 case '#': flags |= SPECIAL; goto repeat;
512 case '0': flags |= ZEROPAD; goto repeat;
515 /* get field width */
518 field_width = skip_atoi(&fmt);
519 else if (*fmt == '*') {
521 /* it's the next argument */
522 field_width = va_arg(args, int);
523 if (field_width < 0) {
524 field_width = -field_width;
529 /* get the precision */
534 precision = skip_atoi(&fmt);
535 else if (*fmt == '*') {
537 /* it's the next argument */
538 precision = va_arg(args, int);
544 /* get the conversion qualifier */
546 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
547 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
550 if (qualifier == 'l' && *fmt == 'l') {
562 while (--field_width > 0)
564 *str++ = (unsigned char) va_arg(args, int);
565 while (--field_width > 0)
570 str = string(str, va_arg(args, char *), field_width, precision, flags);
574 str = pointer(fmt+1, str,
575 va_arg(args, void *),
576 field_width, precision, flags);
577 /* Skip all alphanumeric pointer suffixes */
578 while (isalnum(fmt[1]))
583 if (qualifier == 'l') {
584 long * ip = va_arg(args, long *);
587 int * ip = va_arg(args, int *);
596 /* integer number formats - set up the flags and "break" */
621 if (qualifier == 'L') /* "quad" for 64 bit variables */
622 num = va_arg(args, unsigned long long);
623 else if (qualifier == 'l') {
624 num = va_arg(args, unsigned long);
626 num = (signed long) num;
627 } else if (qualifier == 'Z' || qualifier == 'z') {
628 num = va_arg(args, size_t);
629 } else if (qualifier == 't') {
630 num = va_arg(args, ptrdiff_t);
631 } else if (qualifier == 'h') {
632 num = (unsigned short) va_arg(args, int);
634 num = (signed short) num;
636 num = va_arg(args, unsigned int);
638 num = (signed int) num;
640 str = number(str, num, base, field_width, precision, flags);
647 * sprintf - Format a string and place it in a buffer
648 * @buf: The buffer to place the result into
649 * @fmt: The format string to use
650 * @...: Arguments for the format string
652 * The function returns the number of characters written
655 * See the vsprintf() documentation for format string extensions over C99.
657 int sprintf(char * buf, const char *fmt, ...)
663 i=vsprintf(buf,fmt,args);
668 void panic(const char *fmt, ...)
675 #if defined (CONFIG_PANIC_HANG)
678 udelay (100000); /* allow messages to go out */
679 do_reset (NULL, 0, 0, NULL);