4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioport.h>
28 #include <net/addrconf.h>
30 #include <asm/page.h> /* for PAGE_SIZE */
31 #include <asm/div64.h>
32 #include <asm/sections.h> /* for dereference_function_descriptor() */
34 /* Works only for digits and letters, but small and fast */
35 #define TOLOWER(x) ((x) | 0x20)
37 static unsigned int simple_guess_base(const char *cp)
40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
50 * simple_strtoul - convert a string to an unsigned long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
55 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
57 unsigned long result = 0;
60 base = simple_guess_base(cp);
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
65 while (isxdigit(*cp)) {
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
71 result = result * base + value;
79 EXPORT_SYMBOL(simple_strtoul);
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
87 long simple_strtol(const char *cp, char **endp, unsigned int base)
90 return -simple_strtoul(cp + 1, endp, base);
91 return simple_strtoul(cp, endp, base);
93 EXPORT_SYMBOL(simple_strtol);
96 * simple_strtoull - convert a string to an unsigned long long
97 * @cp: The start of the string
98 * @endp: A pointer to the end of the parsed string will be placed here
99 * @base: The number base to use
101 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
103 unsigned long long result = 0;
106 base = simple_guess_base(cp);
108 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
111 while (isxdigit(*cp)) {
114 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
117 result = result * base + value;
125 EXPORT_SYMBOL(simple_strtoull);
128 * simple_strtoll - convert a string to a signed long long
129 * @cp: The start of the string
130 * @endp: A pointer to the end of the parsed string will be placed here
131 * @base: The number base to use
133 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
136 return -simple_strtoull(cp + 1, endp, base);
137 return simple_strtoull(cp, endp, base);
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
154 * echo will append a newline to the tail.
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
162 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
173 val = simple_strtoul(cp, &tail, base);
176 if ((*tail == '\0') ||
177 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
184 EXPORT_SYMBOL(strict_strtoul);
187 * strict_strtol - convert a string to a long strictly
188 * @cp: The string to be converted
189 * @base: The number base to use
190 * @res: The converted result value
192 * strict_strtol is similiar to strict_strtoul, but it allows the first
193 * character of a string is '-'.
195 * It returns 0 if conversion is successful and *res is set to the converted
196 * value, otherwise it returns -EINVAL and *res is set to 0.
198 int strict_strtol(const char *cp, unsigned int base, long *res)
202 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
206 ret = strict_strtoul(cp, base, (unsigned long *)res);
211 EXPORT_SYMBOL(strict_strtol);
214 * strict_strtoull - convert a string to an unsigned long long strictly
215 * @cp: The string to be converted
216 * @base: The number base to use
217 * @res: The converted result value
219 * strict_strtoull converts a string to an unsigned long long only if the
220 * string is really an unsigned long long string, any string containing
221 * any invalid char at the tail will be rejected and -EINVAL is returned,
222 * only a newline char at the tail is acceptible because people generally
223 * change a module parameter in the following way:
225 * echo 1024 > /sys/module/e1000/parameters/copybreak
227 * echo will append a newline to the tail of the string.
229 * It returns 0 if conversion is successful and *res is set to the converted
230 * value, otherwise it returns -EINVAL and *res is set to 0.
232 * simple_strtoull just ignores the successive invalid characters and
233 * return the converted value of prefix part of the string.
235 int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
238 unsigned long long val;
246 val = simple_strtoull(cp, &tail, base);
249 if ((*tail == '\0') ||
250 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
257 EXPORT_SYMBOL(strict_strtoull);
260 * strict_strtoll - convert a string to a long long strictly
261 * @cp: The string to be converted
262 * @base: The number base to use
263 * @res: The converted result value
265 * strict_strtoll is similiar to strict_strtoull, but it allows the first
266 * character of a string is '-'.
268 * It returns 0 if conversion is successful and *res is set to the converted
269 * value, otherwise it returns -EINVAL and *res is set to 0.
271 int strict_strtoll(const char *cp, unsigned int base, long long *res)
275 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
279 ret = strict_strtoull(cp, base, (unsigned long long *)res);
284 EXPORT_SYMBOL(strict_strtoll);
286 static int skip_atoi(const char **s)
291 i = i*10 + *((*s)++) - '0';
295 /* Decimal conversion is by far the most typical, and is used
296 * for /proc and /sys data. This directly impacts e.g. top performance
297 * with many processes running. We optimize it for speed
299 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
300 * (with permission from the author, Douglas W. Jones). */
302 /* Formats correctly any integer in [0,99999].
303 * Outputs from one to five digits depending on input.
304 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
305 static char* put_dec_trunc(char *buf, unsigned q)
307 unsigned d3, d2, d1, d0;
312 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
313 q = (d0 * 0xcd) >> 11;
315 *buf++ = d0 + '0'; /* least significant digit */
316 d1 = q + 9*d3 + 5*d2 + d1;
318 q = (d1 * 0xcd) >> 11;
320 *buf++ = d1 + '0'; /* next digit */
323 if ((d2 != 0) || (d3 != 0)) {
326 *buf++ = d2 + '0'; /* next digit */
330 q = (d3 * 0xcd) >> 11;
332 *buf++ = d3 + '0'; /* next digit */
334 *buf++ = q + '0'; /* most sign. digit */
340 /* Same with if's removed. Always emits five digits */
341 static char* put_dec_full(char *buf, unsigned q)
343 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
344 /* but anyway, gcc produces better code with full-sized ints */
345 unsigned d3, d2, d1, d0;
350 /* Possible ways to approx. divide by 10 */
351 /* gcc -O2 replaces multiply with shifts and adds */
352 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
353 // (x * 0x67) >> 10: 1100111
354 // (x * 0x34) >> 9: 110100 - same
355 // (x * 0x1a) >> 8: 11010 - same
356 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
358 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
359 q = (d0 * 0xcd) >> 11;
362 d1 = q + 9*d3 + 5*d2 + d1;
363 q = (d1 * 0xcd) >> 11;
373 q = (d3 * 0xcd) >> 11; /* - shorter code */
374 /* q = (d3 * 0x67) >> 10; - would also work */
380 /* No inlining helps gcc to use registers better */
381 static noinline char* put_dec(char *buf, unsigned long long num)
386 return put_dec_trunc(buf, num);
387 rem = do_div(num, 100000);
388 buf = put_dec_full(buf, rem);
392 #define ZEROPAD 1 /* pad with zero */
393 #define SIGN 2 /* unsigned/signed long */
394 #define PLUS 4 /* show plus */
395 #define SPACE 8 /* space if plus */
396 #define LEFT 16 /* left justified */
397 #define SMALL 32 /* Must be 32 == 0x20 */
398 #define SPECIAL 64 /* 0x */
401 FORMAT_TYPE_NONE, /* Just a string part */
403 FORMAT_TYPE_PRECISION,
407 FORMAT_TYPE_PERCENT_CHAR,
409 FORMAT_TYPE_LONG_LONG,
424 enum format_type type;
425 int flags; /* flags to number() */
426 int field_width; /* width of output field */
428 int precision; /* # of digits/chars */
432 static char *number(char *buf, char *end, unsigned long long num,
433 struct printf_spec spec)
435 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
436 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
441 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
446 locase = (spec.flags & SMALL);
447 if (spec.flags & LEFT)
448 spec.flags &= ~ZEROPAD;
450 if (spec.flags & SIGN) {
451 if ((signed long long) num < 0) {
453 num = - (signed long long) num;
455 } else if (spec.flags & PLUS) {
458 } else if (spec.flags & SPACE) {
469 /* generate full string in tmp[], in reverse order */
473 /* Generic code, for any base:
475 tmp[i++] = (digits[do_div(num,base)] | locase);
478 else if (spec.base != 10) { /* 8 or 16 */
479 int mask = spec.base - 1;
481 if (spec.base == 16) shift = 4;
483 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
486 } else { /* base 10 */
487 i = put_dec(tmp, num) - tmp;
490 /* printing 100 using %2d gives "100", not "00" */
491 if (i > spec.precision)
493 /* leading space padding */
494 spec.field_width -= spec.precision;
495 if (!(spec.flags & (ZEROPAD+LEFT))) {
496 while(--spec.field_width >= 0) {
508 /* "0x" / "0" prefix */
513 if (spec.base == 16) {
515 *buf = ('X' | locase);
519 /* zero or space padding */
520 if (!(spec.flags & LEFT)) {
521 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
522 while (--spec.field_width >= 0) {
528 /* hmm even more zero padding? */
529 while (i <= --spec.precision) {
534 /* actual digits of result */
540 /* trailing space padding */
541 while (--spec.field_width >= 0) {
549 static char *string(char *buf, char *end, char *s, struct printf_spec spec)
553 if ((unsigned long)s < PAGE_SIZE)
556 len = strnlen(s, spec.precision);
558 if (!(spec.flags & LEFT)) {
559 while (len < spec.field_width--) {
565 for (i = 0; i < len; ++i) {
570 while (len < spec.field_width--) {
578 static char *symbol_string(char *buf, char *end, void *ptr,
579 struct printf_spec spec, char ext)
581 unsigned long value = (unsigned long) ptr;
582 #ifdef CONFIG_KALLSYMS
583 char sym[KSYM_SYMBOL_LEN];
584 if (ext != 'f' && ext != 's')
585 sprint_symbol(sym, value);
587 kallsyms_lookup(value, NULL, NULL, NULL, sym);
588 return string(buf, end, sym, spec);
590 spec.field_width = 2*sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
593 return number(buf, end, value, spec);
597 static char *resource_string(char *buf, char *end, struct resource *res,
598 struct printf_spec spec, const char *fmt)
600 #ifndef IO_RSRC_PRINTK_SIZE
601 #define IO_RSRC_PRINTK_SIZE 6
604 #ifndef MEM_RSRC_PRINTK_SIZE
605 #define MEM_RSRC_PRINTK_SIZE 10
607 struct printf_spec hex_spec = {
610 .flags = SPECIAL | SMALL | ZEROPAD,
612 struct printf_spec dec_spec = {
617 struct printf_spec str_spec = {
622 struct printf_spec flag_spec = {
625 .flags = SPECIAL | SMALL,
628 * room for three actual numbers (decimal or hex), plus
629 * "[mem 0x-0x 64bit pref disabled flags 0x]\0"
631 char sym[3*3*sizeof(resource_size_t) + 41];
632 char *p = sym, *pend = sym + sizeof(sym);
633 int size = -1, addr = 0;
635 if (res->flags & IORESOURCE_IO) {
636 size = IO_RSRC_PRINTK_SIZE;
638 } else if (res->flags & IORESOURCE_MEM) {
639 size = MEM_RSRC_PRINTK_SIZE;
644 if (fmt[1] == 't' || fmt[1] == 'f') {
645 if (res->flags & IORESOURCE_IO)
646 p = string(p, pend, "io ", str_spec);
647 else if (res->flags & IORESOURCE_MEM)
648 p = string(p, pend, "mem ", str_spec);
649 else if (res->flags & IORESOURCE_IRQ)
650 p = string(p, pend, "irq ", str_spec);
651 else if (res->flags & IORESOURCE_DMA)
652 p = string(p, pend, "dma ", str_spec);
654 hex_spec.field_width = size;
655 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
656 if (res->start != res->end) {
658 p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
660 if (fmt[1] == 't' || fmt[1] == 'f') {
661 if (res->flags & IORESOURCE_MEM_64)
662 p = string(p, pend, " 64bit", str_spec);
663 if (res->flags & IORESOURCE_PREFETCH)
664 p = string(p, pend, " pref", str_spec);
665 if (res->flags & IORESOURCE_DISABLED)
666 p = string(p, pend, " disabled", str_spec);
668 p = string(p, pend, " flags ", str_spec);
669 p = number(p, pend, res->flags & ~IORESOURCE_TYPE_BITS,
676 return string(buf, end, sym, spec);
679 static char *mac_address_string(char *buf, char *end, u8 *addr,
680 struct printf_spec spec, const char *fmt)
682 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
686 for (i = 0; i < 6; i++) {
687 p = pack_hex_byte(p, addr[i]);
688 if (fmt[0] == 'M' && i != 5)
693 return string(buf, end, mac_addr, spec);
696 static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
700 for (i = 0; i < 4; i++) {
701 char temp[3]; /* hold each IP quad in reverse order */
702 int digits = put_dec_trunc(temp, addr[i]) - temp;
709 /* reverse the digits in the quad */
720 static char *ip6_compressed_string(char *p, const char *addr)
725 unsigned char zerolength[8];
731 bool needcolon = false;
735 memcpy(&in6, addr, sizeof(struct in6_addr));
737 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
739 memset(zerolength, 0, sizeof(zerolength));
746 /* find position of longest 0 run */
747 for (i = 0; i < range; i++) {
748 for (j = i; j < range; j++) {
749 if (in6.s6_addr16[j] != 0)
754 for (i = 0; i < range; i++) {
755 if (zerolength[i] > longest) {
756 longest = zerolength[i];
762 for (i = 0; i < range; i++) {
764 if (needcolon || i == 0)
775 /* hex u16 without leading 0s */
776 word = ntohs(in6.s6_addr16[i]);
781 p = pack_hex_byte(p, hi);
783 *p++ = hex_asc_lo(hi);
786 p = pack_hex_byte(p, lo);
788 *p++ = hex_asc_lo(lo);
795 p = ip4_string(p, &in6.s6_addr[12], false);
802 static char *ip6_string(char *p, const char *addr, const char *fmt)
805 for (i = 0; i < 8; i++) {
806 p = pack_hex_byte(p, *addr++);
807 p = pack_hex_byte(p, *addr++);
808 if (fmt[0] == 'I' && i != 7)
816 static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
817 struct printf_spec spec, const char *fmt)
819 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
821 if (fmt[0] == 'I' && fmt[2] == 'c')
822 ip6_compressed_string(ip6_addr, addr);
824 ip6_string(ip6_addr, addr, fmt);
826 return string(buf, end, ip6_addr, spec);
829 static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
830 struct printf_spec spec, const char *fmt)
832 char ip4_addr[sizeof("255.255.255.255")];
834 ip4_string(ip4_addr, addr, fmt[0] == 'i');
836 return string(buf, end, ip4_addr, spec);
840 * Show a '%p' thing. A kernel extension is that the '%p' is followed
841 * by an extra set of alphanumeric characters that are extended format
844 * Right now we handle:
846 * - 'F' For symbolic function descriptor pointers with offset
847 * - 'f' For simple symbolic function names without offset
848 * - 'S' For symbolic direct pointers with offset
849 * - 's' For symbolic direct pointers without offset
850 * - 'R' For a struct resource pointer, print:
851 * R address range only ([0x0-0x1f])
852 * Rt type and range ([mem 0x0-0x1f 64bit pref])
853 * Rf type, range, and flags ([mem 0x0-0x1f 64bit pref flags 0x1])
854 * - 'M' For a 6-byte MAC address, it prints the address in the
855 * usual colon-separated hex notation
856 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
857 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
858 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
859 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
860 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
861 * IPv6 omits the colons (01020304...0f)
862 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
863 * - 'I6c' for IPv6 addresses printed as specified by
864 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
865 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
866 * function pointers are really function descriptors, which contain a
867 * pointer to the real address.
869 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
870 struct printf_spec spec)
873 return string(buf, end, "(null)", spec);
878 ptr = dereference_function_descriptor(ptr);
882 return symbol_string(buf, end, ptr, spec, *fmt);
884 return resource_string(buf, end, ptr, spec, fmt);
885 case 'M': /* Colon separated: 00:01:02:03:04:05 */
886 case 'm': /* Contiguous: 000102030405 */
887 return mac_address_string(buf, end, ptr, spec, fmt);
888 case 'I': /* Formatted IP supported
890 * 6: 0001:0203:...:0708
891 * 6c: 1::708 or 1::1.2.3.4
893 case 'i': /* Contiguous:
899 return ip6_addr_string(buf, end, ptr, spec, fmt);
901 return ip4_addr_string(buf, end, ptr, spec, fmt);
906 if (spec.field_width == -1) {
907 spec.field_width = 2*sizeof(void *);
908 spec.flags |= ZEROPAD;
912 return number(buf, end, (unsigned long) ptr, spec);
916 * Helper function to decode printf style format.
917 * Each call decode a token from the format and return the
918 * number of characters read (or likely the delta where it wants
919 * to go on the next call).
920 * The decoded token is returned through the parameters
922 * 'h', 'l', or 'L' for integer fields
923 * 'z' support added 23/7/1999 S.H.
924 * 'z' changed to 'Z' --davidm 1/25/99
925 * 't' added for ptrdiff_t
927 * @fmt: the format string
928 * @type of the token returned
929 * @flags: various flags such as +, -, # tokens..
930 * @field_width: overwritten width
931 * @base: base of the number (octal, hex, ...)
932 * @precision: precision of a number
933 * @qualifier: qualifier of a number (long, size_t, ...)
935 static int format_decode(const char *fmt, struct printf_spec *spec)
937 const char *start = fmt;
939 /* we finished early by reading the field width */
940 if (spec->type == FORMAT_TYPE_WIDTH) {
941 if (spec->field_width < 0) {
942 spec->field_width = -spec->field_width;
945 spec->type = FORMAT_TYPE_NONE;
949 /* we finished early by reading the precision */
950 if (spec->type == FORMAT_TYPE_PRECISION) {
951 if (spec->precision < 0)
954 spec->type = FORMAT_TYPE_NONE;
959 spec->type = FORMAT_TYPE_NONE;
961 for (; *fmt ; ++fmt) {
966 /* Return the current non-format string */
967 if (fmt != start || !*fmt)
973 while (1) { /* this also skips first '%' */
979 case '-': spec->flags |= LEFT; break;
980 case '+': spec->flags |= PLUS; break;
981 case ' ': spec->flags |= SPACE; break;
982 case '#': spec->flags |= SPECIAL; break;
983 case '0': spec->flags |= ZEROPAD; break;
984 default: found = false;
991 /* get field width */
992 spec->field_width = -1;
995 spec->field_width = skip_atoi(&fmt);
996 else if (*fmt == '*') {
997 /* it's the next argument */
998 spec->type = FORMAT_TYPE_WIDTH;
999 return ++fmt - start;
1003 /* get the precision */
1004 spec->precision = -1;
1007 if (isdigit(*fmt)) {
1008 spec->precision = skip_atoi(&fmt);
1009 if (spec->precision < 0)
1010 spec->precision = 0;
1011 } else if (*fmt == '*') {
1012 /* it's the next argument */
1013 spec->type = FORMAT_TYPE_PRECISION;
1014 return ++fmt - start;
1019 /* get the conversion qualifier */
1020 spec->qualifier = -1;
1021 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1022 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1023 spec->qualifier = *fmt++;
1024 if (unlikely(spec->qualifier == *fmt)) {
1025 if (spec->qualifier == 'l') {
1026 spec->qualifier = 'L';
1028 } else if (spec->qualifier == 'h') {
1029 spec->qualifier = 'H';
1039 spec->type = FORMAT_TYPE_CHAR;
1040 return ++fmt - start;
1043 spec->type = FORMAT_TYPE_STR;
1044 return ++fmt - start;
1047 spec->type = FORMAT_TYPE_PTR;
1052 spec->type = FORMAT_TYPE_NRCHARS;
1053 return ++fmt - start;
1056 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1057 return ++fmt - start;
1059 /* integer number formats - set up the flags and "break" */
1065 spec->flags |= SMALL;
1073 spec->flags |= SIGN;
1078 spec->type = FORMAT_TYPE_INVALID;
1082 if (spec->qualifier == 'L')
1083 spec->type = FORMAT_TYPE_LONG_LONG;
1084 else if (spec->qualifier == 'l') {
1085 if (spec->flags & SIGN)
1086 spec->type = FORMAT_TYPE_LONG;
1088 spec->type = FORMAT_TYPE_ULONG;
1089 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1090 spec->type = FORMAT_TYPE_SIZE_T;
1091 } else if (spec->qualifier == 't') {
1092 spec->type = FORMAT_TYPE_PTRDIFF;
1093 } else if (spec->qualifier == 'H') {
1094 if (spec->flags & SIGN)
1095 spec->type = FORMAT_TYPE_BYTE;
1097 spec->type = FORMAT_TYPE_UBYTE;
1098 } else if (spec->qualifier == 'h') {
1099 if (spec->flags & SIGN)
1100 spec->type = FORMAT_TYPE_SHORT;
1102 spec->type = FORMAT_TYPE_USHORT;
1104 if (spec->flags & SIGN)
1105 spec->type = FORMAT_TYPE_INT;
1107 spec->type = FORMAT_TYPE_UINT;
1110 return ++fmt - start;
1114 * vsnprintf - Format a string and place it in a buffer
1115 * @buf: The buffer to place the result into
1116 * @size: The size of the buffer, including the trailing null space
1117 * @fmt: The format string to use
1118 * @args: Arguments for the format string
1120 * This function follows C99 vsnprintf, but has some extensions:
1121 * %pS output the name of a text symbol with offset
1122 * %ps output the name of a text symbol without offset
1123 * %pF output the name of a function pointer with its offset
1124 * %pf output the name of a function pointer without its offset
1125 * %pR output the address range in a struct resource
1128 * The return value is the number of characters which would
1129 * be generated for the given input, excluding the trailing
1130 * '\0', as per ISO C99. If you want to have the exact
1131 * number of characters written into @buf as return value
1132 * (not including the trailing '\0'), use vscnprintf(). If the
1133 * return is greater than or equal to @size, the resulting
1134 * string is truncated.
1136 * Call this function if you are already dealing with a va_list.
1137 * You probably want snprintf() instead.
1139 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1141 unsigned long long num;
1144 struct printf_spec spec = {0};
1146 /* Reject out-of-range values early. Large positive sizes are
1147 used for unknown buffer sizes. */
1148 if (WARN_ON_ONCE((int) size < 0))
1154 /* Make sure end is always >= buf */
1161 const char *old_fmt = fmt;
1163 read = format_decode(fmt, &spec);
1167 switch (spec.type) {
1168 case FORMAT_TYPE_NONE: {
1171 if (copy > end - str)
1173 memcpy(str, old_fmt, copy);
1179 case FORMAT_TYPE_WIDTH:
1180 spec.field_width = va_arg(args, int);
1183 case FORMAT_TYPE_PRECISION:
1184 spec.precision = va_arg(args, int);
1187 case FORMAT_TYPE_CHAR:
1188 if (!(spec.flags & LEFT)) {
1189 while (--spec.field_width > 0) {
1196 c = (unsigned char) va_arg(args, int);
1200 while (--spec.field_width > 0) {
1207 case FORMAT_TYPE_STR:
1208 str = string(str, end, va_arg(args, char *), spec);
1211 case FORMAT_TYPE_PTR:
1212 str = pointer(fmt+1, str, end, va_arg(args, void *),
1214 while (isalnum(*fmt))
1218 case FORMAT_TYPE_PERCENT_CHAR:
1224 case FORMAT_TYPE_INVALID:
1230 case FORMAT_TYPE_NRCHARS: {
1231 int qualifier = spec.qualifier;
1233 if (qualifier == 'l') {
1234 long *ip = va_arg(args, long *);
1236 } else if (qualifier == 'Z' ||
1238 size_t *ip = va_arg(args, size_t *);
1241 int *ip = va_arg(args, int *);
1248 switch (spec.type) {
1249 case FORMAT_TYPE_LONG_LONG:
1250 num = va_arg(args, long long);
1252 case FORMAT_TYPE_ULONG:
1253 num = va_arg(args, unsigned long);
1255 case FORMAT_TYPE_LONG:
1256 num = va_arg(args, long);
1258 case FORMAT_TYPE_SIZE_T:
1259 num = va_arg(args, size_t);
1261 case FORMAT_TYPE_PTRDIFF:
1262 num = va_arg(args, ptrdiff_t);
1264 case FORMAT_TYPE_UBYTE:
1265 num = (unsigned char) va_arg(args, int);
1267 case FORMAT_TYPE_BYTE:
1268 num = (signed char) va_arg(args, int);
1270 case FORMAT_TYPE_USHORT:
1271 num = (unsigned short) va_arg(args, int);
1273 case FORMAT_TYPE_SHORT:
1274 num = (short) va_arg(args, int);
1276 case FORMAT_TYPE_INT:
1277 num = (int) va_arg(args, int);
1280 num = va_arg(args, unsigned int);
1283 str = number(str, end, num, spec);
1294 /* the trailing null byte doesn't count towards the total */
1298 EXPORT_SYMBOL(vsnprintf);
1301 * vscnprintf - Format a string and place it in a buffer
1302 * @buf: The buffer to place the result into
1303 * @size: The size of the buffer, including the trailing null space
1304 * @fmt: The format string to use
1305 * @args: Arguments for the format string
1307 * The return value is the number of characters which have been written into
1308 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1311 * Call this function if you are already dealing with a va_list.
1312 * You probably want scnprintf() instead.
1314 * See the vsnprintf() documentation for format string extensions over C99.
1316 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1320 i=vsnprintf(buf,size,fmt,args);
1321 return (i >= size) ? (size - 1) : i;
1323 EXPORT_SYMBOL(vscnprintf);
1326 * snprintf - Format a string and place it in a buffer
1327 * @buf: The buffer to place the result into
1328 * @size: The size of the buffer, including the trailing null space
1329 * @fmt: The format string to use
1330 * @...: Arguments for the format string
1332 * The return value is the number of characters which would be
1333 * generated for the given input, excluding the trailing null,
1334 * as per ISO C99. If the return is greater than or equal to
1335 * @size, the resulting string is truncated.
1337 * See the vsnprintf() documentation for format string extensions over C99.
1339 int snprintf(char * buf, size_t size, const char *fmt, ...)
1344 va_start(args, fmt);
1345 i=vsnprintf(buf,size,fmt,args);
1349 EXPORT_SYMBOL(snprintf);
1352 * scnprintf - Format a string and place it in a buffer
1353 * @buf: The buffer to place the result into
1354 * @size: The size of the buffer, including the trailing null space
1355 * @fmt: The format string to use
1356 * @...: Arguments for the format string
1358 * The return value is the number of characters written into @buf not including
1359 * the trailing '\0'. If @size is <= 0 the function returns 0.
1362 int scnprintf(char * buf, size_t size, const char *fmt, ...)
1367 va_start(args, fmt);
1368 i = vsnprintf(buf, size, fmt, args);
1370 return (i >= size) ? (size - 1) : i;
1372 EXPORT_SYMBOL(scnprintf);
1375 * vsprintf - Format a string and place it in a buffer
1376 * @buf: The buffer to place the result into
1377 * @fmt: The format string to use
1378 * @args: Arguments for the format string
1380 * The function returns the number of characters written
1381 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1384 * Call this function if you are already dealing with a va_list.
1385 * You probably want sprintf() instead.
1387 * See the vsnprintf() documentation for format string extensions over C99.
1389 int vsprintf(char *buf, const char *fmt, va_list args)
1391 return vsnprintf(buf, INT_MAX, fmt, args);
1393 EXPORT_SYMBOL(vsprintf);
1396 * sprintf - Format a string and place it in a buffer
1397 * @buf: The buffer to place the result into
1398 * @fmt: The format string to use
1399 * @...: Arguments for the format string
1401 * The function returns the number of characters written
1402 * into @buf. Use snprintf() or scnprintf() in order to avoid
1405 * See the vsnprintf() documentation for format string extensions over C99.
1407 int sprintf(char * buf, const char *fmt, ...)
1412 va_start(args, fmt);
1413 i=vsnprintf(buf, INT_MAX, fmt, args);
1417 EXPORT_SYMBOL(sprintf);
1419 #ifdef CONFIG_BINARY_PRINTF
1422 * vbin_printf() - VA arguments to binary data
1423 * bstr_printf() - Binary data to text string
1427 * vbin_printf - Parse a format string and place args' binary value in a buffer
1428 * @bin_buf: The buffer to place args' binary value
1429 * @size: The size of the buffer(by words(32bits), not characters)
1430 * @fmt: The format string to use
1431 * @args: Arguments for the format string
1433 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1436 * The return value is the number of words(32bits) which would be generated for
1440 * If the return value is greater than @size, the resulting bin_buf is NOT
1441 * valid for bstr_printf().
1443 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1445 struct printf_spec spec = {0};
1449 str = (char *)bin_buf;
1450 end = (char *)(bin_buf + size);
1452 #define save_arg(type) \
1454 if (sizeof(type) == 8) { \
1455 unsigned long long value; \
1456 str = PTR_ALIGN(str, sizeof(u32)); \
1457 value = va_arg(args, unsigned long long); \
1458 if (str + sizeof(type) <= end) { \
1459 *(u32 *)str = *(u32 *)&value; \
1460 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1463 unsigned long value; \
1464 str = PTR_ALIGN(str, sizeof(type)); \
1465 value = va_arg(args, int); \
1466 if (str + sizeof(type) <= end) \
1467 *(typeof(type) *)str = (type)value; \
1469 str += sizeof(type); \
1474 read = format_decode(fmt, &spec);
1478 switch (spec.type) {
1479 case FORMAT_TYPE_NONE:
1482 case FORMAT_TYPE_WIDTH:
1483 case FORMAT_TYPE_PRECISION:
1487 case FORMAT_TYPE_CHAR:
1491 case FORMAT_TYPE_STR: {
1492 const char *save_str = va_arg(args, char *);
1494 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1495 || (unsigned long)save_str < PAGE_SIZE)
1496 save_str = "<NULL>";
1497 len = strlen(save_str);
1498 if (str + len + 1 < end)
1499 memcpy(str, save_str, len + 1);
1504 case FORMAT_TYPE_PTR:
1506 /* skip all alphanumeric pointer suffixes */
1507 while (isalnum(*fmt))
1511 case FORMAT_TYPE_PERCENT_CHAR:
1514 case FORMAT_TYPE_INVALID:
1517 case FORMAT_TYPE_NRCHARS: {
1518 /* skip %n 's argument */
1519 int qualifier = spec.qualifier;
1521 if (qualifier == 'l')
1522 skip_arg = va_arg(args, long *);
1523 else if (qualifier == 'Z' || qualifier == 'z')
1524 skip_arg = va_arg(args, size_t *);
1526 skip_arg = va_arg(args, int *);
1531 switch (spec.type) {
1533 case FORMAT_TYPE_LONG_LONG:
1534 save_arg(long long);
1536 case FORMAT_TYPE_ULONG:
1537 case FORMAT_TYPE_LONG:
1538 save_arg(unsigned long);
1540 case FORMAT_TYPE_SIZE_T:
1543 case FORMAT_TYPE_PTRDIFF:
1544 save_arg(ptrdiff_t);
1546 case FORMAT_TYPE_UBYTE:
1547 case FORMAT_TYPE_BYTE:
1550 case FORMAT_TYPE_USHORT:
1551 case FORMAT_TYPE_SHORT:
1559 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1563 EXPORT_SYMBOL_GPL(vbin_printf);
1566 * bstr_printf - Format a string from binary arguments and place it in a buffer
1567 * @buf: The buffer to place the result into
1568 * @size: The size of the buffer, including the trailing null space
1569 * @fmt: The format string to use
1570 * @bin_buf: Binary arguments for the format string
1572 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1573 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1574 * a binary buffer that generated by vbin_printf.
1576 * The format follows C99 vsnprintf, but has some extensions:
1577 * see vsnprintf comment for details.
1579 * The return value is the number of characters which would
1580 * be generated for the given input, excluding the trailing
1581 * '\0', as per ISO C99. If you want to have the exact
1582 * number of characters written into @buf as return value
1583 * (not including the trailing '\0'), use vscnprintf(). If the
1584 * return is greater than or equal to @size, the resulting
1585 * string is truncated.
1587 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1589 unsigned long long num;
1591 const char *args = (const char *)bin_buf;
1593 struct printf_spec spec = {0};
1595 if (WARN_ON_ONCE((int) size < 0))
1601 #define get_arg(type) \
1603 typeof(type) value; \
1604 if (sizeof(type) == 8) { \
1605 args = PTR_ALIGN(args, sizeof(u32)); \
1606 *(u32 *)&value = *(u32 *)args; \
1607 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1609 args = PTR_ALIGN(args, sizeof(type)); \
1610 value = *(typeof(type) *)args; \
1612 args += sizeof(type); \
1616 /* Make sure end is always >= buf */
1624 const char *old_fmt = fmt;
1626 read = format_decode(fmt, &spec);
1630 switch (spec.type) {
1631 case FORMAT_TYPE_NONE: {
1634 if (copy > end - str)
1636 memcpy(str, old_fmt, copy);
1642 case FORMAT_TYPE_WIDTH:
1643 spec.field_width = get_arg(int);
1646 case FORMAT_TYPE_PRECISION:
1647 spec.precision = get_arg(int);
1650 case FORMAT_TYPE_CHAR:
1651 if (!(spec.flags & LEFT)) {
1652 while (--spec.field_width > 0) {
1658 c = (unsigned char) get_arg(char);
1662 while (--spec.field_width > 0) {
1669 case FORMAT_TYPE_STR: {
1670 const char *str_arg = args;
1671 size_t len = strlen(str_arg);
1673 str = string(str, end, (char *)str_arg, spec);
1677 case FORMAT_TYPE_PTR:
1678 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1679 while (isalnum(*fmt))
1683 case FORMAT_TYPE_PERCENT_CHAR:
1689 case FORMAT_TYPE_INVALID:
1695 case FORMAT_TYPE_NRCHARS:
1700 switch (spec.type) {
1702 case FORMAT_TYPE_LONG_LONG:
1703 num = get_arg(long long);
1705 case FORMAT_TYPE_ULONG:
1706 num = get_arg(unsigned long);
1708 case FORMAT_TYPE_LONG:
1709 num = get_arg(unsigned long);
1711 case FORMAT_TYPE_SIZE_T:
1712 num = get_arg(size_t);
1714 case FORMAT_TYPE_PTRDIFF:
1715 num = get_arg(ptrdiff_t);
1717 case FORMAT_TYPE_UBYTE:
1718 num = get_arg(unsigned char);
1720 case FORMAT_TYPE_BYTE:
1721 num = get_arg(signed char);
1723 case FORMAT_TYPE_USHORT:
1724 num = get_arg(unsigned short);
1726 case FORMAT_TYPE_SHORT:
1727 num = get_arg(short);
1729 case FORMAT_TYPE_UINT:
1730 num = get_arg(unsigned int);
1736 str = number(str, end, num, spec);
1749 /* the trailing null byte doesn't count towards the total */
1752 EXPORT_SYMBOL_GPL(bstr_printf);
1755 * bprintf - Parse a format string and place args' binary value in a buffer
1756 * @bin_buf: The buffer to place args' binary value
1757 * @size: The size of the buffer(by words(32bits), not characters)
1758 * @fmt: The format string to use
1759 * @...: Arguments for the format string
1761 * The function returns the number of words(u32) written
1764 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1769 va_start(args, fmt);
1770 ret = vbin_printf(bin_buf, size, fmt, args);
1774 EXPORT_SYMBOL_GPL(bprintf);
1776 #endif /* CONFIG_BINARY_PRINTF */
1779 * vsscanf - Unformat a buffer into a list of arguments
1780 * @buf: input buffer
1781 * @fmt: format of buffer
1784 int vsscanf(const char * buf, const char * fmt, va_list args)
1786 const char *str = buf;
1795 while(*fmt && *str) {
1796 /* skip any white space in format */
1797 /* white space in format matchs any amount of
1798 * white space, including none, in the input.
1800 if (isspace(*fmt)) {
1801 while (isspace(*fmt))
1803 while (isspace(*str))
1807 /* anything that is not a conversion must match exactly */
1808 if (*fmt != '%' && *fmt) {
1809 if (*fmt++ != *str++)
1818 /* skip this conversion.
1819 * advance both strings to next white space
1822 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1824 while (!isspace(*str) && *str)
1829 /* get field width */
1832 field_width = skip_atoi(&fmt);
1834 /* get conversion qualifier */
1836 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1837 *fmt == 'Z' || *fmt == 'z') {
1839 if (unlikely(qualifier == *fmt)) {
1840 if (qualifier == 'h') {
1843 } else if (qualifier == 'l') {
1858 char *s = (char *) va_arg(args,char*);
1859 if (field_width == -1)
1863 } while (--field_width > 0 && *str);
1869 char *s = (char *) va_arg(args, char *);
1870 if(field_width == -1)
1871 field_width = INT_MAX;
1872 /* first, skip leading white space in buffer */
1873 while (isspace(*str))
1876 /* now copy until next white space */
1877 while (*str && !isspace(*str) && field_width--) {
1885 /* return number of characters read so far */
1887 int *i = (int *)va_arg(args,int*);
1905 /* looking for '%' in str */
1910 /* invalid format; stop here */
1914 /* have some sort of integer conversion.
1915 * first, skip white space in buffer.
1917 while (isspace(*str))
1921 if (is_sign && digit == '-')
1925 || (base == 16 && !isxdigit(digit))
1926 || (base == 10 && !isdigit(digit))
1927 || (base == 8 && (!isdigit(digit) || digit > '7'))
1928 || (base == 0 && !isdigit(digit)))
1932 case 'H': /* that's 'hh' in format */
1934 signed char *s = (signed char *) va_arg(args,signed char *);
1935 *s = (signed char) simple_strtol(str,&next,base);
1937 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1938 *s = (unsigned char) simple_strtoul(str, &next, base);
1943 short *s = (short *) va_arg(args,short *);
1944 *s = (short) simple_strtol(str,&next,base);
1946 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1947 *s = (unsigned short) simple_strtoul(str, &next, base);
1952 long *l = (long *) va_arg(args,long *);
1953 *l = simple_strtol(str,&next,base);
1955 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1956 *l = simple_strtoul(str,&next,base);
1961 long long *l = (long long*) va_arg(args,long long *);
1962 *l = simple_strtoll(str,&next,base);
1964 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1965 *l = simple_strtoull(str,&next,base);
1971 size_t *s = (size_t*) va_arg(args,size_t*);
1972 *s = (size_t) simple_strtoul(str,&next,base);
1977 int *i = (int *) va_arg(args, int*);
1978 *i = (int) simple_strtol(str,&next,base);
1980 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1981 *i = (unsigned int) simple_strtoul(str,&next,base);
1993 * Now we've come all the way through so either the input string or the
1994 * format ended. In the former case, there can be a %n at the current
1995 * position in the format that needs to be filled.
1997 if (*fmt == '%' && *(fmt + 1) == 'n') {
1998 int *p = (int *)va_arg(args, int *);
2004 EXPORT_SYMBOL(vsscanf);
2007 * sscanf - Unformat a buffer into a list of arguments
2008 * @buf: input buffer
2009 * @fmt: formatting of buffer
2010 * @...: resulting arguments
2012 int sscanf(const char * buf, const char * fmt, ...)
2018 i = vsscanf(buf,fmt,args);
2022 EXPORT_SYMBOL(sscanf);