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> /* for KSYM_SYMBOL_LEN */
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() */
37 * simple_strtoull - convert a string to an unsigned long long
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
42 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
44 unsigned long long result;
47 cp = _parse_integer_fixup_radix(cp, &base);
48 rv = _parse_integer(cp, base, &result);
50 cp += (rv & ~KSTRTOX_OVERFLOW);
57 EXPORT_SYMBOL(simple_strtoull);
60 * simple_strtoul - convert a string to an unsigned long
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
65 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
67 return simple_strtoull(cp, endp, base);
69 EXPORT_SYMBOL(simple_strtoul);
72 * simple_strtol - convert a string to a signed long
73 * @cp: The start of the string
74 * @endp: A pointer to the end of the parsed string will be placed here
75 * @base: The number base to use
77 long simple_strtol(const char *cp, char **endp, unsigned int base)
80 return -simple_strtoul(cp + 1, endp, base);
82 return simple_strtoul(cp, endp, base);
84 EXPORT_SYMBOL(simple_strtol);
87 * simple_strtoll - convert a string to a signed long long
88 * @cp: The start of the string
89 * @endp: A pointer to the end of the parsed string will be placed here
90 * @base: The number base to use
92 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
95 return -simple_strtoull(cp + 1, endp, base);
97 return simple_strtoull(cp, endp, base);
99 EXPORT_SYMBOL(simple_strtoll);
101 static noinline_for_stack
102 int skip_atoi(const char **s)
107 i = i*10 + *((*s)++) - '0';
112 /* Decimal conversion is by far the most typical, and is used
113 * for /proc and /sys data. This directly impacts e.g. top performance
114 * with many processes running. We optimize it for speed
115 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
116 * (with permission from the author, Douglas W. Jones).
119 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
120 /* Formats correctly any integer in [0, 999999999] */
121 static noinline_for_stack
122 char *put_dec_full9(char *buf, unsigned q)
126 /* Possible ways to approx. divide by 10
127 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
128 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
129 * (x * 0x6667) >> 18 x < 43699
130 * (x * 0x3334) >> 17 x < 16389
131 * (x * 0x199a) >> 16 x < 16389
132 * (x * 0x0ccd) >> 15 x < 16389
133 * (x * 0x0667) >> 14 x < 2739
134 * (x * 0x0334) >> 13 x < 1029
135 * (x * 0x019a) >> 12 x < 1029
136 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
137 * (x * 0x0067) >> 10 x < 179
138 * (x * 0x0034) >> 9 x < 69 same
139 * (x * 0x001a) >> 8 x < 69 same
140 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
141 * (x * 0x0007) >> 6 x < 19
142 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
144 r = (q * (uint64_t)0x1999999a) >> 32;
145 *buf++ = (q - 10 * r) + '0'; /* 1 */
146 q = (r * (uint64_t)0x1999999a) >> 32;
147 *buf++ = (r - 10 * q) + '0'; /* 2 */
148 r = (q * (uint64_t)0x1999999a) >> 32;
149 *buf++ = (q - 10 * r) + '0'; /* 3 */
150 q = (r * (uint64_t)0x1999999a) >> 32;
151 *buf++ = (r - 10 * q) + '0'; /* 4 */
152 r = (q * (uint64_t)0x1999999a) >> 32;
153 *buf++ = (q - 10 * r) + '0'; /* 5 */
154 /* Now value is under 10000, can avoid 64-bit multiply */
155 q = (r * 0x199a) >> 16;
156 *buf++ = (r - 10 * q) + '0'; /* 6 */
157 r = (q * 0xcd) >> 11;
158 *buf++ = (q - 10 * r) + '0'; /* 7 */
159 q = (r * 0xcd) >> 11;
160 *buf++ = (r - 10 * q) + '0'; /* 8 */
161 *buf++ = q + '0'; /* 9 */
166 /* Similar to above but do not pad with zeros.
167 * Code can be easily arranged to print 9 digits too, but our callers
168 * always call put_dec_full9() instead when the number has 9 decimal digits.
170 static noinline_for_stack
171 char *put_dec_trunc8(char *buf, unsigned r)
175 /* Copy of previous function's body with added early returns */
176 q = (r * (uint64_t)0x1999999a) >> 32;
177 *buf++ = (r - 10 * q) + '0'; /* 2 */
178 if (q == 0) return buf;
179 r = (q * (uint64_t)0x1999999a) >> 32;
180 *buf++ = (q - 10 * r) + '0'; /* 3 */
181 if (r == 0) return buf;
182 q = (r * (uint64_t)0x1999999a) >> 32;
183 *buf++ = (r - 10 * q) + '0'; /* 4 */
184 if (q == 0) return buf;
185 r = (q * (uint64_t)0x1999999a) >> 32;
186 *buf++ = (q - 10 * r) + '0'; /* 5 */
187 if (r == 0) return buf;
188 q = (r * 0x199a) >> 16;
189 *buf++ = (r - 10 * q) + '0'; /* 6 */
190 if (q == 0) return buf;
191 r = (q * 0xcd) >> 11;
192 *buf++ = (q - 10 * r) + '0'; /* 7 */
193 if (r == 0) return buf;
194 q = (r * 0xcd) >> 11;
195 *buf++ = (r - 10 * q) + '0'; /* 8 */
196 if (q == 0) return buf;
197 *buf++ = q + '0'; /* 9 */
201 /* There are two algorithms to print larger numbers.
202 * One is generic: divide by 1000000000 and repeatedly print
203 * groups of (up to) 9 digits. It's conceptually simple,
204 * but requires a (unsigned long long) / 1000000000 division.
206 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
207 * manipulates them cleverly and generates groups of 4 decimal digits.
208 * It so happens that it does NOT require long long division.
210 * If long is > 32 bits, division of 64-bit values is relatively easy,
211 * and we will use the first algorithm.
212 * If long long is > 64 bits (strange architecture with VERY large long long),
213 * second algorithm can't be used, and we again use the first one.
215 * Else (if long is 32 bits and long long is 64 bits) we use second one.
218 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
220 /* First algorithm: generic */
223 char *put_dec(char *buf, unsigned long long n)
225 if (n >= 100*1000*1000) {
226 while (n >= 1000*1000*1000)
227 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
228 if (n >= 100*1000*1000)
229 return put_dec_full9(buf, n);
231 return put_dec_trunc8(buf, n);
236 /* Second algorithm: valid only for 64-bit long longs */
238 static noinline_for_stack
239 char *put_dec_full4(char *buf, unsigned q)
242 r = (q * 0xcccd) >> 19;
243 *buf++ = (q - 10 * r) + '0';
244 q = (r * 0x199a) >> 16;
245 *buf++ = (r - 10 * q) + '0';
246 r = (q * 0xcd) >> 11;
247 *buf++ = (q - 10 * r) + '0';
252 /* Based on code by Douglas W. Jones found at
253 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
254 * (with permission from the author).
255 * Performs no 64-bit division and hence should be fast on 32-bit machines.
258 char *put_dec(char *buf, unsigned long long n)
260 uint32_t d3, d2, d1, q, h;
262 if (n < 100*1000*1000)
263 return put_dec_trunc8(buf, n);
265 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
268 d3 = (h >> 16); /* implicit "& 0xffff" */
270 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
272 buf = put_dec_full4(buf, q % 10000);
275 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
276 buf = put_dec_full4(buf, d1 % 10000);
279 d2 = q + 4749 * d3 + 42 * d2;
280 buf = put_dec_full4(buf, d2 % 10000);
286 buf = put_dec_full4(buf, d3 % 10000);
290 buf = put_dec_full4(buf, q);
292 while (buf[-1] == '0')
301 * Convert passed number to decimal string.
302 * Returns the length of string. On buffer overflow, returns 0.
304 * If speed is not important, use snprintf(). It's easy to read the code.
306 int num_to_str(char *buf, int size, unsigned long long num)
308 char tmp[sizeof(num) * 3];
311 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
316 len = put_dec(tmp, num) - tmp;
321 for (idx = 0; idx < len; ++idx)
322 buf[idx] = tmp[len - idx - 1];
326 #define ZEROPAD 1 /* pad with zero */
327 #define SIGN 2 /* unsigned/signed long */
328 #define PLUS 4 /* show plus */
329 #define SPACE 8 /* space if plus */
330 #define LEFT 16 /* left justified */
331 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
332 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
335 FORMAT_TYPE_NONE, /* Just a string part */
337 FORMAT_TYPE_PRECISION,
341 FORMAT_TYPE_PERCENT_CHAR,
343 FORMAT_TYPE_LONG_LONG,
358 u8 type; /* format_type enum */
359 u8 flags; /* flags to number() */
360 u8 base; /* number base, 8, 10 or 16 only */
361 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
362 s16 field_width; /* width of output field */
363 s16 precision; /* # of digits/chars */
366 static noinline_for_stack
367 char *number(char *buf, char *end, unsigned long long num,
368 struct printf_spec spec)
370 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
371 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
376 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
379 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
380 * produces same digits or (maybe lowercased) letters */
381 locase = (spec.flags & SMALL);
382 if (spec.flags & LEFT)
383 spec.flags &= ~ZEROPAD;
385 if (spec.flags & SIGN) {
386 if ((signed long long)num < 0) {
388 num = -(signed long long)num;
390 } else if (spec.flags & PLUS) {
393 } else if (spec.flags & SPACE) {
404 /* generate full string in tmp[], in reverse order */
407 tmp[i++] = digits[num] | locase;
408 /* Generic code, for any base:
410 tmp[i++] = (digits[do_div(num,base)] | locase);
413 else if (spec.base != 10) { /* 8 or 16 */
414 int mask = spec.base - 1;
420 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
423 } else { /* base 10 */
424 i = put_dec(tmp, num) - tmp;
427 /* printing 100 using %2d gives "100", not "00" */
428 if (i > spec.precision)
430 /* leading space padding */
431 spec.field_width -= spec.precision;
432 if (!(spec.flags & (ZEROPAD+LEFT))) {
433 while (--spec.field_width >= 0) {
445 /* "0x" / "0" prefix */
450 if (spec.base == 16) {
452 *buf = ('X' | locase);
456 /* zero or space padding */
457 if (!(spec.flags & LEFT)) {
458 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
459 while (--spec.field_width >= 0) {
465 /* hmm even more zero padding? */
466 while (i <= --spec.precision) {
471 /* actual digits of result */
477 /* trailing space padding */
478 while (--spec.field_width >= 0) {
487 static noinline_for_stack
488 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
492 if ((unsigned long)s < PAGE_SIZE)
495 len = strnlen(s, spec.precision);
497 if (!(spec.flags & LEFT)) {
498 while (len < spec.field_width--) {
504 for (i = 0; i < len; ++i) {
509 while (len < spec.field_width--) {
518 static noinline_for_stack
519 char *symbol_string(char *buf, char *end, void *ptr,
520 struct printf_spec spec, char ext)
522 unsigned long value = (unsigned long) ptr;
523 #ifdef CONFIG_KALLSYMS
524 char sym[KSYM_SYMBOL_LEN];
526 sprint_backtrace(sym, value);
527 else if (ext != 'f' && ext != 's')
528 sprint_symbol(sym, value);
530 kallsyms_lookup(value, NULL, NULL, NULL, sym);
532 return string(buf, end, sym, spec);
534 spec.field_width = 2 * sizeof(void *);
535 spec.flags |= SPECIAL | SMALL | ZEROPAD;
538 return number(buf, end, value, spec);
542 static noinline_for_stack
543 char *resource_string(char *buf, char *end, struct resource *res,
544 struct printf_spec spec, const char *fmt)
546 #ifndef IO_RSRC_PRINTK_SIZE
547 #define IO_RSRC_PRINTK_SIZE 6
550 #ifndef MEM_RSRC_PRINTK_SIZE
551 #define MEM_RSRC_PRINTK_SIZE 10
553 static const struct printf_spec io_spec = {
555 .field_width = IO_RSRC_PRINTK_SIZE,
557 .flags = SPECIAL | SMALL | ZEROPAD,
559 static const struct printf_spec mem_spec = {
561 .field_width = MEM_RSRC_PRINTK_SIZE,
563 .flags = SPECIAL | SMALL | ZEROPAD,
565 static const struct printf_spec bus_spec = {
569 .flags = SMALL | ZEROPAD,
571 static const struct printf_spec dec_spec = {
576 static const struct printf_spec str_spec = {
581 static const struct printf_spec flag_spec = {
584 .flags = SPECIAL | SMALL,
587 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
588 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
589 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
590 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
591 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
592 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
593 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
594 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
596 char *p = sym, *pend = sym + sizeof(sym);
597 int decode = (fmt[0] == 'R') ? 1 : 0;
598 const struct printf_spec *specp;
601 if (res->flags & IORESOURCE_IO) {
602 p = string(p, pend, "io ", str_spec);
604 } else if (res->flags & IORESOURCE_MEM) {
605 p = string(p, pend, "mem ", str_spec);
607 } else if (res->flags & IORESOURCE_IRQ) {
608 p = string(p, pend, "irq ", str_spec);
610 } else if (res->flags & IORESOURCE_DMA) {
611 p = string(p, pend, "dma ", str_spec);
613 } else if (res->flags & IORESOURCE_BUS) {
614 p = string(p, pend, "bus ", str_spec);
617 p = string(p, pend, "??? ", str_spec);
621 p = number(p, pend, res->start, *specp);
622 if (res->start != res->end) {
624 p = number(p, pend, res->end, *specp);
627 if (res->flags & IORESOURCE_MEM_64)
628 p = string(p, pend, " 64bit", str_spec);
629 if (res->flags & IORESOURCE_PREFETCH)
630 p = string(p, pend, " pref", str_spec);
631 if (res->flags & IORESOURCE_WINDOW)
632 p = string(p, pend, " window", str_spec);
633 if (res->flags & IORESOURCE_DISABLED)
634 p = string(p, pend, " disabled", str_spec);
636 p = string(p, pend, " flags ", str_spec);
637 p = number(p, pend, res->flags, flag_spec);
642 return string(buf, end, sym, spec);
645 static noinline_for_stack
646 char *mac_address_string(char *buf, char *end, u8 *addr,
647 struct printf_spec spec, const char *fmt)
649 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
654 if (fmt[1] == 'F') { /* FDDI canonical format */
660 for (i = 0; i < 6; i++) {
661 p = hex_byte_pack(p, addr[i]);
662 if (fmt[0] == 'M' && i != 5)
667 return string(buf, end, mac_addr, spec);
670 static noinline_for_stack
671 char *ip4_string(char *p, const u8 *addr, const char *fmt)
674 bool leading_zeros = (fmt[0] == 'i');
699 for (i = 0; i < 4; i++) {
700 char temp[3]; /* hold each IP quad in reverse order */
701 int digits = put_dec_trunc8(temp, addr[index]) - temp;
708 /* reverse the digits in the quad */
720 static noinline_for_stack
721 char *ip6_compressed_string(char *p, const char *addr)
724 unsigned char zerolength[8];
729 bool needcolon = false;
733 memcpy(&in6, addr, sizeof(struct in6_addr));
735 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
737 memset(zerolength, 0, sizeof(zerolength));
744 /* find position of longest 0 run */
745 for (i = 0; i < range; i++) {
746 for (j = i; j < range; j++) {
747 if (in6.s6_addr16[j] != 0)
752 for (i = 0; i < range; i++) {
753 if (zerolength[i] > longest) {
754 longest = zerolength[i];
758 if (longest == 1) /* don't compress a single 0 */
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 = hex_byte_pack(p, hi);
783 *p++ = hex_asc_lo(hi);
784 p = hex_byte_pack(p, lo);
787 p = hex_byte_pack(p, lo);
789 *p++ = hex_asc_lo(lo);
796 p = ip4_string(p, &in6.s6_addr[12], "I4");
803 static noinline_for_stack
804 char *ip6_string(char *p, const char *addr, const char *fmt)
808 for (i = 0; i < 8; i++) {
809 p = hex_byte_pack(p, *addr++);
810 p = hex_byte_pack(p, *addr++);
811 if (fmt[0] == 'I' && i != 7)
819 static noinline_for_stack
820 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
821 struct printf_spec spec, const char *fmt)
823 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
825 if (fmt[0] == 'I' && fmt[2] == 'c')
826 ip6_compressed_string(ip6_addr, addr);
828 ip6_string(ip6_addr, addr, fmt);
830 return string(buf, end, ip6_addr, spec);
833 static noinline_for_stack
834 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
835 struct printf_spec spec, const char *fmt)
837 char ip4_addr[sizeof("255.255.255.255")];
839 ip4_string(ip4_addr, addr, fmt);
841 return string(buf, end, ip4_addr, spec);
844 static noinline_for_stack
845 char *uuid_string(char *buf, char *end, const u8 *addr,
846 struct printf_spec spec, const char *fmt)
848 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
851 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
852 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
853 const u8 *index = be;
858 uc = true; /* fall-through */
867 for (i = 0; i < 16; i++) {
868 p = hex_byte_pack(p, addr[index[i]]);
888 return string(buf, end, uuid, spec);
892 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
893 struct printf_spec spec)
895 spec.flags |= SPECIAL | SMALL | ZEROPAD;
896 if (spec.field_width == -1)
897 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
900 return number(buf, end, *(const netdev_features_t *)addr, spec);
903 int kptr_restrict __read_mostly;
906 * Show a '%p' thing. A kernel extension is that the '%p' is followed
907 * by an extra set of alphanumeric characters that are extended format
910 * Right now we handle:
912 * - 'F' For symbolic function descriptor pointers with offset
913 * - 'f' For simple symbolic function names without offset
914 * - 'S' For symbolic direct pointers with offset
915 * - 's' For symbolic direct pointers without offset
916 * - 'B' For backtraced symbolic direct pointers with offset
917 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
918 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
919 * - 'M' For a 6-byte MAC address, it prints the address in the
920 * usual colon-separated hex notation
921 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
922 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
923 * with a dash-separated hex notation
924 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
925 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
926 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
927 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
928 * IPv6 omits the colons (01020304...0f)
929 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
930 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
931 * - 'I6c' for IPv6 addresses printed as specified by
932 * http://tools.ietf.org/html/rfc5952
933 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
934 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
935 * Options for %pU are:
936 * b big endian lower case hex (default)
937 * B big endian UPPER case hex
938 * l little endian lower case hex
939 * L little endian UPPER case hex
940 * big endian output byte order is:
941 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
942 * little endian output byte order is:
943 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
944 * - 'V' For a struct va_format which contains a format string * and va_list *,
945 * call vsnprintf(->format, *->va_list).
946 * Implements a "recursive vsnprintf".
947 * Do not use this feature without some mechanism to verify the
948 * correctness of the format string and va_list arguments.
949 * - 'K' For a kernel pointer that should be hidden from unprivileged users
950 * - 'NF' For a netdev_features_t
952 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
953 * function pointers are really function descriptors, which contain a
954 * pointer to the real address.
956 static noinline_for_stack
957 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
958 struct printf_spec spec)
960 if (!ptr && *fmt != 'K') {
962 * Print (null) with the same width as a pointer so it makes
963 * tabular output look nice.
965 if (spec.field_width == -1)
966 spec.field_width = 2 * sizeof(void *);
967 return string(buf, end, "(null)", spec);
973 ptr = dereference_function_descriptor(ptr);
978 return symbol_string(buf, end, ptr, spec, *fmt);
981 return resource_string(buf, end, ptr, spec, fmt);
982 case 'M': /* Colon separated: 00:01:02:03:04:05 */
983 case 'm': /* Contiguous: 000102030405 */
984 /* [mM]F (FDDI, bit reversed) */
985 return mac_address_string(buf, end, ptr, spec, fmt);
986 case 'I': /* Formatted IP supported
988 * 6: 0001:0203:...:0708
989 * 6c: 1::708 or 1::1.2.3.4
991 case 'i': /* Contiguous:
997 return ip6_addr_string(buf, end, ptr, spec, fmt);
999 return ip4_addr_string(buf, end, ptr, spec, fmt);
1003 return uuid_string(buf, end, ptr, spec, fmt);
1008 va_copy(va, *((struct va_format *)ptr)->va);
1009 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1010 ((struct va_format *)ptr)->fmt, va);
1016 * %pK cannot be used in IRQ context because its test
1017 * for CAP_SYSLOG would be meaningless.
1019 if (in_irq() || in_serving_softirq() || in_nmi()) {
1020 if (spec.field_width == -1)
1021 spec.field_width = 2 * sizeof(void *);
1022 return string(buf, end, "pK-error", spec);
1024 if (!((kptr_restrict == 0) ||
1025 (kptr_restrict == 1 &&
1026 has_capability_noaudit(current, CAP_SYSLOG))))
1032 return netdev_feature_string(buf, end, ptr, spec);
1036 spec.flags |= SMALL;
1037 if (spec.field_width == -1) {
1038 spec.field_width = 2 * sizeof(void *);
1039 spec.flags |= ZEROPAD;
1043 return number(buf, end, (unsigned long) ptr, spec);
1047 * Helper function to decode printf style format.
1048 * Each call decode a token from the format and return the
1049 * number of characters read (or likely the delta where it wants
1050 * to go on the next call).
1051 * The decoded token is returned through the parameters
1053 * 'h', 'l', or 'L' for integer fields
1054 * 'z' support added 23/7/1999 S.H.
1055 * 'z' changed to 'Z' --davidm 1/25/99
1056 * 't' added for ptrdiff_t
1058 * @fmt: the format string
1059 * @type of the token returned
1060 * @flags: various flags such as +, -, # tokens..
1061 * @field_width: overwritten width
1062 * @base: base of the number (octal, hex, ...)
1063 * @precision: precision of a number
1064 * @qualifier: qualifier of a number (long, size_t, ...)
1066 static noinline_for_stack
1067 int format_decode(const char *fmt, struct printf_spec *spec)
1069 const char *start = fmt;
1071 /* we finished early by reading the field width */
1072 if (spec->type == FORMAT_TYPE_WIDTH) {
1073 if (spec->field_width < 0) {
1074 spec->field_width = -spec->field_width;
1075 spec->flags |= LEFT;
1077 spec->type = FORMAT_TYPE_NONE;
1081 /* we finished early by reading the precision */
1082 if (spec->type == FORMAT_TYPE_PRECISION) {
1083 if (spec->precision < 0)
1084 spec->precision = 0;
1086 spec->type = FORMAT_TYPE_NONE;
1091 spec->type = FORMAT_TYPE_NONE;
1093 for (; *fmt ; ++fmt) {
1098 /* Return the current non-format string */
1099 if (fmt != start || !*fmt)
1105 while (1) { /* this also skips first '%' */
1111 case '-': spec->flags |= LEFT; break;
1112 case '+': spec->flags |= PLUS; break;
1113 case ' ': spec->flags |= SPACE; break;
1114 case '#': spec->flags |= SPECIAL; break;
1115 case '0': spec->flags |= ZEROPAD; break;
1116 default: found = false;
1123 /* get field width */
1124 spec->field_width = -1;
1127 spec->field_width = skip_atoi(&fmt);
1128 else if (*fmt == '*') {
1129 /* it's the next argument */
1130 spec->type = FORMAT_TYPE_WIDTH;
1131 return ++fmt - start;
1135 /* get the precision */
1136 spec->precision = -1;
1139 if (isdigit(*fmt)) {
1140 spec->precision = skip_atoi(&fmt);
1141 if (spec->precision < 0)
1142 spec->precision = 0;
1143 } else if (*fmt == '*') {
1144 /* it's the next argument */
1145 spec->type = FORMAT_TYPE_PRECISION;
1146 return ++fmt - start;
1151 /* get the conversion qualifier */
1152 spec->qualifier = -1;
1153 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1154 _tolower(*fmt) == 'z' || *fmt == 't') {
1155 spec->qualifier = *fmt++;
1156 if (unlikely(spec->qualifier == *fmt)) {
1157 if (spec->qualifier == 'l') {
1158 spec->qualifier = 'L';
1160 } else if (spec->qualifier == 'h') {
1161 spec->qualifier = 'H';
1171 spec->type = FORMAT_TYPE_CHAR;
1172 return ++fmt - start;
1175 spec->type = FORMAT_TYPE_STR;
1176 return ++fmt - start;
1179 spec->type = FORMAT_TYPE_PTR;
1184 spec->type = FORMAT_TYPE_NRCHARS;
1185 return ++fmt - start;
1188 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1189 return ++fmt - start;
1191 /* integer number formats - set up the flags and "break" */
1197 spec->flags |= SMALL;
1205 spec->flags |= SIGN;
1210 spec->type = FORMAT_TYPE_INVALID;
1214 if (spec->qualifier == 'L')
1215 spec->type = FORMAT_TYPE_LONG_LONG;
1216 else if (spec->qualifier == 'l') {
1217 if (spec->flags & SIGN)
1218 spec->type = FORMAT_TYPE_LONG;
1220 spec->type = FORMAT_TYPE_ULONG;
1221 } else if (_tolower(spec->qualifier) == 'z') {
1222 spec->type = FORMAT_TYPE_SIZE_T;
1223 } else if (spec->qualifier == 't') {
1224 spec->type = FORMAT_TYPE_PTRDIFF;
1225 } else if (spec->qualifier == 'H') {
1226 if (spec->flags & SIGN)
1227 spec->type = FORMAT_TYPE_BYTE;
1229 spec->type = FORMAT_TYPE_UBYTE;
1230 } else if (spec->qualifier == 'h') {
1231 if (spec->flags & SIGN)
1232 spec->type = FORMAT_TYPE_SHORT;
1234 spec->type = FORMAT_TYPE_USHORT;
1236 if (spec->flags & SIGN)
1237 spec->type = FORMAT_TYPE_INT;
1239 spec->type = FORMAT_TYPE_UINT;
1242 return ++fmt - start;
1246 * vsnprintf - Format a string and place it in a buffer
1247 * @buf: The buffer to place the result into
1248 * @size: The size of the buffer, including the trailing null space
1249 * @fmt: The format string to use
1250 * @args: Arguments for the format string
1252 * This function follows C99 vsnprintf, but has some extensions:
1253 * %pS output the name of a text symbol with offset
1254 * %ps output the name of a text symbol without offset
1255 * %pF output the name of a function pointer with its offset
1256 * %pf output the name of a function pointer without its offset
1257 * %pB output the name of a backtrace symbol with its offset
1258 * %pR output the address range in a struct resource with decoded flags
1259 * %pr output the address range in a struct resource with raw flags
1260 * %pM output a 6-byte MAC address with colons
1261 * %pm output a 6-byte MAC address without colons
1262 * %pI4 print an IPv4 address without leading zeros
1263 * %pi4 print an IPv4 address with leading zeros
1264 * %pI6 print an IPv6 address with colons
1265 * %pi6 print an IPv6 address without colons
1266 * %pI6c print an IPv6 address as specified by RFC 5952
1267 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1271 * The return value is the number of characters which would
1272 * be generated for the given input, excluding the trailing
1273 * '\0', as per ISO C99. If you want to have the exact
1274 * number of characters written into @buf as return value
1275 * (not including the trailing '\0'), use vscnprintf(). If the
1276 * return is greater than or equal to @size, the resulting
1277 * string is truncated.
1279 * If you're not already dealing with a va_list consider using snprintf().
1281 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1283 unsigned long long num;
1285 struct printf_spec spec = {0};
1287 /* Reject out-of-range values early. Large positive sizes are
1288 used for unknown buffer sizes. */
1289 if (WARN_ON_ONCE((int) size < 0))
1295 /* Make sure end is always >= buf */
1302 const char *old_fmt = fmt;
1303 int read = format_decode(fmt, &spec);
1307 switch (spec.type) {
1308 case FORMAT_TYPE_NONE: {
1311 if (copy > end - str)
1313 memcpy(str, old_fmt, copy);
1319 case FORMAT_TYPE_WIDTH:
1320 spec.field_width = va_arg(args, int);
1323 case FORMAT_TYPE_PRECISION:
1324 spec.precision = va_arg(args, int);
1327 case FORMAT_TYPE_CHAR: {
1330 if (!(spec.flags & LEFT)) {
1331 while (--spec.field_width > 0) {
1338 c = (unsigned char) va_arg(args, int);
1342 while (--spec.field_width > 0) {
1350 case FORMAT_TYPE_STR:
1351 str = string(str, end, va_arg(args, char *), spec);
1354 case FORMAT_TYPE_PTR:
1355 str = pointer(fmt+1, str, end, va_arg(args, void *),
1357 while (isalnum(*fmt))
1361 case FORMAT_TYPE_PERCENT_CHAR:
1367 case FORMAT_TYPE_INVALID:
1373 case FORMAT_TYPE_NRCHARS: {
1374 u8 qualifier = spec.qualifier;
1376 if (qualifier == 'l') {
1377 long *ip = va_arg(args, long *);
1379 } else if (_tolower(qualifier) == 'z') {
1380 size_t *ip = va_arg(args, size_t *);
1383 int *ip = va_arg(args, int *);
1390 switch (spec.type) {
1391 case FORMAT_TYPE_LONG_LONG:
1392 num = va_arg(args, long long);
1394 case FORMAT_TYPE_ULONG:
1395 num = va_arg(args, unsigned long);
1397 case FORMAT_TYPE_LONG:
1398 num = va_arg(args, long);
1400 case FORMAT_TYPE_SIZE_T:
1401 num = va_arg(args, size_t);
1403 case FORMAT_TYPE_PTRDIFF:
1404 num = va_arg(args, ptrdiff_t);
1406 case FORMAT_TYPE_UBYTE:
1407 num = (unsigned char) va_arg(args, int);
1409 case FORMAT_TYPE_BYTE:
1410 num = (signed char) va_arg(args, int);
1412 case FORMAT_TYPE_USHORT:
1413 num = (unsigned short) va_arg(args, int);
1415 case FORMAT_TYPE_SHORT:
1416 num = (short) va_arg(args, int);
1418 case FORMAT_TYPE_INT:
1419 num = (int) va_arg(args, int);
1422 num = va_arg(args, unsigned int);
1425 str = number(str, end, num, spec);
1436 /* the trailing null byte doesn't count towards the total */
1440 EXPORT_SYMBOL(vsnprintf);
1443 * vscnprintf - Format a string and place it in a buffer
1444 * @buf: The buffer to place the result into
1445 * @size: The size of the buffer, including the trailing null space
1446 * @fmt: The format string to use
1447 * @args: Arguments for the format string
1449 * The return value is the number of characters which have been written into
1450 * the @buf not including the trailing '\0'. If @size is == 0 the function
1453 * If you're not already dealing with a va_list consider using scnprintf().
1455 * See the vsnprintf() documentation for format string extensions over C99.
1457 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1461 i = vsnprintf(buf, size, fmt, args);
1463 if (likely(i < size))
1469 EXPORT_SYMBOL(vscnprintf);
1472 * snprintf - Format a string and place it in a buffer
1473 * @buf: The buffer to place the result into
1474 * @size: The size of the buffer, including the trailing null space
1475 * @fmt: The format string to use
1476 * @...: Arguments for the format string
1478 * The return value is the number of characters which would be
1479 * generated for the given input, excluding the trailing null,
1480 * as per ISO C99. If the return is greater than or equal to
1481 * @size, the resulting string is truncated.
1483 * See the vsnprintf() documentation for format string extensions over C99.
1485 int snprintf(char *buf, size_t size, const char *fmt, ...)
1490 va_start(args, fmt);
1491 i = vsnprintf(buf, size, fmt, args);
1496 EXPORT_SYMBOL(snprintf);
1499 * scnprintf - Format a string and place it in a buffer
1500 * @buf: The buffer to place the result into
1501 * @size: The size of the buffer, including the trailing null space
1502 * @fmt: The format string to use
1503 * @...: Arguments for the format string
1505 * The return value is the number of characters written into @buf not including
1506 * the trailing '\0'. If @size is == 0 the function returns 0.
1509 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1514 va_start(args, fmt);
1515 i = vscnprintf(buf, size, fmt, args);
1520 EXPORT_SYMBOL(scnprintf);
1523 * vsprintf - Format a string and place it in a buffer
1524 * @buf: The buffer to place the result into
1525 * @fmt: The format string to use
1526 * @args: Arguments for the format string
1528 * The function returns the number of characters written
1529 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1532 * If you're not already dealing with a va_list consider using sprintf().
1534 * See the vsnprintf() documentation for format string extensions over C99.
1536 int vsprintf(char *buf, const char *fmt, va_list args)
1538 return vsnprintf(buf, INT_MAX, fmt, args);
1540 EXPORT_SYMBOL(vsprintf);
1543 * sprintf - Format a string and place it in a buffer
1544 * @buf: The buffer to place the result into
1545 * @fmt: The format string to use
1546 * @...: Arguments for the format string
1548 * The function returns the number of characters written
1549 * into @buf. Use snprintf() or scnprintf() in order to avoid
1552 * See the vsnprintf() documentation for format string extensions over C99.
1554 int sprintf(char *buf, const char *fmt, ...)
1559 va_start(args, fmt);
1560 i = vsnprintf(buf, INT_MAX, fmt, args);
1565 EXPORT_SYMBOL(sprintf);
1567 #ifdef CONFIG_BINARY_PRINTF
1570 * vbin_printf() - VA arguments to binary data
1571 * bstr_printf() - Binary data to text string
1575 * vbin_printf - Parse a format string and place args' binary value in a buffer
1576 * @bin_buf: The buffer to place args' binary value
1577 * @size: The size of the buffer(by words(32bits), not characters)
1578 * @fmt: The format string to use
1579 * @args: Arguments for the format string
1581 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1584 * The return value is the number of words(32bits) which would be generated for
1588 * If the return value is greater than @size, the resulting bin_buf is NOT
1589 * valid for bstr_printf().
1591 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1593 struct printf_spec spec = {0};
1596 str = (char *)bin_buf;
1597 end = (char *)(bin_buf + size);
1599 #define save_arg(type) \
1601 if (sizeof(type) == 8) { \
1602 unsigned long long value; \
1603 str = PTR_ALIGN(str, sizeof(u32)); \
1604 value = va_arg(args, unsigned long long); \
1605 if (str + sizeof(type) <= end) { \
1606 *(u32 *)str = *(u32 *)&value; \
1607 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1610 unsigned long value; \
1611 str = PTR_ALIGN(str, sizeof(type)); \
1612 value = va_arg(args, int); \
1613 if (str + sizeof(type) <= end) \
1614 *(typeof(type) *)str = (type)value; \
1616 str += sizeof(type); \
1620 int read = format_decode(fmt, &spec);
1624 switch (spec.type) {
1625 case FORMAT_TYPE_NONE:
1626 case FORMAT_TYPE_INVALID:
1627 case FORMAT_TYPE_PERCENT_CHAR:
1630 case FORMAT_TYPE_WIDTH:
1631 case FORMAT_TYPE_PRECISION:
1635 case FORMAT_TYPE_CHAR:
1639 case FORMAT_TYPE_STR: {
1640 const char *save_str = va_arg(args, char *);
1643 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1644 || (unsigned long)save_str < PAGE_SIZE)
1645 save_str = "(null)";
1646 len = strlen(save_str) + 1;
1647 if (str + len < end)
1648 memcpy(str, save_str, len);
1653 case FORMAT_TYPE_PTR:
1655 /* skip all alphanumeric pointer suffixes */
1656 while (isalnum(*fmt))
1660 case FORMAT_TYPE_NRCHARS: {
1661 /* skip %n 's argument */
1662 u8 qualifier = spec.qualifier;
1664 if (qualifier == 'l')
1665 skip_arg = va_arg(args, long *);
1666 else if (_tolower(qualifier) == 'z')
1667 skip_arg = va_arg(args, size_t *);
1669 skip_arg = va_arg(args, int *);
1674 switch (spec.type) {
1676 case FORMAT_TYPE_LONG_LONG:
1677 save_arg(long long);
1679 case FORMAT_TYPE_ULONG:
1680 case FORMAT_TYPE_LONG:
1681 save_arg(unsigned long);
1683 case FORMAT_TYPE_SIZE_T:
1686 case FORMAT_TYPE_PTRDIFF:
1687 save_arg(ptrdiff_t);
1689 case FORMAT_TYPE_UBYTE:
1690 case FORMAT_TYPE_BYTE:
1693 case FORMAT_TYPE_USHORT:
1694 case FORMAT_TYPE_SHORT:
1703 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1706 EXPORT_SYMBOL_GPL(vbin_printf);
1709 * bstr_printf - Format a string from binary arguments and place it in a buffer
1710 * @buf: The buffer to place the result into
1711 * @size: The size of the buffer, including the trailing null space
1712 * @fmt: The format string to use
1713 * @bin_buf: Binary arguments for the format string
1715 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1716 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1717 * a binary buffer that generated by vbin_printf.
1719 * The format follows C99 vsnprintf, but has some extensions:
1720 * see vsnprintf comment for details.
1722 * The return value is the number of characters which would
1723 * be generated for the given input, excluding the trailing
1724 * '\0', as per ISO C99. If you want to have the exact
1725 * number of characters written into @buf as return value
1726 * (not including the trailing '\0'), use vscnprintf(). If the
1727 * return is greater than or equal to @size, the resulting
1728 * string is truncated.
1730 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1732 struct printf_spec spec = {0};
1734 const char *args = (const char *)bin_buf;
1736 if (WARN_ON_ONCE((int) size < 0))
1742 #define get_arg(type) \
1744 typeof(type) value; \
1745 if (sizeof(type) == 8) { \
1746 args = PTR_ALIGN(args, sizeof(u32)); \
1747 *(u32 *)&value = *(u32 *)args; \
1748 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1750 args = PTR_ALIGN(args, sizeof(type)); \
1751 value = *(typeof(type) *)args; \
1753 args += sizeof(type); \
1757 /* Make sure end is always >= buf */
1764 const char *old_fmt = fmt;
1765 int read = format_decode(fmt, &spec);
1769 switch (spec.type) {
1770 case FORMAT_TYPE_NONE: {
1773 if (copy > end - str)
1775 memcpy(str, old_fmt, copy);
1781 case FORMAT_TYPE_WIDTH:
1782 spec.field_width = get_arg(int);
1785 case FORMAT_TYPE_PRECISION:
1786 spec.precision = get_arg(int);
1789 case FORMAT_TYPE_CHAR: {
1792 if (!(spec.flags & LEFT)) {
1793 while (--spec.field_width > 0) {
1799 c = (unsigned char) get_arg(char);
1803 while (--spec.field_width > 0) {
1811 case FORMAT_TYPE_STR: {
1812 const char *str_arg = args;
1813 args += strlen(str_arg) + 1;
1814 str = string(str, end, (char *)str_arg, spec);
1818 case FORMAT_TYPE_PTR:
1819 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1820 while (isalnum(*fmt))
1824 case FORMAT_TYPE_PERCENT_CHAR:
1825 case FORMAT_TYPE_INVALID:
1831 case FORMAT_TYPE_NRCHARS:
1836 unsigned long long num;
1838 switch (spec.type) {
1840 case FORMAT_TYPE_LONG_LONG:
1841 num = get_arg(long long);
1843 case FORMAT_TYPE_ULONG:
1844 case FORMAT_TYPE_LONG:
1845 num = get_arg(unsigned long);
1847 case FORMAT_TYPE_SIZE_T:
1848 num = get_arg(size_t);
1850 case FORMAT_TYPE_PTRDIFF:
1851 num = get_arg(ptrdiff_t);
1853 case FORMAT_TYPE_UBYTE:
1854 num = get_arg(unsigned char);
1856 case FORMAT_TYPE_BYTE:
1857 num = get_arg(signed char);
1859 case FORMAT_TYPE_USHORT:
1860 num = get_arg(unsigned short);
1862 case FORMAT_TYPE_SHORT:
1863 num = get_arg(short);
1865 case FORMAT_TYPE_UINT:
1866 num = get_arg(unsigned int);
1872 str = number(str, end, num, spec);
1874 } /* switch(spec.type) */
1886 /* the trailing null byte doesn't count towards the total */
1889 EXPORT_SYMBOL_GPL(bstr_printf);
1892 * bprintf - Parse a format string and place args' binary value in a buffer
1893 * @bin_buf: The buffer to place args' binary value
1894 * @size: The size of the buffer(by words(32bits), not characters)
1895 * @fmt: The format string to use
1896 * @...: Arguments for the format string
1898 * The function returns the number of words(u32) written
1901 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1906 va_start(args, fmt);
1907 ret = vbin_printf(bin_buf, size, fmt, args);
1912 EXPORT_SYMBOL_GPL(bprintf);
1914 #endif /* CONFIG_BINARY_PRINTF */
1917 * vsscanf - Unformat a buffer into a list of arguments
1918 * @buf: input buffer
1919 * @fmt: format of buffer
1922 int vsscanf(const char *buf, const char *fmt, va_list args)
1924 const char *str = buf;
1933 while (*fmt && *str) {
1934 /* skip any white space in format */
1935 /* white space in format matchs any amount of
1936 * white space, including none, in the input.
1938 if (isspace(*fmt)) {
1939 fmt = skip_spaces(++fmt);
1940 str = skip_spaces(str);
1943 /* anything that is not a conversion must match exactly */
1944 if (*fmt != '%' && *fmt) {
1945 if (*fmt++ != *str++)
1954 /* skip this conversion.
1955 * advance both strings to next white space
1958 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1960 while (!isspace(*str) && *str)
1965 /* get field width */
1968 field_width = skip_atoi(&fmt);
1970 /* get conversion qualifier */
1972 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1973 _tolower(*fmt) == 'z') {
1975 if (unlikely(qualifier == *fmt)) {
1976 if (qualifier == 'h') {
1979 } else if (qualifier == 'l') {
1995 char *s = (char *)va_arg(args, char*);
1996 if (field_width == -1)
2000 } while (--field_width > 0 && *str);
2006 char *s = (char *)va_arg(args, char *);
2007 if (field_width == -1)
2008 field_width = SHRT_MAX;
2009 /* first, skip leading white space in buffer */
2010 str = skip_spaces(str);
2012 /* now copy until next white space */
2013 while (*str && !isspace(*str) && field_width--)
2020 /* return number of characters read so far */
2022 int *i = (int *)va_arg(args, int*);
2040 /* looking for '%' in str */
2045 /* invalid format; stop here */
2049 /* have some sort of integer conversion.
2050 * first, skip white space in buffer.
2052 str = skip_spaces(str);
2055 if (is_sign && digit == '-')
2059 || (base == 16 && !isxdigit(digit))
2060 || (base == 10 && !isdigit(digit))
2061 || (base == 8 && (!isdigit(digit) || digit > '7'))
2062 || (base == 0 && !isdigit(digit)))
2065 switch (qualifier) {
2066 case 'H': /* that's 'hh' in format */
2068 signed char *s = (signed char *)va_arg(args, signed char *);
2069 *s = (signed char)simple_strtol(str, &next, base);
2071 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2072 *s = (unsigned char)simple_strtoul(str, &next, base);
2077 short *s = (short *)va_arg(args, short *);
2078 *s = (short)simple_strtol(str, &next, base);
2080 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2081 *s = (unsigned short)simple_strtoul(str, &next, base);
2086 long *l = (long *)va_arg(args, long *);
2087 *l = simple_strtol(str, &next, base);
2089 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2090 *l = simple_strtoul(str, &next, base);
2095 long long *l = (long long *)va_arg(args, long long *);
2096 *l = simple_strtoll(str, &next, base);
2098 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2099 *l = simple_strtoull(str, &next, base);
2105 size_t *s = (size_t *)va_arg(args, size_t *);
2106 *s = (size_t)simple_strtoul(str, &next, base);
2111 int *i = (int *)va_arg(args, int *);
2112 *i = (int)simple_strtol(str, &next, base);
2114 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2115 *i = (unsigned int)simple_strtoul(str, &next, base);
2127 * Now we've come all the way through so either the input string or the
2128 * format ended. In the former case, there can be a %n at the current
2129 * position in the format that needs to be filled.
2131 if (*fmt == '%' && *(fmt + 1) == 'n') {
2132 int *p = (int *)va_arg(args, int *);
2138 EXPORT_SYMBOL(vsscanf);
2141 * sscanf - Unformat a buffer into a list of arguments
2142 * @buf: input buffer
2143 * @fmt: formatting of buffer
2144 * @...: resulting arguments
2146 int sscanf(const char *buf, const char *fmt, ...)
2151 va_start(args, fmt);
2152 i = vsscanf(buf, fmt, args);
2157 EXPORT_SYMBOL(sscanf);