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)
127 * Possible ways to approx. divide by 10
128 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
129 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
130 * (x * 0x6667) >> 18 x < 43699
131 * (x * 0x3334) >> 17 x < 16389
132 * (x * 0x199a) >> 16 x < 16389
133 * (x * 0x0ccd) >> 15 x < 16389
134 * (x * 0x0667) >> 14 x < 2739
135 * (x * 0x0334) >> 13 x < 1029
136 * (x * 0x019a) >> 12 x < 1029
137 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
138 * (x * 0x0067) >> 10 x < 179
139 * (x * 0x0034) >> 9 x < 69 same
140 * (x * 0x001a) >> 8 x < 69 same
141 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
142 * (x * 0x0007) >> 6 x < 19
143 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
145 r = (q * (uint64_t)0x1999999a) >> 32;
146 *buf++ = (q - 10 * r) + '0'; /* 1 */
147 q = (r * (uint64_t)0x1999999a) >> 32;
148 *buf++ = (r - 10 * q) + '0'; /* 2 */
149 r = (q * (uint64_t)0x1999999a) >> 32;
150 *buf++ = (q - 10 * r) + '0'; /* 3 */
151 q = (r * (uint64_t)0x1999999a) >> 32;
152 *buf++ = (r - 10 * q) + '0'; /* 4 */
153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 5 */
155 /* Now value is under 10000, can avoid 64-bit multiply */
156 q = (r * 0x199a) >> 16;
157 *buf++ = (r - 10 * q) + '0'; /* 6 */
158 r = (q * 0xcd) >> 11;
159 *buf++ = (q - 10 * r) + '0'; /* 7 */
160 q = (r * 0xcd) >> 11;
161 *buf++ = (r - 10 * q) + '0'; /* 8 */
162 *buf++ = q + '0'; /* 9 */
167 /* Similar to above but do not pad with zeros.
168 * Code can be easily arranged to print 9 digits too, but our callers
169 * always call put_dec_full9() instead when the number has 9 decimal digits.
171 static noinline_for_stack
172 char *put_dec_trunc8(char *buf, unsigned r)
176 /* Copy of previous function's body with added early returns */
177 q = (r * (uint64_t)0x1999999a) >> 32;
178 *buf++ = (r - 10 * q) + '0'; /* 2 */
181 r = (q * (uint64_t)0x1999999a) >> 32;
182 *buf++ = (q - 10 * r) + '0'; /* 3 */
185 q = (r * (uint64_t)0x1999999a) >> 32;
186 *buf++ = (r - 10 * q) + '0'; /* 4 */
189 r = (q * (uint64_t)0x1999999a) >> 32;
190 *buf++ = (q - 10 * r) + '0'; /* 5 */
193 q = (r * 0x199a) >> 16;
194 *buf++ = (r - 10 * q) + '0'; /* 6 */
197 r = (q * 0xcd) >> 11;
198 *buf++ = (q - 10 * r) + '0'; /* 7 */
201 q = (r * 0xcd) >> 11;
202 *buf++ = (r - 10 * q) + '0'; /* 8 */
205 *buf++ = q + '0'; /* 9 */
209 /* There are two algorithms to print larger numbers.
210 * One is generic: divide by 1000000000 and repeatedly print
211 * groups of (up to) 9 digits. It's conceptually simple,
212 * but requires a (unsigned long long) / 1000000000 division.
214 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
215 * manipulates them cleverly and generates groups of 4 decimal digits.
216 * It so happens that it does NOT require long long division.
218 * If long is > 32 bits, division of 64-bit values is relatively easy,
219 * and we will use the first algorithm.
220 * If long long is > 64 bits (strange architecture with VERY large long long),
221 * second algorithm can't be used, and we again use the first one.
223 * Else (if long is 32 bits and long long is 64 bits) we use second one.
226 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
228 /* First algorithm: generic */
231 char *put_dec(char *buf, unsigned long long n)
233 if (n >= 100*1000*1000) {
234 while (n >= 1000*1000*1000)
235 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
236 if (n >= 100*1000*1000)
237 return put_dec_full9(buf, n);
239 return put_dec_trunc8(buf, n);
244 /* Second algorithm: valid only for 64-bit long longs */
246 static noinline_for_stack
247 char *put_dec_full4(char *buf, unsigned q)
250 r = (q * 0xcccd) >> 19;
251 *buf++ = (q - 10 * r) + '0';
252 q = (r * 0x199a) >> 16;
253 *buf++ = (r - 10 * q) + '0';
254 r = (q * 0xcd) >> 11;
255 *buf++ = (q - 10 * r) + '0';
260 /* Based on code by Douglas W. Jones found at
261 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
262 * (with permission from the author).
263 * Performs no 64-bit division and hence should be fast on 32-bit machines.
266 char *put_dec(char *buf, unsigned long long n)
268 uint32_t d3, d2, d1, q, h;
270 if (n < 100*1000*1000)
271 return put_dec_trunc8(buf, n);
273 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
276 d3 = (h >> 16); /* implicit "& 0xffff" */
278 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
280 buf = put_dec_full4(buf, q % 10000);
283 d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1;
284 buf = put_dec_full4(buf, d1 % 10000);
287 d2 = q + 4749 * d3 + 42 * d2;
288 buf = put_dec_full4(buf, d2 % 10000);
294 buf = put_dec_full4(buf, d3 % 10000);
298 buf = put_dec_full4(buf, q);
300 while (buf[-1] == '0')
309 * Convert passed number to decimal string.
310 * Returns the length of string. On buffer overflow, returns 0.
312 * If speed is not important, use snprintf(). It's easy to read the code.
314 int num_to_str(char *buf, int size, unsigned long long num)
316 char tmp[sizeof(num) * 3];
319 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
324 len = put_dec(tmp, num) - tmp;
329 for (idx = 0; idx < len; ++idx)
330 buf[idx] = tmp[len - idx - 1];
334 #define ZEROPAD 1 /* pad with zero */
335 #define SIGN 2 /* unsigned/signed long */
336 #define PLUS 4 /* show plus */
337 #define SPACE 8 /* space if plus */
338 #define LEFT 16 /* left justified */
339 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
340 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
343 FORMAT_TYPE_NONE, /* Just a string part */
345 FORMAT_TYPE_PRECISION,
349 FORMAT_TYPE_PERCENT_CHAR,
351 FORMAT_TYPE_LONG_LONG,
366 u8 type; /* format_type enum */
367 u8 flags; /* flags to number() */
368 u8 base; /* number base, 8, 10 or 16 only */
369 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
370 s16 field_width; /* width of output field */
371 s16 precision; /* # of digits/chars */
374 static noinline_for_stack
375 char *number(char *buf, char *end, unsigned long long num,
376 struct printf_spec spec)
378 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
379 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
384 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
386 bool is_zero = num == 0LL;
388 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
389 * produces same digits or (maybe lowercased) letters */
390 locase = (spec.flags & SMALL);
391 if (spec.flags & LEFT)
392 spec.flags &= ~ZEROPAD;
394 if (spec.flags & SIGN) {
395 if ((signed long long)num < 0) {
397 num = -(signed long long)num;
399 } else if (spec.flags & PLUS) {
402 } else if (spec.flags & SPACE) {
409 spec.field_width -= 2;
414 /* generate full string in tmp[], in reverse order */
417 tmp[i++] = digits[num] | locase;
418 /* Generic code, for any base:
420 tmp[i++] = (digits[do_div(num,base)] | locase);
423 else if (spec.base != 10) { /* 8 or 16 */
424 int mask = spec.base - 1;
430 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
433 } else { /* base 10 */
434 i = put_dec(tmp, num) - tmp;
437 /* printing 100 using %2d gives "100", not "00" */
438 if (i > spec.precision)
440 /* leading space padding */
441 spec.field_width -= spec.precision;
442 if (!(spec.flags & (ZEROPAD+LEFT))) {
443 while (--spec.field_width >= 0) {
455 /* "0x" / "0" prefix */
457 if (spec.base == 16 || !is_zero) {
462 if (spec.base == 16) {
464 *buf = ('X' | locase);
468 /* zero or space padding */
469 if (!(spec.flags & LEFT)) {
470 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
471 while (--spec.field_width >= 0) {
477 /* hmm even more zero padding? */
478 while (i <= --spec.precision) {
483 /* actual digits of result */
489 /* trailing space padding */
490 while (--spec.field_width >= 0) {
499 static noinline_for_stack
500 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
504 if ((unsigned long)s < PAGE_SIZE)
507 len = strnlen(s, spec.precision);
509 if (!(spec.flags & LEFT)) {
510 while (len < spec.field_width--) {
516 for (i = 0; i < len; ++i) {
521 while (len < spec.field_width--) {
530 static noinline_for_stack
531 char *symbol_string(char *buf, char *end, void *ptr,
532 struct printf_spec spec, char ext)
534 unsigned long value = (unsigned long) ptr;
535 #ifdef CONFIG_KALLSYMS
536 char sym[KSYM_SYMBOL_LEN];
538 sprint_backtrace(sym, value);
539 else if (ext != 'f' && ext != 's')
540 sprint_symbol(sym, value);
542 sprint_symbol_no_offset(sym, value);
544 return string(buf, end, sym, spec);
546 spec.field_width = 2 * sizeof(void *);
547 spec.flags |= SPECIAL | SMALL | ZEROPAD;
550 return number(buf, end, value, spec);
554 static noinline_for_stack
555 char *resource_string(char *buf, char *end, struct resource *res,
556 struct printf_spec spec, const char *fmt)
558 #ifndef IO_RSRC_PRINTK_SIZE
559 #define IO_RSRC_PRINTK_SIZE 6
562 #ifndef MEM_RSRC_PRINTK_SIZE
563 #define MEM_RSRC_PRINTK_SIZE 10
565 static const struct printf_spec io_spec = {
567 .field_width = IO_RSRC_PRINTK_SIZE,
569 .flags = SPECIAL | SMALL | ZEROPAD,
571 static const struct printf_spec mem_spec = {
573 .field_width = MEM_RSRC_PRINTK_SIZE,
575 .flags = SPECIAL | SMALL | ZEROPAD,
577 static const struct printf_spec bus_spec = {
581 .flags = SMALL | ZEROPAD,
583 static const struct printf_spec dec_spec = {
588 static const struct printf_spec str_spec = {
593 static const struct printf_spec flag_spec = {
596 .flags = SPECIAL | SMALL,
599 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
600 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
601 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
602 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
603 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
604 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
605 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
606 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
608 char *p = sym, *pend = sym + sizeof(sym);
609 int decode = (fmt[0] == 'R') ? 1 : 0;
610 const struct printf_spec *specp;
613 if (res->flags & IORESOURCE_IO) {
614 p = string(p, pend, "io ", str_spec);
616 } else if (res->flags & IORESOURCE_MEM) {
617 p = string(p, pend, "mem ", str_spec);
619 } else if (res->flags & IORESOURCE_IRQ) {
620 p = string(p, pend, "irq ", str_spec);
622 } else if (res->flags & IORESOURCE_DMA) {
623 p = string(p, pend, "dma ", str_spec);
625 } else if (res->flags & IORESOURCE_BUS) {
626 p = string(p, pend, "bus ", str_spec);
629 p = string(p, pend, "??? ", str_spec);
633 p = number(p, pend, res->start, *specp);
634 if (res->start != res->end) {
636 p = number(p, pend, res->end, *specp);
639 if (res->flags & IORESOURCE_MEM_64)
640 p = string(p, pend, " 64bit", str_spec);
641 if (res->flags & IORESOURCE_PREFETCH)
642 p = string(p, pend, " pref", str_spec);
643 if (res->flags & IORESOURCE_WINDOW)
644 p = string(p, pend, " window", str_spec);
645 if (res->flags & IORESOURCE_DISABLED)
646 p = string(p, pend, " disabled", str_spec);
648 p = string(p, pend, " flags ", str_spec);
649 p = number(p, pend, res->flags, flag_spec);
654 return string(buf, end, sym, spec);
657 static noinline_for_stack
658 char *mac_address_string(char *buf, char *end, u8 *addr,
659 struct printf_spec spec, const char *fmt)
661 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
665 bool reversed = false;
681 for (i = 0; i < 6; i++) {
683 p = hex_byte_pack(p, addr[5 - i]);
685 p = hex_byte_pack(p, addr[i]);
687 if (fmt[0] == 'M' && i != 5)
692 return string(buf, end, mac_addr, spec);
695 static noinline_for_stack
696 char *ip4_string(char *p, const u8 *addr, const char *fmt)
699 bool leading_zeros = (fmt[0] == 'i');
724 for (i = 0; i < 4; i++) {
725 char temp[3]; /* hold each IP quad in reverse order */
726 int digits = put_dec_trunc8(temp, addr[index]) - temp;
733 /* reverse the digits in the quad */
745 static noinline_for_stack
746 char *ip6_compressed_string(char *p, const char *addr)
749 unsigned char zerolength[8];
754 bool needcolon = false;
758 memcpy(&in6, addr, sizeof(struct in6_addr));
760 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
762 memset(zerolength, 0, sizeof(zerolength));
769 /* find position of longest 0 run */
770 for (i = 0; i < range; i++) {
771 for (j = i; j < range; j++) {
772 if (in6.s6_addr16[j] != 0)
777 for (i = 0; i < range; i++) {
778 if (zerolength[i] > longest) {
779 longest = zerolength[i];
783 if (longest == 1) /* don't compress a single 0 */
787 for (i = 0; i < range; i++) {
789 if (needcolon || i == 0)
800 /* hex u16 without leading 0s */
801 word = ntohs(in6.s6_addr16[i]);
806 p = hex_byte_pack(p, hi);
808 *p++ = hex_asc_lo(hi);
809 p = hex_byte_pack(p, lo);
812 p = hex_byte_pack(p, lo);
814 *p++ = hex_asc_lo(lo);
821 p = ip4_string(p, &in6.s6_addr[12], "I4");
828 static noinline_for_stack
829 char *ip6_string(char *p, const char *addr, const char *fmt)
833 for (i = 0; i < 8; i++) {
834 p = hex_byte_pack(p, *addr++);
835 p = hex_byte_pack(p, *addr++);
836 if (fmt[0] == 'I' && i != 7)
844 static noinline_for_stack
845 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
846 struct printf_spec spec, const char *fmt)
848 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
850 if (fmt[0] == 'I' && fmt[2] == 'c')
851 ip6_compressed_string(ip6_addr, addr);
853 ip6_string(ip6_addr, addr, fmt);
855 return string(buf, end, ip6_addr, spec);
858 static noinline_for_stack
859 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
860 struct printf_spec spec, const char *fmt)
862 char ip4_addr[sizeof("255.255.255.255")];
864 ip4_string(ip4_addr, addr, fmt);
866 return string(buf, end, ip4_addr, spec);
869 static noinline_for_stack
870 char *uuid_string(char *buf, char *end, const u8 *addr,
871 struct printf_spec spec, const char *fmt)
873 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
876 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
877 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
878 const u8 *index = be;
883 uc = true; /* fall-through */
892 for (i = 0; i < 16; i++) {
893 p = hex_byte_pack(p, addr[index[i]]);
913 return string(buf, end, uuid, spec);
917 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
918 struct printf_spec spec)
920 spec.flags |= SPECIAL | SMALL | ZEROPAD;
921 if (spec.field_width == -1)
922 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
925 return number(buf, end, *(const netdev_features_t *)addr, spec);
928 int kptr_restrict __read_mostly;
931 * Show a '%p' thing. A kernel extension is that the '%p' is followed
932 * by an extra set of alphanumeric characters that are extended format
935 * Right now we handle:
937 * - 'F' For symbolic function descriptor pointers with offset
938 * - 'f' For simple symbolic function names without offset
939 * - 'S' For symbolic direct pointers with offset
940 * - 's' For symbolic direct pointers without offset
941 * - 'B' For backtraced symbolic direct pointers with offset
942 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
943 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
944 * - 'M' For a 6-byte MAC address, it prints the address in the
945 * usual colon-separated hex notation
946 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
947 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
948 * with a dash-separated hex notation
949 * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
950 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
951 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
952 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
953 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
954 * IPv6 omits the colons (01020304...0f)
955 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
956 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
957 * - 'I6c' for IPv6 addresses printed as specified by
958 * http://tools.ietf.org/html/rfc5952
959 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
960 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
961 * Options for %pU are:
962 * b big endian lower case hex (default)
963 * B big endian UPPER case hex
964 * l little endian lower case hex
965 * L little endian UPPER case hex
966 * big endian output byte order is:
967 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
968 * little endian output byte order is:
969 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
970 * - 'V' For a struct va_format which contains a format string * and va_list *,
971 * call vsnprintf(->format, *->va_list).
972 * Implements a "recursive vsnprintf".
973 * Do not use this feature without some mechanism to verify the
974 * correctness of the format string and va_list arguments.
975 * - 'K' For a kernel pointer that should be hidden from unprivileged users
976 * - 'NF' For a netdev_features_t
978 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
979 * function pointers are really function descriptors, which contain a
980 * pointer to the real address.
982 static noinline_for_stack
983 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
984 struct printf_spec spec)
986 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
988 if (!ptr && *fmt != 'K') {
990 * Print (null) with the same width as a pointer so it makes
991 * tabular output look nice.
993 if (spec.field_width == -1)
994 spec.field_width = default_width;
995 return string(buf, end, "(null)", spec);
1001 ptr = dereference_function_descriptor(ptr);
1006 return symbol_string(buf, end, ptr, spec, *fmt);
1009 return resource_string(buf, end, ptr, spec, fmt);
1010 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1011 case 'm': /* Contiguous: 000102030405 */
1013 /* [mM]R (Reverse order; Bluetooth) */
1014 return mac_address_string(buf, end, ptr, spec, fmt);
1015 case 'I': /* Formatted IP supported
1017 * 6: 0001:0203:...:0708
1018 * 6c: 1::708 or 1::1.2.3.4
1020 case 'i': /* Contiguous:
1021 * 4: 001.002.003.004
1026 return ip6_addr_string(buf, end, ptr, spec, fmt);
1028 return ip4_addr_string(buf, end, ptr, spec, fmt);
1032 return uuid_string(buf, end, ptr, spec, fmt);
1037 va_copy(va, *((struct va_format *)ptr)->va);
1038 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1039 ((struct va_format *)ptr)->fmt, va);
1045 * %pK cannot be used in IRQ context because its test
1046 * for CAP_SYSLOG would be meaningless.
1048 if (in_irq() || in_serving_softirq() || in_nmi()) {
1049 if (spec.field_width == -1)
1050 spec.field_width = default_width;
1051 return string(buf, end, "pK-error", spec);
1053 if (!((kptr_restrict == 0) ||
1054 (kptr_restrict == 1 &&
1055 has_capability_noaudit(current, CAP_SYSLOG))))
1061 return netdev_feature_string(buf, end, ptr, spec);
1065 spec.flags |= SMALL;
1066 if (spec.field_width == -1) {
1067 spec.field_width = default_width;
1068 spec.flags |= ZEROPAD;
1072 return number(buf, end, (unsigned long) ptr, spec);
1076 * Helper function to decode printf style format.
1077 * Each call decode a token from the format and return the
1078 * number of characters read (or likely the delta where it wants
1079 * to go on the next call).
1080 * The decoded token is returned through the parameters
1082 * 'h', 'l', or 'L' for integer fields
1083 * 'z' support added 23/7/1999 S.H.
1084 * 'z' changed to 'Z' --davidm 1/25/99
1085 * 't' added for ptrdiff_t
1087 * @fmt: the format string
1088 * @type of the token returned
1089 * @flags: various flags such as +, -, # tokens..
1090 * @field_width: overwritten width
1091 * @base: base of the number (octal, hex, ...)
1092 * @precision: precision of a number
1093 * @qualifier: qualifier of a number (long, size_t, ...)
1095 static noinline_for_stack
1096 int format_decode(const char *fmt, struct printf_spec *spec)
1098 const char *start = fmt;
1100 /* we finished early by reading the field width */
1101 if (spec->type == FORMAT_TYPE_WIDTH) {
1102 if (spec->field_width < 0) {
1103 spec->field_width = -spec->field_width;
1104 spec->flags |= LEFT;
1106 spec->type = FORMAT_TYPE_NONE;
1110 /* we finished early by reading the precision */
1111 if (spec->type == FORMAT_TYPE_PRECISION) {
1112 if (spec->precision < 0)
1113 spec->precision = 0;
1115 spec->type = FORMAT_TYPE_NONE;
1120 spec->type = FORMAT_TYPE_NONE;
1122 for (; *fmt ; ++fmt) {
1127 /* Return the current non-format string */
1128 if (fmt != start || !*fmt)
1134 while (1) { /* this also skips first '%' */
1140 case '-': spec->flags |= LEFT; break;
1141 case '+': spec->flags |= PLUS; break;
1142 case ' ': spec->flags |= SPACE; break;
1143 case '#': spec->flags |= SPECIAL; break;
1144 case '0': spec->flags |= ZEROPAD; break;
1145 default: found = false;
1152 /* get field width */
1153 spec->field_width = -1;
1156 spec->field_width = skip_atoi(&fmt);
1157 else if (*fmt == '*') {
1158 /* it's the next argument */
1159 spec->type = FORMAT_TYPE_WIDTH;
1160 return ++fmt - start;
1164 /* get the precision */
1165 spec->precision = -1;
1168 if (isdigit(*fmt)) {
1169 spec->precision = skip_atoi(&fmt);
1170 if (spec->precision < 0)
1171 spec->precision = 0;
1172 } else if (*fmt == '*') {
1173 /* it's the next argument */
1174 spec->type = FORMAT_TYPE_PRECISION;
1175 return ++fmt - start;
1180 /* get the conversion qualifier */
1181 spec->qualifier = -1;
1182 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1183 _tolower(*fmt) == 'z' || *fmt == 't') {
1184 spec->qualifier = *fmt++;
1185 if (unlikely(spec->qualifier == *fmt)) {
1186 if (spec->qualifier == 'l') {
1187 spec->qualifier = 'L';
1189 } else if (spec->qualifier == 'h') {
1190 spec->qualifier = 'H';
1200 spec->type = FORMAT_TYPE_CHAR;
1201 return ++fmt - start;
1204 spec->type = FORMAT_TYPE_STR;
1205 return ++fmt - start;
1208 spec->type = FORMAT_TYPE_PTR;
1213 spec->type = FORMAT_TYPE_NRCHARS;
1214 return ++fmt - start;
1217 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1218 return ++fmt - start;
1220 /* integer number formats - set up the flags and "break" */
1226 spec->flags |= SMALL;
1234 spec->flags |= SIGN;
1239 spec->type = FORMAT_TYPE_INVALID;
1243 if (spec->qualifier == 'L')
1244 spec->type = FORMAT_TYPE_LONG_LONG;
1245 else if (spec->qualifier == 'l') {
1246 if (spec->flags & SIGN)
1247 spec->type = FORMAT_TYPE_LONG;
1249 spec->type = FORMAT_TYPE_ULONG;
1250 } else if (_tolower(spec->qualifier) == 'z') {
1251 spec->type = FORMAT_TYPE_SIZE_T;
1252 } else if (spec->qualifier == 't') {
1253 spec->type = FORMAT_TYPE_PTRDIFF;
1254 } else if (spec->qualifier == 'H') {
1255 if (spec->flags & SIGN)
1256 spec->type = FORMAT_TYPE_BYTE;
1258 spec->type = FORMAT_TYPE_UBYTE;
1259 } else if (spec->qualifier == 'h') {
1260 if (spec->flags & SIGN)
1261 spec->type = FORMAT_TYPE_SHORT;
1263 spec->type = FORMAT_TYPE_USHORT;
1265 if (spec->flags & SIGN)
1266 spec->type = FORMAT_TYPE_INT;
1268 spec->type = FORMAT_TYPE_UINT;
1271 return ++fmt - start;
1275 * vsnprintf - Format a string and place it in a buffer
1276 * @buf: The buffer to place the result into
1277 * @size: The size of the buffer, including the trailing null space
1278 * @fmt: The format string to use
1279 * @args: Arguments for the format string
1281 * This function follows C99 vsnprintf, but has some extensions:
1282 * %pS output the name of a text symbol with offset
1283 * %ps output the name of a text symbol without offset
1284 * %pF output the name of a function pointer with its offset
1285 * %pf output the name of a function pointer without its offset
1286 * %pB output the name of a backtrace symbol with its offset
1287 * %pR output the address range in a struct resource with decoded flags
1288 * %pr output the address range in a struct resource with raw flags
1289 * %pM output a 6-byte MAC address with colons
1290 * %pm output a 6-byte MAC address without colons
1291 * %pI4 print an IPv4 address without leading zeros
1292 * %pi4 print an IPv4 address with leading zeros
1293 * %pI6 print an IPv6 address with colons
1294 * %pi6 print an IPv6 address without colons
1295 * %pI6c print an IPv6 address as specified by RFC 5952
1296 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1300 * The return value is the number of characters which would
1301 * be generated for the given input, excluding the trailing
1302 * '\0', as per ISO C99. If you want to have the exact
1303 * number of characters written into @buf as return value
1304 * (not including the trailing '\0'), use vscnprintf(). If the
1305 * return is greater than or equal to @size, the resulting
1306 * string is truncated.
1308 * If you're not already dealing with a va_list consider using snprintf().
1310 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1312 unsigned long long num;
1314 struct printf_spec spec = {0};
1316 /* Reject out-of-range values early. Large positive sizes are
1317 used for unknown buffer sizes. */
1318 if (WARN_ON_ONCE((int) size < 0))
1324 /* Make sure end is always >= buf */
1331 const char *old_fmt = fmt;
1332 int read = format_decode(fmt, &spec);
1336 switch (spec.type) {
1337 case FORMAT_TYPE_NONE: {
1340 if (copy > end - str)
1342 memcpy(str, old_fmt, copy);
1348 case FORMAT_TYPE_WIDTH:
1349 spec.field_width = va_arg(args, int);
1352 case FORMAT_TYPE_PRECISION:
1353 spec.precision = va_arg(args, int);
1356 case FORMAT_TYPE_CHAR: {
1359 if (!(spec.flags & LEFT)) {
1360 while (--spec.field_width > 0) {
1367 c = (unsigned char) va_arg(args, int);
1371 while (--spec.field_width > 0) {
1379 case FORMAT_TYPE_STR:
1380 str = string(str, end, va_arg(args, char *), spec);
1383 case FORMAT_TYPE_PTR:
1384 str = pointer(fmt+1, str, end, va_arg(args, void *),
1386 while (isalnum(*fmt))
1390 case FORMAT_TYPE_PERCENT_CHAR:
1396 case FORMAT_TYPE_INVALID:
1402 case FORMAT_TYPE_NRCHARS: {
1403 u8 qualifier = spec.qualifier;
1405 if (qualifier == 'l') {
1406 long *ip = va_arg(args, long *);
1408 } else if (_tolower(qualifier) == 'z') {
1409 size_t *ip = va_arg(args, size_t *);
1412 int *ip = va_arg(args, int *);
1419 switch (spec.type) {
1420 case FORMAT_TYPE_LONG_LONG:
1421 num = va_arg(args, long long);
1423 case FORMAT_TYPE_ULONG:
1424 num = va_arg(args, unsigned long);
1426 case FORMAT_TYPE_LONG:
1427 num = va_arg(args, long);
1429 case FORMAT_TYPE_SIZE_T:
1430 num = va_arg(args, size_t);
1432 case FORMAT_TYPE_PTRDIFF:
1433 num = va_arg(args, ptrdiff_t);
1435 case FORMAT_TYPE_UBYTE:
1436 num = (unsigned char) va_arg(args, int);
1438 case FORMAT_TYPE_BYTE:
1439 num = (signed char) va_arg(args, int);
1441 case FORMAT_TYPE_USHORT:
1442 num = (unsigned short) va_arg(args, int);
1444 case FORMAT_TYPE_SHORT:
1445 num = (short) va_arg(args, int);
1447 case FORMAT_TYPE_INT:
1448 num = (int) va_arg(args, int);
1451 num = va_arg(args, unsigned int);
1454 str = number(str, end, num, spec);
1465 /* the trailing null byte doesn't count towards the total */
1469 EXPORT_SYMBOL(vsnprintf);
1472 * vscnprintf - 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 * @args: Arguments for the format string
1478 * The return value is the number of characters which have been written into
1479 * the @buf not including the trailing '\0'. If @size is == 0 the function
1482 * If you're not already dealing with a va_list consider using scnprintf().
1484 * See the vsnprintf() documentation for format string extensions over C99.
1486 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1490 i = vsnprintf(buf, size, fmt, args);
1492 if (likely(i < size))
1498 EXPORT_SYMBOL(vscnprintf);
1501 * snprintf - Format a string and place it in a buffer
1502 * @buf: The buffer to place the result into
1503 * @size: The size of the buffer, including the trailing null space
1504 * @fmt: The format string to use
1505 * @...: Arguments for the format string
1507 * The return value is the number of characters which would be
1508 * generated for the given input, excluding the trailing null,
1509 * as per ISO C99. If the return is greater than or equal to
1510 * @size, the resulting string is truncated.
1512 * See the vsnprintf() documentation for format string extensions over C99.
1514 int snprintf(char *buf, size_t size, const char *fmt, ...)
1519 va_start(args, fmt);
1520 i = vsnprintf(buf, size, fmt, args);
1525 EXPORT_SYMBOL(snprintf);
1528 * scnprintf - Format a string and place it in a buffer
1529 * @buf: The buffer to place the result into
1530 * @size: The size of the buffer, including the trailing null space
1531 * @fmt: The format string to use
1532 * @...: Arguments for the format string
1534 * The return value is the number of characters written into @buf not including
1535 * the trailing '\0'. If @size is == 0 the function returns 0.
1538 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1543 va_start(args, fmt);
1544 i = vscnprintf(buf, size, fmt, args);
1549 EXPORT_SYMBOL(scnprintf);
1552 * vsprintf - Format a string and place it in a buffer
1553 * @buf: The buffer to place the result into
1554 * @fmt: The format string to use
1555 * @args: Arguments for the format string
1557 * The function returns the number of characters written
1558 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1561 * If you're not already dealing with a va_list consider using sprintf().
1563 * See the vsnprintf() documentation for format string extensions over C99.
1565 int vsprintf(char *buf, const char *fmt, va_list args)
1567 return vsnprintf(buf, INT_MAX, fmt, args);
1569 EXPORT_SYMBOL(vsprintf);
1572 * sprintf - Format a string and place it in a buffer
1573 * @buf: The buffer to place the result into
1574 * @fmt: The format string to use
1575 * @...: Arguments for the format string
1577 * The function returns the number of characters written
1578 * into @buf. Use snprintf() or scnprintf() in order to avoid
1581 * See the vsnprintf() documentation for format string extensions over C99.
1583 int sprintf(char *buf, const char *fmt, ...)
1588 va_start(args, fmt);
1589 i = vsnprintf(buf, INT_MAX, fmt, args);
1594 EXPORT_SYMBOL(sprintf);
1596 #ifdef CONFIG_BINARY_PRINTF
1599 * vbin_printf() - VA arguments to binary data
1600 * bstr_printf() - Binary data to text string
1604 * vbin_printf - Parse a format string and place args' binary value in a buffer
1605 * @bin_buf: The buffer to place args' binary value
1606 * @size: The size of the buffer(by words(32bits), not characters)
1607 * @fmt: The format string to use
1608 * @args: Arguments for the format string
1610 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1613 * The return value is the number of words(32bits) which would be generated for
1617 * If the return value is greater than @size, the resulting bin_buf is NOT
1618 * valid for bstr_printf().
1620 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1622 struct printf_spec spec = {0};
1625 str = (char *)bin_buf;
1626 end = (char *)(bin_buf + size);
1628 #define save_arg(type) \
1630 if (sizeof(type) == 8) { \
1631 unsigned long long value; \
1632 str = PTR_ALIGN(str, sizeof(u32)); \
1633 value = va_arg(args, unsigned long long); \
1634 if (str + sizeof(type) <= end) { \
1635 *(u32 *)str = *(u32 *)&value; \
1636 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1639 unsigned long value; \
1640 str = PTR_ALIGN(str, sizeof(type)); \
1641 value = va_arg(args, int); \
1642 if (str + sizeof(type) <= end) \
1643 *(typeof(type) *)str = (type)value; \
1645 str += sizeof(type); \
1649 int read = format_decode(fmt, &spec);
1653 switch (spec.type) {
1654 case FORMAT_TYPE_NONE:
1655 case FORMAT_TYPE_INVALID:
1656 case FORMAT_TYPE_PERCENT_CHAR:
1659 case FORMAT_TYPE_WIDTH:
1660 case FORMAT_TYPE_PRECISION:
1664 case FORMAT_TYPE_CHAR:
1668 case FORMAT_TYPE_STR: {
1669 const char *save_str = va_arg(args, char *);
1672 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1673 || (unsigned long)save_str < PAGE_SIZE)
1674 save_str = "(null)";
1675 len = strlen(save_str) + 1;
1676 if (str + len < end)
1677 memcpy(str, save_str, len);
1682 case FORMAT_TYPE_PTR:
1684 /* skip all alphanumeric pointer suffixes */
1685 while (isalnum(*fmt))
1689 case FORMAT_TYPE_NRCHARS: {
1690 /* skip %n 's argument */
1691 u8 qualifier = spec.qualifier;
1693 if (qualifier == 'l')
1694 skip_arg = va_arg(args, long *);
1695 else if (_tolower(qualifier) == 'z')
1696 skip_arg = va_arg(args, size_t *);
1698 skip_arg = va_arg(args, int *);
1703 switch (spec.type) {
1705 case FORMAT_TYPE_LONG_LONG:
1706 save_arg(long long);
1708 case FORMAT_TYPE_ULONG:
1709 case FORMAT_TYPE_LONG:
1710 save_arg(unsigned long);
1712 case FORMAT_TYPE_SIZE_T:
1715 case FORMAT_TYPE_PTRDIFF:
1716 save_arg(ptrdiff_t);
1718 case FORMAT_TYPE_UBYTE:
1719 case FORMAT_TYPE_BYTE:
1722 case FORMAT_TYPE_USHORT:
1723 case FORMAT_TYPE_SHORT:
1732 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1735 EXPORT_SYMBOL_GPL(vbin_printf);
1738 * bstr_printf - Format a string from binary arguments and place it in a buffer
1739 * @buf: The buffer to place the result into
1740 * @size: The size of the buffer, including the trailing null space
1741 * @fmt: The format string to use
1742 * @bin_buf: Binary arguments for the format string
1744 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1745 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1746 * a binary buffer that generated by vbin_printf.
1748 * The format follows C99 vsnprintf, but has some extensions:
1749 * see vsnprintf comment for details.
1751 * The return value is the number of characters which would
1752 * be generated for the given input, excluding the trailing
1753 * '\0', as per ISO C99. If you want to have the exact
1754 * number of characters written into @buf as return value
1755 * (not including the trailing '\0'), use vscnprintf(). If the
1756 * return is greater than or equal to @size, the resulting
1757 * string is truncated.
1759 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1761 struct printf_spec spec = {0};
1763 const char *args = (const char *)bin_buf;
1765 if (WARN_ON_ONCE((int) size < 0))
1771 #define get_arg(type) \
1773 typeof(type) value; \
1774 if (sizeof(type) == 8) { \
1775 args = PTR_ALIGN(args, sizeof(u32)); \
1776 *(u32 *)&value = *(u32 *)args; \
1777 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1779 args = PTR_ALIGN(args, sizeof(type)); \
1780 value = *(typeof(type) *)args; \
1782 args += sizeof(type); \
1786 /* Make sure end is always >= buf */
1793 const char *old_fmt = fmt;
1794 int read = format_decode(fmt, &spec);
1798 switch (spec.type) {
1799 case FORMAT_TYPE_NONE: {
1802 if (copy > end - str)
1804 memcpy(str, old_fmt, copy);
1810 case FORMAT_TYPE_WIDTH:
1811 spec.field_width = get_arg(int);
1814 case FORMAT_TYPE_PRECISION:
1815 spec.precision = get_arg(int);
1818 case FORMAT_TYPE_CHAR: {
1821 if (!(spec.flags & LEFT)) {
1822 while (--spec.field_width > 0) {
1828 c = (unsigned char) get_arg(char);
1832 while (--spec.field_width > 0) {
1840 case FORMAT_TYPE_STR: {
1841 const char *str_arg = args;
1842 args += strlen(str_arg) + 1;
1843 str = string(str, end, (char *)str_arg, spec);
1847 case FORMAT_TYPE_PTR:
1848 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1849 while (isalnum(*fmt))
1853 case FORMAT_TYPE_PERCENT_CHAR:
1854 case FORMAT_TYPE_INVALID:
1860 case FORMAT_TYPE_NRCHARS:
1865 unsigned long long num;
1867 switch (spec.type) {
1869 case FORMAT_TYPE_LONG_LONG:
1870 num = get_arg(long long);
1872 case FORMAT_TYPE_ULONG:
1873 case FORMAT_TYPE_LONG:
1874 num = get_arg(unsigned long);
1876 case FORMAT_TYPE_SIZE_T:
1877 num = get_arg(size_t);
1879 case FORMAT_TYPE_PTRDIFF:
1880 num = get_arg(ptrdiff_t);
1882 case FORMAT_TYPE_UBYTE:
1883 num = get_arg(unsigned char);
1885 case FORMAT_TYPE_BYTE:
1886 num = get_arg(signed char);
1888 case FORMAT_TYPE_USHORT:
1889 num = get_arg(unsigned short);
1891 case FORMAT_TYPE_SHORT:
1892 num = get_arg(short);
1894 case FORMAT_TYPE_UINT:
1895 num = get_arg(unsigned int);
1901 str = number(str, end, num, spec);
1903 } /* switch(spec.type) */
1915 /* the trailing null byte doesn't count towards the total */
1918 EXPORT_SYMBOL_GPL(bstr_printf);
1921 * bprintf - Parse a format string and place args' binary value in a buffer
1922 * @bin_buf: The buffer to place args' binary value
1923 * @size: The size of the buffer(by words(32bits), not characters)
1924 * @fmt: The format string to use
1925 * @...: Arguments for the format string
1927 * The function returns the number of words(u32) written
1930 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1935 va_start(args, fmt);
1936 ret = vbin_printf(bin_buf, size, fmt, args);
1941 EXPORT_SYMBOL_GPL(bprintf);
1943 #endif /* CONFIG_BINARY_PRINTF */
1946 * vsscanf - Unformat a buffer into a list of arguments
1947 * @buf: input buffer
1948 * @fmt: format of buffer
1951 int vsscanf(const char *buf, const char *fmt, va_list args)
1953 const char *str = buf;
1962 while (*fmt && *str) {
1963 /* skip any white space in format */
1964 /* white space in format matchs any amount of
1965 * white space, including none, in the input.
1967 if (isspace(*fmt)) {
1968 fmt = skip_spaces(++fmt);
1969 str = skip_spaces(str);
1972 /* anything that is not a conversion must match exactly */
1973 if (*fmt != '%' && *fmt) {
1974 if (*fmt++ != *str++)
1983 /* skip this conversion.
1984 * advance both strings to next white space
1987 while (!isspace(*fmt) && *fmt != '%' && *fmt)
1989 while (!isspace(*str) && *str)
1994 /* get field width */
1997 field_width = skip_atoi(&fmt);
1999 /* get conversion qualifier */
2001 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2002 _tolower(*fmt) == 'z') {
2004 if (unlikely(qualifier == *fmt)) {
2005 if (qualifier == 'h') {
2008 } else if (qualifier == 'l') {
2024 char *s = (char *)va_arg(args, char*);
2025 if (field_width == -1)
2029 } while (--field_width > 0 && *str);
2035 char *s = (char *)va_arg(args, char *);
2036 if (field_width == -1)
2037 field_width = SHRT_MAX;
2038 /* first, skip leading white space in buffer */
2039 str = skip_spaces(str);
2041 /* now copy until next white space */
2042 while (*str && !isspace(*str) && field_width--)
2049 /* return number of characters read so far */
2051 int *i = (int *)va_arg(args, int*);
2069 /* looking for '%' in str */
2074 /* invalid format; stop here */
2078 /* have some sort of integer conversion.
2079 * first, skip white space in buffer.
2081 str = skip_spaces(str);
2084 if (is_sign && digit == '-')
2088 || (base == 16 && !isxdigit(digit))
2089 || (base == 10 && !isdigit(digit))
2090 || (base == 8 && (!isdigit(digit) || digit > '7'))
2091 || (base == 0 && !isdigit(digit)))
2094 switch (qualifier) {
2095 case 'H': /* that's 'hh' in format */
2097 signed char *s = (signed char *)va_arg(args, signed char *);
2098 *s = (signed char)simple_strtol(str, &next, base);
2100 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2101 *s = (unsigned char)simple_strtoul(str, &next, base);
2106 short *s = (short *)va_arg(args, short *);
2107 *s = (short)simple_strtol(str, &next, base);
2109 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2110 *s = (unsigned short)simple_strtoul(str, &next, base);
2115 long *l = (long *)va_arg(args, long *);
2116 *l = simple_strtol(str, &next, base);
2118 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2119 *l = simple_strtoul(str, &next, base);
2124 long long *l = (long long *)va_arg(args, long long *);
2125 *l = simple_strtoll(str, &next, base);
2127 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2128 *l = simple_strtoull(str, &next, base);
2134 size_t *s = (size_t *)va_arg(args, size_t *);
2135 *s = (size_t)simple_strtoul(str, &next, base);
2140 int *i = (int *)va_arg(args, int *);
2141 *i = (int)simple_strtol(str, &next, base);
2143 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2144 *i = (unsigned int)simple_strtoul(str, &next, base);
2156 * Now we've come all the way through so either the input string or the
2157 * format ended. In the former case, there can be a %n at the current
2158 * position in the format that needs to be filled.
2160 if (*fmt == '%' && *(fmt + 1) == 'n') {
2161 int *p = (int *)va_arg(args, int *);
2167 EXPORT_SYMBOL(vsscanf);
2170 * sscanf - Unformat a buffer into a list of arguments
2171 * @buf: input buffer
2172 * @fmt: formatting of buffer
2173 * @...: resulting arguments
2175 int sscanf(const char *buf, const char *fmt, ...)
2180 va_start(args, fmt);
2181 i = vsscanf(buf, fmt, args);
2186 EXPORT_SYMBOL(sscanf);