4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 * from hush: simple_itoa() was lifted from boa-0.93.15
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
21 #if !defined (CONFIG_PANIC_HANG)
26 # define NUM_TYPE long long
27 #define noinline __attribute__((noinline))
29 const char hex_asc[] = "0123456789abcdef";
30 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
31 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
33 static inline char *pack_hex_byte(char *buf, u8 byte)
35 *buf++ = hex_asc_hi(byte);
36 *buf++ = hex_asc_lo(byte);
40 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
42 unsigned long result = 0,value;
46 if ((*cp == 'x') && isxdigit(cp[1])) {
57 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
58 ? toupper(*cp) : *cp)-'A'+10) < base) {
59 result = result*base + value;
68 * strict_strtoul - convert a string to an unsigned long strictly
69 * @cp: The string to be converted
70 * @base: The number base to use
71 * @res: The converted result value
73 * strict_strtoul converts a string to an unsigned long only if the
74 * string is really an unsigned long string, any string containing
75 * any invalid char at the tail will be rejected and -EINVAL is returned,
76 * only a newline char at the tail is acceptible because people generally
77 * change a module parameter in the following way:
79 * echo 1024 > /sys/module/e1000/parameters/copybreak
81 * echo will append a newline to the tail.
83 * It returns 0 if conversion is successful and *res is set to the converted
84 * value, otherwise it returns -EINVAL and *res is set to 0.
86 * simple_strtoul just ignores the successive invalid characters and
87 * return the converted value of prefix part of the string.
89 * Copied this function from Linux 2.6.38 commit ID:
90 * 521cb40b0c44418a4fd36dc633f575813d59a43d
93 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
104 val = simple_strtoul(cp, &tail, base);
108 if ((*tail == '\0') ||
109 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
117 long simple_strtol(const char *cp,char **endp,unsigned int base)
120 return -simple_strtoul(cp+1,endp,base);
121 return simple_strtoul(cp,endp,base);
124 int ustrtoul(const char *cp, char **endp, unsigned int base)
126 unsigned long result = simple_strtoul(cp, endp, base);
137 if ((*endp)[1] == 'i') {
138 if ((*endp)[2] == 'B')
147 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
149 unsigned long long result = 0, value;
153 if ((*cp == 'x') && isxdigit (cp[1])) {
164 while (isxdigit (*cp) && (value = isdigit (*cp)
166 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
167 result = result * base + value;
175 /* we use this so that we can do without the ctype library */
176 #define is_digit(c) ((c) >= '0' && (c) <= '9')
178 static int skip_atoi(const char **s)
182 while (is_digit(**s))
183 i = i*10 + *((*s)++) - '0';
187 /* Decimal conversion is by far the most typical, and is used
188 * for /proc and /sys data. This directly impacts e.g. top performance
189 * with many processes running. We optimize it for speed
191 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
192 * (with permission from the author, Douglas W. Jones). */
194 /* Formats correctly any integer in [0,99999].
195 * Outputs from one to five digits depending on input.
196 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
197 static char* put_dec_trunc(char *buf, unsigned q)
199 unsigned d3, d2, d1, d0;
204 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
205 q = (d0 * 0xcd) >> 11;
207 *buf++ = d0 + '0'; /* least significant digit */
208 d1 = q + 9*d3 + 5*d2 + d1;
210 q = (d1 * 0xcd) >> 11;
212 *buf++ = d1 + '0'; /* next digit */
215 if ((d2 != 0) || (d3 != 0)) {
218 *buf++ = d2 + '0'; /* next digit */
222 q = (d3 * 0xcd) >> 11;
224 *buf++ = d3 + '0'; /* next digit */
226 *buf++ = q + '0'; /* most sign. digit */
232 /* Same with if's removed. Always emits five digits */
233 static char* put_dec_full(char *buf, unsigned q)
235 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
236 /* but anyway, gcc produces better code with full-sized ints */
237 unsigned d3, d2, d1, d0;
243 * Possible ways to approx. divide by 10
244 * gcc -O2 replaces multiply with shifts and adds
245 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
246 * (x * 0x67) >> 10: 1100111
247 * (x * 0x34) >> 9: 110100 - same
248 * (x * 0x1a) >> 8: 11010 - same
249 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
252 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
253 q = (d0 * 0xcd) >> 11;
256 d1 = q + 9*d3 + 5*d2 + d1;
257 q = (d1 * 0xcd) >> 11;
267 q = (d3 * 0xcd) >> 11; /* - shorter code */
268 /* q = (d3 * 0x67) >> 10; - would also work */
274 /* No inlining helps gcc to use registers better */
275 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
280 return put_dec_trunc(buf, num);
281 rem = do_div(num, 100000);
282 buf = put_dec_full(buf, rem);
286 #define ZEROPAD 1 /* pad with zero */
287 #define SIGN 2 /* unsigned/signed long */
288 #define PLUS 4 /* show plus */
289 #define SPACE 8 /* space if plus */
290 #define LEFT 16 /* left justified */
291 #define SMALL 32 /* Must be 32 == 0x20 */
292 #define SPECIAL 64 /* 0x */
294 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
296 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
297 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
302 int need_pfx = ((type & SPECIAL) && base != 10);
305 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
306 * produces same digits or (maybe lowercased) letters */
307 locase = (type & SMALL);
312 if ((signed NUM_TYPE) num < 0) {
314 num = - (signed NUM_TYPE) num;
316 } else if (type & PLUS) {
319 } else if (type & SPACE) {
330 /* generate full string in tmp[], in reverse order */
334 /* Generic code, for any base:
336 tmp[i++] = (digits[do_div(num,base)] | locase);
339 else if (base != 10) { /* 8 or 16 */
342 if (base == 16) shift = 4;
344 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
347 } else { /* base 10 */
348 i = put_dec(tmp, num) - tmp;
351 /* printing 100 using %2d gives "100", not "00" */
354 /* leading space padding */
356 if (!(type & (ZEROPAD+LEFT)))
362 /* "0x" / "0" prefix */
366 *buf++ = ('X' | locase);
368 /* zero or space padding */
369 if (!(type & LEFT)) {
370 char c = (type & ZEROPAD) ? '0' : ' ';
374 /* hmm even more zero padding? */
375 while (i <= --precision)
377 /* actual digits of result */
380 /* trailing space padding */
386 static char *string(char *buf, char *s, int field_width, int precision, int flags)
393 len = strnlen(s, precision);
396 while (len < field_width--)
398 for (i = 0; i < len; ++i)
400 while (len < field_width--)
405 #ifdef CONFIG_CMD_NET
406 static char *mac_address_string(char *buf, u8 *addr, int field_width,
407 int precision, int flags)
409 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
413 for (i = 0; i < 6; i++) {
414 p = pack_hex_byte(p, addr[i]);
415 if (!(flags & SPECIAL) && i != 5)
420 return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
423 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
424 int precision, int flags)
426 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
430 for (i = 0; i < 8; i++) {
431 p = pack_hex_byte(p, addr[2 * i]);
432 p = pack_hex_byte(p, addr[2 * i + 1]);
433 if (!(flags & SPECIAL) && i != 7)
438 return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
441 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
442 int precision, int flags)
444 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
445 char temp[3]; /* hold each IP quad in reverse order */
449 for (i = 0; i < 4; i++) {
450 digits = put_dec_trunc(temp, addr[i]) - temp;
451 /* reverse the digits in the quad */
459 return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
464 * Show a '%p' thing. A kernel extension is that the '%p' is followed
465 * by an extra set of alphanumeric characters that are extended format
468 * Right now we handle:
470 * - 'M' For a 6-byte MAC address, it prints the address in the
471 * usual colon-separated hex notation
472 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
473 * decimal for v4 and colon separated network-order 16 bit hex for v6)
474 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
477 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
478 * function pointers are really function descriptors, which contain a
479 * pointer to the real address.
481 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
484 return string(buf, "(null)", field_width, precision, flags);
486 #ifdef CONFIG_CMD_NET
492 return mac_address_string(buf, ptr, field_width, precision, flags);
498 return ip6_addr_string(buf, ptr, field_width, precision, flags);
500 return ip4_addr_string(buf, ptr, field_width, precision, flags);
506 if (field_width == -1) {
507 field_width = 2*sizeof(void *);
510 return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
514 * vsprintf - Format a string and place it in a buffer
515 * @buf: The buffer to place the result into
516 * @fmt: The format string to use
517 * @args: Arguments for the format string
519 * This function follows C99 vsprintf, but has some extensions:
520 * %pS output the name of a text symbol
521 * %pF output the name of a function pointer
522 * %pR output the address range in a struct resource
524 * The function returns the number of characters written
527 * Call this function if you are already dealing with a va_list.
528 * You probably want sprintf() instead.
530 int vsprintf(char *buf, const char *fmt, va_list args)
532 unsigned NUM_TYPE num;
536 int flags; /* flags to number() */
538 int field_width; /* width of output field */
539 int precision; /* min. # of digits for integers; max
540 number of chars for from string */
541 int qualifier; /* 'h', 'l', or 'L' for integer fields */
542 /* 'z' support added 23/7/1999 S.H. */
543 /* 'z' changed to 'Z' --davidm 1/25/99 */
544 /* 't' added for ptrdiff_t */
548 for (; *fmt ; ++fmt) {
557 ++fmt; /* this also skips first '%' */
559 case '-': flags |= LEFT; goto repeat;
560 case '+': flags |= PLUS; goto repeat;
561 case ' ': flags |= SPACE; goto repeat;
562 case '#': flags |= SPECIAL; goto repeat;
563 case '0': flags |= ZEROPAD; goto repeat;
566 /* get field width */
569 field_width = skip_atoi(&fmt);
570 else if (*fmt == '*') {
572 /* it's the next argument */
573 field_width = va_arg(args, int);
574 if (field_width < 0) {
575 field_width = -field_width;
580 /* get the precision */
585 precision = skip_atoi(&fmt);
586 else if (*fmt == '*') {
588 /* it's the next argument */
589 precision = va_arg(args, int);
595 /* get the conversion qualifier */
597 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
598 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
601 if (qualifier == 'l' && *fmt == 'l') {
613 while (--field_width > 0)
615 *str++ = (unsigned char) va_arg(args, int);
616 while (--field_width > 0)
621 str = string(str, va_arg(args, char *), field_width, precision, flags);
625 str = pointer(fmt+1, str,
626 va_arg(args, void *),
627 field_width, precision, flags);
628 /* Skip all alphanumeric pointer suffixes */
629 while (isalnum(fmt[1]))
634 if (qualifier == 'l') {
635 long * ip = va_arg(args, long *);
638 int * ip = va_arg(args, int *);
647 /* integer number formats - set up the flags and "break" */
672 if (qualifier == 'L') /* "quad" for 64 bit variables */
673 num = va_arg(args, unsigned long long);
674 else if (qualifier == 'l') {
675 num = va_arg(args, unsigned long);
677 num = (signed long) num;
678 } else if (qualifier == 'Z' || qualifier == 'z') {
679 num = va_arg(args, size_t);
680 } else if (qualifier == 't') {
681 num = va_arg(args, ptrdiff_t);
682 } else if (qualifier == 'h') {
683 num = (unsigned short) va_arg(args, int);
685 num = (signed short) num;
687 num = va_arg(args, unsigned int);
689 num = (signed int) num;
691 str = number(str, num, base, field_width, precision, flags);
698 * sprintf - Format a string and place it in a buffer
699 * @buf: The buffer to place the result into
700 * @fmt: The format string to use
701 * @...: Arguments for the format string
703 * The function returns the number of characters written
706 * See the vsprintf() documentation for format string extensions over C99.
708 int sprintf(char * buf, const char *fmt, ...)
714 i=vsprintf(buf,fmt,args);
719 void panic(const char *fmt, ...)
726 #if defined (CONFIG_PANIC_HANG)
729 udelay (100000); /* allow messages to go out */
730 do_reset (NULL, 0, 0, NULL);
736 void __assert_fail(const char *assertion, const char *file, unsigned line,
737 const char *function)
739 /* This will not return */
740 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
744 char *simple_itoa(ulong i)
746 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
747 static char local[22];
748 char *p = &local[21];