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)
23 # define NUM_TYPE long long
24 #define noinline __attribute__((noinline))
26 const char hex_asc[] = "0123456789abcdef";
27 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
28 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
30 static inline char *pack_hex_byte(char *buf, u8 byte)
32 *buf++ = hex_asc_hi(byte);
33 *buf++ = hex_asc_lo(byte);
37 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
39 unsigned long result = 0,value;
43 if ((*cp == 'x') && isxdigit(cp[1])) {
54 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
55 ? toupper(*cp) : *cp)-'A'+10) < base) {
56 result = result*base + value;
64 long simple_strtol(const char *cp,char **endp,unsigned int base)
67 return -simple_strtoul(cp+1,endp,base);
68 return simple_strtoul(cp,endp,base);
71 int ustrtoul(const char *cp, char **endp, unsigned int base)
73 unsigned long result = simple_strtoul(cp, endp, base);
84 if ((*endp)[1] == 'i') {
85 if ((*endp)[2] == 'B')
94 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
96 unsigned long long result = 0, value;
100 if ((*cp == 'x') && isxdigit (cp[1])) {
111 while (isxdigit (*cp) && (value = isdigit (*cp)
113 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
114 result = result * base + value;
122 /* we use this so that we can do without the ctype library */
123 #define is_digit(c) ((c) >= '0' && (c) <= '9')
125 static int skip_atoi(const char **s)
129 while (is_digit(**s))
130 i = i*10 + *((*s)++) - '0';
134 /* Decimal conversion is by far the most typical, and is used
135 * for /proc and /sys data. This directly impacts e.g. top performance
136 * with many processes running. We optimize it for speed
138 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
139 * (with permission from the author, Douglas W. Jones). */
141 /* Formats correctly any integer in [0,99999].
142 * Outputs from one to five digits depending on input.
143 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
144 static char* put_dec_trunc(char *buf, unsigned q)
146 unsigned d3, d2, d1, d0;
151 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
152 q = (d0 * 0xcd) >> 11;
154 *buf++ = d0 + '0'; /* least significant digit */
155 d1 = q + 9*d3 + 5*d2 + d1;
157 q = (d1 * 0xcd) >> 11;
159 *buf++ = d1 + '0'; /* next digit */
162 if ((d2 != 0) || (d3 != 0)) {
165 *buf++ = d2 + '0'; /* next digit */
169 q = (d3 * 0xcd) >> 11;
171 *buf++ = d3 + '0'; /* next digit */
173 *buf++ = q + '0'; /* most sign. digit */
179 /* Same with if's removed. Always emits five digits */
180 static char* put_dec_full(char *buf, unsigned q)
182 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
183 /* but anyway, gcc produces better code with full-sized ints */
184 unsigned d3, d2, d1, d0;
190 * Possible ways to approx. divide by 10
191 * gcc -O2 replaces multiply with shifts and adds
192 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
193 * (x * 0x67) >> 10: 1100111
194 * (x * 0x34) >> 9: 110100 - same
195 * (x * 0x1a) >> 8: 11010 - same
196 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
199 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
200 q = (d0 * 0xcd) >> 11;
203 d1 = q + 9*d3 + 5*d2 + d1;
204 q = (d1 * 0xcd) >> 11;
214 q = (d3 * 0xcd) >> 11; /* - shorter code */
215 /* q = (d3 * 0x67) >> 10; - would also work */
221 /* No inlining helps gcc to use registers better */
222 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
227 return put_dec_trunc(buf, num);
228 rem = do_div(num, 100000);
229 buf = put_dec_full(buf, rem);
233 #define ZEROPAD 1 /* pad with zero */
234 #define SIGN 2 /* unsigned/signed long */
235 #define PLUS 4 /* show plus */
236 #define SPACE 8 /* space if plus */
237 #define LEFT 16 /* left justified */
238 #define SMALL 32 /* Must be 32 == 0x20 */
239 #define SPECIAL 64 /* 0x */
241 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
243 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
244 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
249 int need_pfx = ((type & SPECIAL) && base != 10);
252 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
253 * produces same digits or (maybe lowercased) letters */
254 locase = (type & SMALL);
259 if ((signed NUM_TYPE) num < 0) {
261 num = - (signed NUM_TYPE) num;
263 } else if (type & PLUS) {
266 } else if (type & SPACE) {
277 /* generate full string in tmp[], in reverse order */
281 /* Generic code, for any base:
283 tmp[i++] = (digits[do_div(num,base)] | locase);
286 else if (base != 10) { /* 8 or 16 */
289 if (base == 16) shift = 4;
291 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
294 } else { /* base 10 */
295 i = put_dec(tmp, num) - tmp;
298 /* printing 100 using %2d gives "100", not "00" */
301 /* leading space padding */
303 if (!(type & (ZEROPAD+LEFT)))
309 /* "0x" / "0" prefix */
313 *buf++ = ('X' | locase);
315 /* zero or space padding */
316 if (!(type & LEFT)) {
317 char c = (type & ZEROPAD) ? '0' : ' ';
321 /* hmm even more zero padding? */
322 while (i <= --precision)
324 /* actual digits of result */
327 /* trailing space padding */
333 static char *string(char *buf, char *s, int field_width, int precision, int flags)
340 len = strnlen(s, precision);
343 while (len < field_width--)
345 for (i = 0; i < len; ++i)
347 while (len < field_width--)
352 #ifdef CONFIG_CMD_NET
353 static char *mac_address_string(char *buf, u8 *addr, int field_width,
354 int precision, int flags)
356 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
360 for (i = 0; i < 6; i++) {
361 p = pack_hex_byte(p, addr[i]);
362 if (!(flags & SPECIAL) && i != 5)
367 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
370 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
371 int precision, int flags)
373 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
377 for (i = 0; i < 8; i++) {
378 p = pack_hex_byte(p, addr[2 * i]);
379 p = pack_hex_byte(p, addr[2 * i + 1]);
380 if (!(flags & SPECIAL) && i != 7)
385 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
388 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
389 int precision, int flags)
391 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
392 char temp[3]; /* hold each IP quad in reverse order */
396 for (i = 0; i < 4; i++) {
397 digits = put_dec_trunc(temp, addr[i]) - temp;
398 /* reverse the digits in the quad */
406 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
411 * Show a '%p' thing. A kernel extension is that the '%p' is followed
412 * by an extra set of alphanumeric characters that are extended format
415 * Right now we handle:
417 * - 'M' For a 6-byte MAC address, it prints the address in the
418 * usual colon-separated hex notation
419 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
420 * decimal for v4 and colon separated network-order 16 bit hex for v6)
421 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
424 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
425 * function pointers are really function descriptors, which contain a
426 * pointer to the real address.
428 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
431 return string(buf, "(null)", field_width, precision, flags);
433 #ifdef CONFIG_CMD_NET
439 return mac_address_string(buf, ptr, field_width, precision, flags);
445 return ip6_addr_string(buf, ptr, field_width, precision, flags);
447 return ip4_addr_string(buf, ptr, field_width, precision, flags);
453 if (field_width == -1) {
454 field_width = 2*sizeof(void *);
457 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
461 * vsprintf - Format a string and place it in a buffer
462 * @buf: The buffer to place the result into
463 * @fmt: The format string to use
464 * @args: Arguments for the format string
466 * This function follows C99 vsprintf, but has some extensions:
467 * %pS output the name of a text symbol
468 * %pF output the name of a function pointer
469 * %pR output the address range in a struct resource
471 * The function returns the number of characters written
474 * Call this function if you are already dealing with a va_list.
475 * You probably want sprintf() instead.
477 int vsprintf(char *buf, const char *fmt, va_list args)
479 unsigned NUM_TYPE num;
483 int flags; /* flags to number() */
485 int field_width; /* width of output field */
486 int precision; /* min. # of digits for integers; max
487 number of chars for from string */
488 int qualifier; /* 'h', 'l', or 'L' for integer fields */
489 /* 'z' support added 23/7/1999 S.H. */
490 /* 'z' changed to 'Z' --davidm 1/25/99 */
491 /* 't' added for ptrdiff_t */
495 for (; *fmt ; ++fmt) {
504 ++fmt; /* this also skips first '%' */
506 case '-': flags |= LEFT; goto repeat;
507 case '+': flags |= PLUS; goto repeat;
508 case ' ': flags |= SPACE; goto repeat;
509 case '#': flags |= SPECIAL; goto repeat;
510 case '0': flags |= ZEROPAD; goto repeat;
513 /* get field width */
516 field_width = skip_atoi(&fmt);
517 else if (*fmt == '*') {
519 /* it's the next argument */
520 field_width = va_arg(args, int);
521 if (field_width < 0) {
522 field_width = -field_width;
527 /* get the precision */
532 precision = skip_atoi(&fmt);
533 else if (*fmt == '*') {
535 /* it's the next argument */
536 precision = va_arg(args, int);
542 /* get the conversion qualifier */
544 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
545 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
548 if (qualifier == 'l' && *fmt == 'l') {
560 while (--field_width > 0)
562 *str++ = (unsigned char) va_arg(args, int);
563 while (--field_width > 0)
568 str = string(str, va_arg(args, char *), field_width, precision, flags);
572 str = pointer(fmt+1, str,
573 va_arg(args, void *),
574 field_width, precision, flags);
575 /* Skip all alphanumeric pointer suffixes */
576 while (isalnum(fmt[1]))
581 if (qualifier == 'l') {
582 long * ip = va_arg(args, long *);
585 int * ip = va_arg(args, int *);
594 /* integer number formats - set up the flags and "break" */
619 if (qualifier == 'L') /* "quad" for 64 bit variables */
620 num = va_arg(args, unsigned long long);
621 else if (qualifier == 'l') {
622 num = va_arg(args, unsigned long);
624 num = (signed long) num;
625 } else if (qualifier == 'Z' || qualifier == 'z') {
626 num = va_arg(args, size_t);
627 } else if (qualifier == 't') {
628 num = va_arg(args, ptrdiff_t);
629 } else if (qualifier == 'h') {
630 num = (unsigned short) va_arg(args, int);
632 num = (signed short) num;
634 num = va_arg(args, unsigned int);
636 num = (signed int) num;
638 str = number(str, num, base, field_width, precision, flags);
645 * sprintf - Format a string and place it in a buffer
646 * @buf: The buffer to place the result into
647 * @fmt: The format string to use
648 * @...: Arguments for the format string
650 * The function returns the number of characters written
653 * See the vsprintf() documentation for format string extensions over C99.
655 int sprintf(char * buf, const char *fmt, ...)
661 i=vsprintf(buf,fmt,args);
666 void panic(const char *fmt, ...)
673 #if defined (CONFIG_PANIC_HANG)
676 udelay (100000); /* allow messages to go out */
677 do_reset (NULL, 0, 0, NULL);