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/math64.h>
27 #include <linux/uaccess.h>
28 #include <linux/ioport.h>
29 #include <net/addrconf.h>
31 #include <asm/page.h> /* for PAGE_SIZE */
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 * This function is obsolete. Please use kstrtoull instead.
44 unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
46 unsigned long long result;
49 cp = _parse_integer_fixup_radix(cp, &base);
50 rv = _parse_integer(cp, base, &result);
52 cp += (rv & ~KSTRTOX_OVERFLOW);
59 EXPORT_SYMBOL(simple_strtoull);
62 * simple_strtoul - convert a string to an unsigned long
63 * @cp: The start of the string
64 * @endp: A pointer to the end of the parsed string will be placed here
65 * @base: The number base to use
67 * This function is obsolete. Please use kstrtoul instead.
69 unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
71 return simple_strtoull(cp, endp, base);
73 EXPORT_SYMBOL(simple_strtoul);
76 * simple_strtol - convert a string to a signed long
77 * @cp: The start of the string
78 * @endp: A pointer to the end of the parsed string will be placed here
79 * @base: The number base to use
81 * This function is obsolete. Please use kstrtol instead.
83 long simple_strtol(const char *cp, char **endp, unsigned int base)
86 return -simple_strtoul(cp + 1, endp, base);
88 return simple_strtoul(cp, endp, base);
90 EXPORT_SYMBOL(simple_strtol);
93 * simple_strtoll - convert a string to a signed long long
94 * @cp: The start of the string
95 * @endp: A pointer to the end of the parsed string will be placed here
96 * @base: The number base to use
98 * This function is obsolete. Please use kstrtoll instead.
100 long long simple_strtoll(const char *cp, char **endp, unsigned int base)
103 return -simple_strtoull(cp + 1, endp, base);
105 return simple_strtoull(cp, endp, base);
107 EXPORT_SYMBOL(simple_strtoll);
109 static noinline_for_stack
110 int skip_atoi(const char **s)
115 i = i*10 + *((*s)++) - '0';
120 /* Decimal conversion is by far the most typical, and is used
121 * for /proc and /sys data. This directly impacts e.g. top performance
122 * with many processes running. We optimize it for speed
123 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
124 * (with permission from the author, Douglas W. Jones).
127 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
128 /* Formats correctly any integer in [0, 999999999] */
129 static noinline_for_stack
130 char *put_dec_full9(char *buf, unsigned q)
135 * Possible ways to approx. divide by 10
136 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
137 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
138 * (x * 0x6667) >> 18 x < 43699
139 * (x * 0x3334) >> 17 x < 16389
140 * (x * 0x199a) >> 16 x < 16389
141 * (x * 0x0ccd) >> 15 x < 16389
142 * (x * 0x0667) >> 14 x < 2739
143 * (x * 0x0334) >> 13 x < 1029
144 * (x * 0x019a) >> 12 x < 1029
145 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
146 * (x * 0x0067) >> 10 x < 179
147 * (x * 0x0034) >> 9 x < 69 same
148 * (x * 0x001a) >> 8 x < 69 same
149 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
150 * (x * 0x0007) >> 6 x < 19
151 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 1 */
155 q = (r * (uint64_t)0x1999999a) >> 32;
156 *buf++ = (r - 10 * q) + '0'; /* 2 */
157 r = (q * (uint64_t)0x1999999a) >> 32;
158 *buf++ = (q - 10 * r) + '0'; /* 3 */
159 q = (r * (uint64_t)0x1999999a) >> 32;
160 *buf++ = (r - 10 * q) + '0'; /* 4 */
161 r = (q * (uint64_t)0x1999999a) >> 32;
162 *buf++ = (q - 10 * r) + '0'; /* 5 */
163 /* Now value is under 10000, can avoid 64-bit multiply */
164 q = (r * 0x199a) >> 16;
165 *buf++ = (r - 10 * q) + '0'; /* 6 */
166 r = (q * 0xcd) >> 11;
167 *buf++ = (q - 10 * r) + '0'; /* 7 */
168 q = (r * 0xcd) >> 11;
169 *buf++ = (r - 10 * q) + '0'; /* 8 */
170 *buf++ = q + '0'; /* 9 */
175 /* Similar to above but do not pad with zeros.
176 * Code can be easily arranged to print 9 digits too, but our callers
177 * always call put_dec_full9() instead when the number has 9 decimal digits.
179 static noinline_for_stack
180 char *put_dec_trunc8(char *buf, unsigned r)
184 /* Copy of previous function's body with added early returns */
187 r = (r * (uint64_t)0x1999999a) >> 32;
191 q = (r * 0x199a) >> 16; /* r <= 9999 */
192 *buf++ = (r - 10 * q) + '0';
195 r = (q * 0xcd) >> 11; /* q <= 999 */
196 *buf++ = (q - 10 * r) + '0';
199 q = (r * 0xcd) >> 11; /* r <= 99 */
200 *buf++ = (r - 10 * q) + '0';
203 *buf++ = q + '0'; /* q <= 9 */
207 /* There are two algorithms to print larger numbers.
208 * One is generic: divide by 1000000000 and repeatedly print
209 * groups of (up to) 9 digits. It's conceptually simple,
210 * but requires a (unsigned long long) / 1000000000 division.
212 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
213 * manipulates them cleverly and generates groups of 4 decimal digits.
214 * It so happens that it does NOT require long long division.
216 * If long is > 32 bits, division of 64-bit values is relatively easy,
217 * and we will use the first algorithm.
218 * If long long is > 64 bits (strange architecture with VERY large long long),
219 * second algorithm can't be used, and we again use the first one.
221 * Else (if long is 32 bits and long long is 64 bits) we use second one.
224 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
226 /* First algorithm: generic */
229 char *put_dec(char *buf, unsigned long long n)
231 if (n >= 100*1000*1000) {
232 while (n >= 1000*1000*1000)
233 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
234 if (n >= 100*1000*1000)
235 return put_dec_full9(buf, n);
237 return put_dec_trunc8(buf, n);
242 /* Second algorithm: valid only for 64-bit long longs */
244 /* See comment in put_dec_full9 for choice of constants */
245 static noinline_for_stack
246 void put_dec_full4(char *buf, unsigned q)
249 r = (q * 0xccd) >> 15;
250 buf[0] = (q - 10 * r) + '0';
251 q = (r * 0xcd) >> 11;
252 buf[1] = (r - 10 * q) + '0';
253 r = (q * 0xcd) >> 11;
254 buf[2] = (q - 10 * r) + '0';
259 * Call put_dec_full4 on x % 10000, return x / 10000.
260 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
261 * holds for all x < 1,128,869,999. The largest value this
262 * helper will ever be asked to convert is 1,125,520,955.
263 * (d1 in the put_dec code, assuming n is all-ones).
266 unsigned put_dec_helper4(char *buf, unsigned x)
268 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
270 put_dec_full4(buf, x - q * 10000);
274 /* Based on code by Douglas W. Jones found at
275 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
276 * (with permission from the author).
277 * Performs no 64-bit division and hence should be fast on 32-bit machines.
280 char *put_dec(char *buf, unsigned long long n)
282 uint32_t d3, d2, d1, q, h;
284 if (n < 100*1000*1000)
285 return put_dec_trunc8(buf, n);
287 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
290 d3 = (h >> 16); /* implicit "& 0xffff" */
292 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
293 q = put_dec_helper4(buf, q);
295 q += 7671 * d3 + 9496 * d2 + 6 * d1;
296 q = put_dec_helper4(buf+4, q);
298 q += 4749 * d3 + 42 * d2;
299 q = put_dec_helper4(buf+8, q);
304 buf = put_dec_trunc8(buf, q);
305 else while (buf[-1] == '0')
314 * Convert passed number to decimal string.
315 * Returns the length of string. On buffer overflow, returns 0.
317 * If speed is not important, use snprintf(). It's easy to read the code.
319 int num_to_str(char *buf, int size, unsigned long long num)
321 char tmp[sizeof(num) * 3];
324 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
329 len = put_dec(tmp, num) - tmp;
334 for (idx = 0; idx < len; ++idx)
335 buf[idx] = tmp[len - idx - 1];
339 #define ZEROPAD 1 /* pad with zero */
340 #define SIGN 2 /* unsigned/signed long */
341 #define PLUS 4 /* show plus */
342 #define SPACE 8 /* space if plus */
343 #define LEFT 16 /* left justified */
344 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
345 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
348 FORMAT_TYPE_NONE, /* Just a string part */
350 FORMAT_TYPE_PRECISION,
354 FORMAT_TYPE_PERCENT_CHAR,
356 FORMAT_TYPE_LONG_LONG,
371 u8 type; /* format_type enum */
372 u8 flags; /* flags to number() */
373 u8 base; /* number base, 8, 10 or 16 only */
374 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
375 s16 field_width; /* width of output field */
376 s16 precision; /* # of digits/chars */
379 static noinline_for_stack
380 char *number(char *buf, char *end, unsigned long long num,
381 struct printf_spec spec)
383 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
384 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
389 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
391 bool is_zero = num == 0LL;
393 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
394 * produces same digits or (maybe lowercased) letters */
395 locase = (spec.flags & SMALL);
396 if (spec.flags & LEFT)
397 spec.flags &= ~ZEROPAD;
399 if (spec.flags & SIGN) {
400 if ((signed long long)num < 0) {
402 num = -(signed long long)num;
404 } else if (spec.flags & PLUS) {
407 } else if (spec.flags & SPACE) {
414 spec.field_width -= 2;
419 /* generate full string in tmp[], in reverse order */
422 tmp[i++] = digits[num] | locase;
423 /* Generic code, for any base:
425 tmp[i++] = (digits[do_div(num,base)] | locase);
428 else if (spec.base != 10) { /* 8 or 16 */
429 int mask = spec.base - 1;
435 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
438 } else { /* base 10 */
439 i = put_dec(tmp, num) - tmp;
442 /* printing 100 using %2d gives "100", not "00" */
443 if (i > spec.precision)
445 /* leading space padding */
446 spec.field_width -= spec.precision;
447 if (!(spec.flags & (ZEROPAD+LEFT))) {
448 while (--spec.field_width >= 0) {
460 /* "0x" / "0" prefix */
462 if (spec.base == 16 || !is_zero) {
467 if (spec.base == 16) {
469 *buf = ('X' | locase);
473 /* zero or space padding */
474 if (!(spec.flags & LEFT)) {
475 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
476 while (--spec.field_width >= 0) {
482 /* hmm even more zero padding? */
483 while (i <= --spec.precision) {
488 /* actual digits of result */
494 /* trailing space padding */
495 while (--spec.field_width >= 0) {
504 static noinline_for_stack
505 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
509 if ((unsigned long)s < PAGE_SIZE)
512 len = strnlen(s, spec.precision);
514 if (!(spec.flags & LEFT)) {
515 while (len < spec.field_width--) {
521 for (i = 0; i < len; ++i) {
526 while (len < spec.field_width--) {
535 static noinline_for_stack
536 char *symbol_string(char *buf, char *end, void *ptr,
537 struct printf_spec spec, const char *fmt)
540 #ifdef CONFIG_KALLSYMS
541 char sym[KSYM_SYMBOL_LEN];
545 ptr = __builtin_extract_return_addr(ptr);
546 value = (unsigned long)ptr;
548 #ifdef CONFIG_KALLSYMS
550 sprint_backtrace(sym, value);
551 else if (*fmt != 'f' && *fmt != 's')
552 sprint_symbol(sym, value);
554 sprint_symbol_no_offset(sym, value);
556 return string(buf, end, sym, spec);
558 spec.field_width = 2 * sizeof(void *);
559 spec.flags |= SPECIAL | SMALL | ZEROPAD;
562 return number(buf, end, value, spec);
566 static noinline_for_stack
567 char *resource_string(char *buf, char *end, struct resource *res,
568 struct printf_spec spec, const char *fmt)
570 #ifndef IO_RSRC_PRINTK_SIZE
571 #define IO_RSRC_PRINTK_SIZE 6
574 #ifndef MEM_RSRC_PRINTK_SIZE
575 #define MEM_RSRC_PRINTK_SIZE 10
577 static const struct printf_spec io_spec = {
579 .field_width = IO_RSRC_PRINTK_SIZE,
581 .flags = SPECIAL | SMALL | ZEROPAD,
583 static const struct printf_spec mem_spec = {
585 .field_width = MEM_RSRC_PRINTK_SIZE,
587 .flags = SPECIAL | SMALL | ZEROPAD,
589 static const struct printf_spec bus_spec = {
593 .flags = SMALL | ZEROPAD,
595 static const struct printf_spec dec_spec = {
600 static const struct printf_spec str_spec = {
605 static const struct printf_spec flag_spec = {
608 .flags = SPECIAL | SMALL,
611 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
612 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
613 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
614 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
615 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
616 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
617 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
618 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
620 char *p = sym, *pend = sym + sizeof(sym);
621 int decode = (fmt[0] == 'R') ? 1 : 0;
622 const struct printf_spec *specp;
625 if (res->flags & IORESOURCE_IO) {
626 p = string(p, pend, "io ", str_spec);
628 } else if (res->flags & IORESOURCE_MEM) {
629 p = string(p, pend, "mem ", str_spec);
631 } else if (res->flags & IORESOURCE_IRQ) {
632 p = string(p, pend, "irq ", str_spec);
634 } else if (res->flags & IORESOURCE_DMA) {
635 p = string(p, pend, "dma ", str_spec);
637 } else if (res->flags & IORESOURCE_BUS) {
638 p = string(p, pend, "bus ", str_spec);
641 p = string(p, pend, "??? ", str_spec);
645 p = number(p, pend, res->start, *specp);
646 if (res->start != res->end) {
648 p = number(p, pend, res->end, *specp);
651 if (res->flags & IORESOURCE_MEM_64)
652 p = string(p, pend, " 64bit", str_spec);
653 if (res->flags & IORESOURCE_PREFETCH)
654 p = string(p, pend, " pref", str_spec);
655 if (res->flags & IORESOURCE_WINDOW)
656 p = string(p, pend, " window", str_spec);
657 if (res->flags & IORESOURCE_DISABLED)
658 p = string(p, pend, " disabled", str_spec);
660 p = string(p, pend, " flags ", str_spec);
661 p = number(p, pend, res->flags, flag_spec);
666 return string(buf, end, sym, spec);
669 static noinline_for_stack
670 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
673 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
674 negative value, fallback to the default */
677 if (spec.field_width == 0)
678 /* nothing to print */
681 if (ZERO_OR_NULL_PTR(addr))
683 return string(buf, end, NULL, spec);
700 if (spec.field_width > 0)
701 len = min_t(int, spec.field_width, 64);
703 for (i = 0; i < len && buf < end - 1; i++) {
704 buf = hex_byte_pack(buf, addr[i]);
706 if (buf < end && separator && i != len - 1)
713 static noinline_for_stack
714 char *mac_address_string(char *buf, char *end, u8 *addr,
715 struct printf_spec spec, const char *fmt)
717 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
721 bool reversed = false;
737 for (i = 0; i < 6; i++) {
739 p = hex_byte_pack(p, addr[5 - i]);
741 p = hex_byte_pack(p, addr[i]);
743 if (fmt[0] == 'M' && i != 5)
748 return string(buf, end, mac_addr, spec);
751 static noinline_for_stack
752 char *ip4_string(char *p, const u8 *addr, const char *fmt)
755 bool leading_zeros = (fmt[0] == 'i');
780 for (i = 0; i < 4; i++) {
781 char temp[3]; /* hold each IP quad in reverse order */
782 int digits = put_dec_trunc8(temp, addr[index]) - temp;
789 /* reverse the digits in the quad */
801 static noinline_for_stack
802 char *ip6_compressed_string(char *p, const char *addr)
805 unsigned char zerolength[8];
810 bool needcolon = false;
814 memcpy(&in6, addr, sizeof(struct in6_addr));
816 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
818 memset(zerolength, 0, sizeof(zerolength));
825 /* find position of longest 0 run */
826 for (i = 0; i < range; i++) {
827 for (j = i; j < range; j++) {
828 if (in6.s6_addr16[j] != 0)
833 for (i = 0; i < range; i++) {
834 if (zerolength[i] > longest) {
835 longest = zerolength[i];
839 if (longest == 1) /* don't compress a single 0 */
843 for (i = 0; i < range; i++) {
845 if (needcolon || i == 0)
856 /* hex u16 without leading 0s */
857 word = ntohs(in6.s6_addr16[i]);
862 p = hex_byte_pack(p, hi);
864 *p++ = hex_asc_lo(hi);
865 p = hex_byte_pack(p, lo);
868 p = hex_byte_pack(p, lo);
870 *p++ = hex_asc_lo(lo);
877 p = ip4_string(p, &in6.s6_addr[12], "I4");
884 static noinline_for_stack
885 char *ip6_string(char *p, const char *addr, const char *fmt)
889 for (i = 0; i < 8; i++) {
890 p = hex_byte_pack(p, *addr++);
891 p = hex_byte_pack(p, *addr++);
892 if (fmt[0] == 'I' && i != 7)
900 static noinline_for_stack
901 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
902 struct printf_spec spec, const char *fmt)
904 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
906 if (fmt[0] == 'I' && fmt[2] == 'c')
907 ip6_compressed_string(ip6_addr, addr);
909 ip6_string(ip6_addr, addr, fmt);
911 return string(buf, end, ip6_addr, spec);
914 static noinline_for_stack
915 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
916 struct printf_spec spec, const char *fmt)
918 char ip4_addr[sizeof("255.255.255.255")];
920 ip4_string(ip4_addr, addr, fmt);
922 return string(buf, end, ip4_addr, spec);
925 static noinline_for_stack
926 char *uuid_string(char *buf, char *end, const u8 *addr,
927 struct printf_spec spec, const char *fmt)
929 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
932 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
933 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
934 const u8 *index = be;
939 uc = true; /* fall-through */
948 for (i = 0; i < 16; i++) {
949 p = hex_byte_pack(p, addr[index[i]]);
969 return string(buf, end, uuid, spec);
973 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
974 struct printf_spec spec)
976 spec.flags |= SPECIAL | SMALL | ZEROPAD;
977 if (spec.field_width == -1)
978 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
981 return number(buf, end, *(const netdev_features_t *)addr, spec);
984 int kptr_restrict __read_mostly;
987 * Show a '%p' thing. A kernel extension is that the '%p' is followed
988 * by an extra set of alphanumeric characters that are extended format
991 * Right now we handle:
993 * - 'F' For symbolic function descriptor pointers with offset
994 * - 'f' For simple symbolic function names without offset
995 * - 'S' For symbolic direct pointers with offset
996 * - 's' For symbolic direct pointers without offset
997 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
998 * - 'B' For backtraced symbolic direct pointers with offset
999 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1000 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1001 * - 'M' For a 6-byte MAC address, it prints the address in the
1002 * usual colon-separated hex notation
1003 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1004 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1005 * with a dash-separated hex notation
1006 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1007 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1008 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1009 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1010 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1011 * IPv6 omits the colons (01020304...0f)
1012 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1013 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
1014 * - 'I6c' for IPv6 addresses printed as specified by
1015 * http://tools.ietf.org/html/rfc5952
1016 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1017 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1018 * Options for %pU are:
1019 * b big endian lower case hex (default)
1020 * B big endian UPPER case hex
1021 * l little endian lower case hex
1022 * L little endian UPPER case hex
1023 * big endian output byte order is:
1024 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1025 * little endian output byte order is:
1026 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1027 * - 'V' For a struct va_format which contains a format string * and va_list *,
1028 * call vsnprintf(->format, *->va_list).
1029 * Implements a "recursive vsnprintf".
1030 * Do not use this feature without some mechanism to verify the
1031 * correctness of the format string and va_list arguments.
1032 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1033 * - 'NF' For a netdev_features_t
1034 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1035 * a certain separator (' ' by default):
1039 * The maximum supported length is 64 bytes of the input. Consider
1040 * to use print_hex_dump() for the larger input.
1041 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
1043 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1044 * function pointers are really function descriptors, which contain a
1045 * pointer to the real address.
1047 static noinline_for_stack
1048 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1049 struct printf_spec spec)
1051 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1053 if (!ptr && *fmt != 'K') {
1055 * Print (null) with the same width as a pointer so it makes
1056 * tabular output look nice.
1058 if (spec.field_width == -1)
1059 spec.field_width = default_width;
1060 return string(buf, end, "(null)", spec);
1066 ptr = dereference_function_descriptor(ptr);
1071 return symbol_string(buf, end, ptr, spec, fmt);
1074 return resource_string(buf, end, ptr, spec, fmt);
1076 return hex_string(buf, end, ptr, spec, fmt);
1077 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1078 case 'm': /* Contiguous: 000102030405 */
1080 /* [mM]R (Reverse order; Bluetooth) */
1081 return mac_address_string(buf, end, ptr, spec, fmt);
1082 case 'I': /* Formatted IP supported
1084 * 6: 0001:0203:...:0708
1085 * 6c: 1::708 or 1::1.2.3.4
1087 case 'i': /* Contiguous:
1088 * 4: 001.002.003.004
1093 return ip6_addr_string(buf, end, ptr, spec, fmt);
1095 return ip4_addr_string(buf, end, ptr, spec, fmt);
1099 return uuid_string(buf, end, ptr, spec, fmt);
1104 va_copy(va, *((struct va_format *)ptr)->va);
1105 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1106 ((struct va_format *)ptr)->fmt, va);
1112 * %pK cannot be used in IRQ context because its test
1113 * for CAP_SYSLOG would be meaningless.
1115 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1117 if (spec.field_width == -1)
1118 spec.field_width = default_width;
1119 return string(buf, end, "pK-error", spec);
1121 if (!((kptr_restrict == 0) ||
1122 (kptr_restrict == 1 &&
1123 has_capability_noaudit(current, CAP_SYSLOG))))
1129 return netdev_feature_string(buf, end, ptr, spec);
1133 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1134 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1136 return number(buf, end,
1137 (unsigned long long) *((phys_addr_t *)ptr), spec);
1139 spec.flags |= SMALL;
1140 if (spec.field_width == -1) {
1141 spec.field_width = default_width;
1142 spec.flags |= ZEROPAD;
1146 return number(buf, end, (unsigned long) ptr, spec);
1150 * Helper function to decode printf style format.
1151 * Each call decode a token from the format and return the
1152 * number of characters read (or likely the delta where it wants
1153 * to go on the next call).
1154 * The decoded token is returned through the parameters
1156 * 'h', 'l', or 'L' for integer fields
1157 * 'z' support added 23/7/1999 S.H.
1158 * 'z' changed to 'Z' --davidm 1/25/99
1159 * 't' added for ptrdiff_t
1161 * @fmt: the format string
1162 * @type of the token returned
1163 * @flags: various flags such as +, -, # tokens..
1164 * @field_width: overwritten width
1165 * @base: base of the number (octal, hex, ...)
1166 * @precision: precision of a number
1167 * @qualifier: qualifier of a number (long, size_t, ...)
1169 static noinline_for_stack
1170 int format_decode(const char *fmt, struct printf_spec *spec)
1172 const char *start = fmt;
1174 /* we finished early by reading the field width */
1175 if (spec->type == FORMAT_TYPE_WIDTH) {
1176 if (spec->field_width < 0) {
1177 spec->field_width = -spec->field_width;
1178 spec->flags |= LEFT;
1180 spec->type = FORMAT_TYPE_NONE;
1184 /* we finished early by reading the precision */
1185 if (spec->type == FORMAT_TYPE_PRECISION) {
1186 if (spec->precision < 0)
1187 spec->precision = 0;
1189 spec->type = FORMAT_TYPE_NONE;
1194 spec->type = FORMAT_TYPE_NONE;
1196 for (; *fmt ; ++fmt) {
1201 /* Return the current non-format string */
1202 if (fmt != start || !*fmt)
1208 while (1) { /* this also skips first '%' */
1214 case '-': spec->flags |= LEFT; break;
1215 case '+': spec->flags |= PLUS; break;
1216 case ' ': spec->flags |= SPACE; break;
1217 case '#': spec->flags |= SPECIAL; break;
1218 case '0': spec->flags |= ZEROPAD; break;
1219 default: found = false;
1226 /* get field width */
1227 spec->field_width = -1;
1230 spec->field_width = skip_atoi(&fmt);
1231 else if (*fmt == '*') {
1232 /* it's the next argument */
1233 spec->type = FORMAT_TYPE_WIDTH;
1234 return ++fmt - start;
1238 /* get the precision */
1239 spec->precision = -1;
1242 if (isdigit(*fmt)) {
1243 spec->precision = skip_atoi(&fmt);
1244 if (spec->precision < 0)
1245 spec->precision = 0;
1246 } else if (*fmt == '*') {
1247 /* it's the next argument */
1248 spec->type = FORMAT_TYPE_PRECISION;
1249 return ++fmt - start;
1254 /* get the conversion qualifier */
1255 spec->qualifier = -1;
1256 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1257 _tolower(*fmt) == 'z' || *fmt == 't') {
1258 spec->qualifier = *fmt++;
1259 if (unlikely(spec->qualifier == *fmt)) {
1260 if (spec->qualifier == 'l') {
1261 spec->qualifier = 'L';
1263 } else if (spec->qualifier == 'h') {
1264 spec->qualifier = 'H';
1274 spec->type = FORMAT_TYPE_CHAR;
1275 return ++fmt - start;
1278 spec->type = FORMAT_TYPE_STR;
1279 return ++fmt - start;
1282 spec->type = FORMAT_TYPE_PTR;
1287 spec->type = FORMAT_TYPE_NRCHARS;
1288 return ++fmt - start;
1291 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1292 return ++fmt - start;
1294 /* integer number formats - set up the flags and "break" */
1300 spec->flags |= SMALL;
1308 spec->flags |= SIGN;
1313 spec->type = FORMAT_TYPE_INVALID;
1317 if (spec->qualifier == 'L')
1318 spec->type = FORMAT_TYPE_LONG_LONG;
1319 else if (spec->qualifier == 'l') {
1320 if (spec->flags & SIGN)
1321 spec->type = FORMAT_TYPE_LONG;
1323 spec->type = FORMAT_TYPE_ULONG;
1324 } else if (_tolower(spec->qualifier) == 'z') {
1325 spec->type = FORMAT_TYPE_SIZE_T;
1326 } else if (spec->qualifier == 't') {
1327 spec->type = FORMAT_TYPE_PTRDIFF;
1328 } else if (spec->qualifier == 'H') {
1329 if (spec->flags & SIGN)
1330 spec->type = FORMAT_TYPE_BYTE;
1332 spec->type = FORMAT_TYPE_UBYTE;
1333 } else if (spec->qualifier == 'h') {
1334 if (spec->flags & SIGN)
1335 spec->type = FORMAT_TYPE_SHORT;
1337 spec->type = FORMAT_TYPE_USHORT;
1339 if (spec->flags & SIGN)
1340 spec->type = FORMAT_TYPE_INT;
1342 spec->type = FORMAT_TYPE_UINT;
1345 return ++fmt - start;
1349 * vsnprintf - Format a string and place it in a buffer
1350 * @buf: The buffer to place the result into
1351 * @size: The size of the buffer, including the trailing null space
1352 * @fmt: The format string to use
1353 * @args: Arguments for the format string
1355 * This function follows C99 vsnprintf, but has some extensions:
1356 * %pS output the name of a text symbol with offset
1357 * %ps output the name of a text symbol without offset
1358 * %pF output the name of a function pointer with its offset
1359 * %pf output the name of a function pointer without its offset
1360 * %pB output the name of a backtrace symbol with its offset
1361 * %pR output the address range in a struct resource with decoded flags
1362 * %pr output the address range in a struct resource with raw flags
1363 * %pM output a 6-byte MAC address with colons
1364 * %pMR output a 6-byte MAC address with colons in reversed order
1365 * %pMF output a 6-byte MAC address with dashes
1366 * %pm output a 6-byte MAC address without colons
1367 * %pmR output a 6-byte MAC address without colons in reversed order
1368 * %pI4 print an IPv4 address without leading zeros
1369 * %pi4 print an IPv4 address with leading zeros
1370 * %pI6 print an IPv6 address with colons
1371 * %pi6 print an IPv6 address without colons
1372 * %pI6c print an IPv6 address as specified by RFC 5952
1373 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1375 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1376 * bytes of the input)
1379 * ** Please update Documentation/printk-formats.txt when making changes **
1381 * The return value is the number of characters which would
1382 * be generated for the given input, excluding the trailing
1383 * '\0', as per ISO C99. If you want to have the exact
1384 * number of characters written into @buf as return value
1385 * (not including the trailing '\0'), use vscnprintf(). If the
1386 * return is greater than or equal to @size, the resulting
1387 * string is truncated.
1389 * If you're not already dealing with a va_list consider using snprintf().
1391 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1393 unsigned long long num;
1395 struct printf_spec spec = {0};
1397 /* Reject out-of-range values early. Large positive sizes are
1398 used for unknown buffer sizes. */
1399 if (WARN_ON_ONCE((int) size < 0))
1405 /* Make sure end is always >= buf */
1412 const char *old_fmt = fmt;
1413 int read = format_decode(fmt, &spec);
1417 switch (spec.type) {
1418 case FORMAT_TYPE_NONE: {
1421 if (copy > end - str)
1423 memcpy(str, old_fmt, copy);
1429 case FORMAT_TYPE_WIDTH:
1430 spec.field_width = va_arg(args, int);
1433 case FORMAT_TYPE_PRECISION:
1434 spec.precision = va_arg(args, int);
1437 case FORMAT_TYPE_CHAR: {
1440 if (!(spec.flags & LEFT)) {
1441 while (--spec.field_width > 0) {
1448 c = (unsigned char) va_arg(args, int);
1452 while (--spec.field_width > 0) {
1460 case FORMAT_TYPE_STR:
1461 str = string(str, end, va_arg(args, char *), spec);
1464 case FORMAT_TYPE_PTR:
1465 str = pointer(fmt+1, str, end, va_arg(args, void *),
1467 while (isalnum(*fmt))
1471 case FORMAT_TYPE_PERCENT_CHAR:
1477 case FORMAT_TYPE_INVALID:
1483 case FORMAT_TYPE_NRCHARS: {
1484 u8 qualifier = spec.qualifier;
1486 if (qualifier == 'l') {
1487 long *ip = va_arg(args, long *);
1489 } else if (_tolower(qualifier) == 'z') {
1490 size_t *ip = va_arg(args, size_t *);
1493 int *ip = va_arg(args, int *);
1500 switch (spec.type) {
1501 case FORMAT_TYPE_LONG_LONG:
1502 num = va_arg(args, long long);
1504 case FORMAT_TYPE_ULONG:
1505 num = va_arg(args, unsigned long);
1507 case FORMAT_TYPE_LONG:
1508 num = va_arg(args, long);
1510 case FORMAT_TYPE_SIZE_T:
1511 if (spec.flags & SIGN)
1512 num = va_arg(args, ssize_t);
1514 num = va_arg(args, size_t);
1516 case FORMAT_TYPE_PTRDIFF:
1517 num = va_arg(args, ptrdiff_t);
1519 case FORMAT_TYPE_UBYTE:
1520 num = (unsigned char) va_arg(args, int);
1522 case FORMAT_TYPE_BYTE:
1523 num = (signed char) va_arg(args, int);
1525 case FORMAT_TYPE_USHORT:
1526 num = (unsigned short) va_arg(args, int);
1528 case FORMAT_TYPE_SHORT:
1529 num = (short) va_arg(args, int);
1531 case FORMAT_TYPE_INT:
1532 num = (int) va_arg(args, int);
1535 num = va_arg(args, unsigned int);
1538 str = number(str, end, num, spec);
1549 /* the trailing null byte doesn't count towards the total */
1553 EXPORT_SYMBOL(vsnprintf);
1556 * vscnprintf - Format a string and place it in a buffer
1557 * @buf: The buffer to place the result into
1558 * @size: The size of the buffer, including the trailing null space
1559 * @fmt: The format string to use
1560 * @args: Arguments for the format string
1562 * The return value is the number of characters which have been written into
1563 * the @buf not including the trailing '\0'. If @size is == 0 the function
1566 * If you're not already dealing with a va_list consider using scnprintf().
1568 * See the vsnprintf() documentation for format string extensions over C99.
1570 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1574 i = vsnprintf(buf, size, fmt, args);
1576 if (likely(i < size))
1582 EXPORT_SYMBOL(vscnprintf);
1585 * snprintf - Format a string and place it in a buffer
1586 * @buf: The buffer to place the result into
1587 * @size: The size of the buffer, including the trailing null space
1588 * @fmt: The format string to use
1589 * @...: Arguments for the format string
1591 * The return value is the number of characters which would be
1592 * generated for the given input, excluding the trailing null,
1593 * as per ISO C99. If the return is greater than or equal to
1594 * @size, the resulting string is truncated.
1596 * See the vsnprintf() documentation for format string extensions over C99.
1598 int snprintf(char *buf, size_t size, const char *fmt, ...)
1603 va_start(args, fmt);
1604 i = vsnprintf(buf, size, fmt, args);
1609 EXPORT_SYMBOL(snprintf);
1612 * scnprintf - Format a string and place it in a buffer
1613 * @buf: The buffer to place the result into
1614 * @size: The size of the buffer, including the trailing null space
1615 * @fmt: The format string to use
1616 * @...: Arguments for the format string
1618 * The return value is the number of characters written into @buf not including
1619 * the trailing '\0'. If @size is == 0 the function returns 0.
1622 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1627 va_start(args, fmt);
1628 i = vscnprintf(buf, size, fmt, args);
1633 EXPORT_SYMBOL(scnprintf);
1636 * vsprintf - Format a string and place it in a buffer
1637 * @buf: The buffer to place the result into
1638 * @fmt: The format string to use
1639 * @args: Arguments for the format string
1641 * The function returns the number of characters written
1642 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1645 * If you're not already dealing with a va_list consider using sprintf().
1647 * See the vsnprintf() documentation for format string extensions over C99.
1649 int vsprintf(char *buf, const char *fmt, va_list args)
1651 return vsnprintf(buf, INT_MAX, fmt, args);
1653 EXPORT_SYMBOL(vsprintf);
1656 * sprintf - Format a string and place it in a buffer
1657 * @buf: The buffer to place the result into
1658 * @fmt: The format string to use
1659 * @...: Arguments for the format string
1661 * The function returns the number of characters written
1662 * into @buf. Use snprintf() or scnprintf() in order to avoid
1665 * See the vsnprintf() documentation for format string extensions over C99.
1667 int sprintf(char *buf, const char *fmt, ...)
1672 va_start(args, fmt);
1673 i = vsnprintf(buf, INT_MAX, fmt, args);
1678 EXPORT_SYMBOL(sprintf);
1680 #ifdef CONFIG_BINARY_PRINTF
1683 * vbin_printf() - VA arguments to binary data
1684 * bstr_printf() - Binary data to text string
1688 * vbin_printf - Parse a format string and place args' binary value in a buffer
1689 * @bin_buf: The buffer to place args' binary value
1690 * @size: The size of the buffer(by words(32bits), not characters)
1691 * @fmt: The format string to use
1692 * @args: Arguments for the format string
1694 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1697 * The return value is the number of words(32bits) which would be generated for
1701 * If the return value is greater than @size, the resulting bin_buf is NOT
1702 * valid for bstr_printf().
1704 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1706 struct printf_spec spec = {0};
1709 str = (char *)bin_buf;
1710 end = (char *)(bin_buf + size);
1712 #define save_arg(type) \
1714 if (sizeof(type) == 8) { \
1715 unsigned long long value; \
1716 str = PTR_ALIGN(str, sizeof(u32)); \
1717 value = va_arg(args, unsigned long long); \
1718 if (str + sizeof(type) <= end) { \
1719 *(u32 *)str = *(u32 *)&value; \
1720 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1723 unsigned long value; \
1724 str = PTR_ALIGN(str, sizeof(type)); \
1725 value = va_arg(args, int); \
1726 if (str + sizeof(type) <= end) \
1727 *(typeof(type) *)str = (type)value; \
1729 str += sizeof(type); \
1733 int read = format_decode(fmt, &spec);
1737 switch (spec.type) {
1738 case FORMAT_TYPE_NONE:
1739 case FORMAT_TYPE_INVALID:
1740 case FORMAT_TYPE_PERCENT_CHAR:
1743 case FORMAT_TYPE_WIDTH:
1744 case FORMAT_TYPE_PRECISION:
1748 case FORMAT_TYPE_CHAR:
1752 case FORMAT_TYPE_STR: {
1753 const char *save_str = va_arg(args, char *);
1756 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1757 || (unsigned long)save_str < PAGE_SIZE)
1758 save_str = "(null)";
1759 len = strlen(save_str) + 1;
1760 if (str + len < end)
1761 memcpy(str, save_str, len);
1766 case FORMAT_TYPE_PTR:
1768 /* skip all alphanumeric pointer suffixes */
1769 while (isalnum(*fmt))
1773 case FORMAT_TYPE_NRCHARS: {
1774 /* skip %n 's argument */
1775 u8 qualifier = spec.qualifier;
1777 if (qualifier == 'l')
1778 skip_arg = va_arg(args, long *);
1779 else if (_tolower(qualifier) == 'z')
1780 skip_arg = va_arg(args, size_t *);
1782 skip_arg = va_arg(args, int *);
1787 switch (spec.type) {
1789 case FORMAT_TYPE_LONG_LONG:
1790 save_arg(long long);
1792 case FORMAT_TYPE_ULONG:
1793 case FORMAT_TYPE_LONG:
1794 save_arg(unsigned long);
1796 case FORMAT_TYPE_SIZE_T:
1799 case FORMAT_TYPE_PTRDIFF:
1800 save_arg(ptrdiff_t);
1802 case FORMAT_TYPE_UBYTE:
1803 case FORMAT_TYPE_BYTE:
1806 case FORMAT_TYPE_USHORT:
1807 case FORMAT_TYPE_SHORT:
1816 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1819 EXPORT_SYMBOL_GPL(vbin_printf);
1822 * bstr_printf - Format a string from binary arguments and place it in a buffer
1823 * @buf: The buffer to place the result into
1824 * @size: The size of the buffer, including the trailing null space
1825 * @fmt: The format string to use
1826 * @bin_buf: Binary arguments for the format string
1828 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1829 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1830 * a binary buffer that generated by vbin_printf.
1832 * The format follows C99 vsnprintf, but has some extensions:
1833 * see vsnprintf comment for details.
1835 * The return value is the number of characters which would
1836 * be generated for the given input, excluding the trailing
1837 * '\0', as per ISO C99. If you want to have the exact
1838 * number of characters written into @buf as return value
1839 * (not including the trailing '\0'), use vscnprintf(). If the
1840 * return is greater than or equal to @size, the resulting
1841 * string is truncated.
1843 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1845 struct printf_spec spec = {0};
1847 const char *args = (const char *)bin_buf;
1849 if (WARN_ON_ONCE((int) size < 0))
1855 #define get_arg(type) \
1857 typeof(type) value; \
1858 if (sizeof(type) == 8) { \
1859 args = PTR_ALIGN(args, sizeof(u32)); \
1860 *(u32 *)&value = *(u32 *)args; \
1861 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1863 args = PTR_ALIGN(args, sizeof(type)); \
1864 value = *(typeof(type) *)args; \
1866 args += sizeof(type); \
1870 /* Make sure end is always >= buf */
1877 const char *old_fmt = fmt;
1878 int read = format_decode(fmt, &spec);
1882 switch (spec.type) {
1883 case FORMAT_TYPE_NONE: {
1886 if (copy > end - str)
1888 memcpy(str, old_fmt, copy);
1894 case FORMAT_TYPE_WIDTH:
1895 spec.field_width = get_arg(int);
1898 case FORMAT_TYPE_PRECISION:
1899 spec.precision = get_arg(int);
1902 case FORMAT_TYPE_CHAR: {
1905 if (!(spec.flags & LEFT)) {
1906 while (--spec.field_width > 0) {
1912 c = (unsigned char) get_arg(char);
1916 while (--spec.field_width > 0) {
1924 case FORMAT_TYPE_STR: {
1925 const char *str_arg = args;
1926 args += strlen(str_arg) + 1;
1927 str = string(str, end, (char *)str_arg, spec);
1931 case FORMAT_TYPE_PTR:
1932 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1933 while (isalnum(*fmt))
1937 case FORMAT_TYPE_PERCENT_CHAR:
1938 case FORMAT_TYPE_INVALID:
1944 case FORMAT_TYPE_NRCHARS:
1949 unsigned long long num;
1951 switch (spec.type) {
1953 case FORMAT_TYPE_LONG_LONG:
1954 num = get_arg(long long);
1956 case FORMAT_TYPE_ULONG:
1957 case FORMAT_TYPE_LONG:
1958 num = get_arg(unsigned long);
1960 case FORMAT_TYPE_SIZE_T:
1961 num = get_arg(size_t);
1963 case FORMAT_TYPE_PTRDIFF:
1964 num = get_arg(ptrdiff_t);
1966 case FORMAT_TYPE_UBYTE:
1967 num = get_arg(unsigned char);
1969 case FORMAT_TYPE_BYTE:
1970 num = get_arg(signed char);
1972 case FORMAT_TYPE_USHORT:
1973 num = get_arg(unsigned short);
1975 case FORMAT_TYPE_SHORT:
1976 num = get_arg(short);
1978 case FORMAT_TYPE_UINT:
1979 num = get_arg(unsigned int);
1985 str = number(str, end, num, spec);
1987 } /* switch(spec.type) */
1999 /* the trailing null byte doesn't count towards the total */
2002 EXPORT_SYMBOL_GPL(bstr_printf);
2005 * bprintf - Parse a format string and place args' binary value in a buffer
2006 * @bin_buf: The buffer to place args' binary value
2007 * @size: The size of the buffer(by words(32bits), not characters)
2008 * @fmt: The format string to use
2009 * @...: Arguments for the format string
2011 * The function returns the number of words(u32) written
2014 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2019 va_start(args, fmt);
2020 ret = vbin_printf(bin_buf, size, fmt, args);
2025 EXPORT_SYMBOL_GPL(bprintf);
2027 #endif /* CONFIG_BINARY_PRINTF */
2030 * vsscanf - Unformat a buffer into a list of arguments
2031 * @buf: input buffer
2032 * @fmt: format of buffer
2035 int vsscanf(const char *buf, const char *fmt, va_list args)
2037 const char *str = buf;
2045 unsigned long long u;
2051 /* skip any white space in format */
2052 /* white space in format matchs any amount of
2053 * white space, including none, in the input.
2055 if (isspace(*fmt)) {
2056 fmt = skip_spaces(++fmt);
2057 str = skip_spaces(str);
2060 /* anything that is not a conversion must match exactly */
2061 if (*fmt != '%' && *fmt) {
2062 if (*fmt++ != *str++)
2071 /* skip this conversion.
2072 * advance both strings to next white space
2077 while (!isspace(*fmt) && *fmt != '%' && *fmt)
2079 while (!isspace(*str) && *str)
2084 /* get field width */
2086 if (isdigit(*fmt)) {
2087 field_width = skip_atoi(&fmt);
2088 if (field_width <= 0)
2092 /* get conversion qualifier */
2094 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2095 _tolower(*fmt) == 'z') {
2097 if (unlikely(qualifier == *fmt)) {
2098 if (qualifier == 'h') {
2101 } else if (qualifier == 'l') {
2112 /* return number of characters read so far */
2113 *va_arg(args, int *) = str - buf;
2127 char *s = (char *)va_arg(args, char*);
2128 if (field_width == -1)
2132 } while (--field_width > 0 && *str);
2138 char *s = (char *)va_arg(args, char *);
2139 if (field_width == -1)
2140 field_width = SHRT_MAX;
2141 /* first, skip leading white space in buffer */
2142 str = skip_spaces(str);
2144 /* now copy until next white space */
2145 while (*str && !isspace(*str) && field_width--)
2165 /* looking for '%' in str */
2170 /* invalid format; stop here */
2174 /* have some sort of integer conversion.
2175 * first, skip white space in buffer.
2177 str = skip_spaces(str);
2180 if (is_sign && digit == '-')
2184 || (base == 16 && !isxdigit(digit))
2185 || (base == 10 && !isdigit(digit))
2186 || (base == 8 && (!isdigit(digit) || digit > '7'))
2187 || (base == 0 && !isdigit(digit)))
2191 val.s = qualifier != 'L' ?
2192 simple_strtol(str, &next, base) :
2193 simple_strtoll(str, &next, base);
2195 val.u = qualifier != 'L' ?
2196 simple_strtoul(str, &next, base) :
2197 simple_strtoull(str, &next, base);
2199 if (field_width > 0 && next - str > field_width) {
2201 _parse_integer_fixup_radix(str, &base);
2202 while (next - str > field_width) {
2204 val.s = div_s64(val.s, base);
2206 val.u = div_u64(val.u, base);
2211 switch (qualifier) {
2212 case 'H': /* that's 'hh' in format */
2214 *va_arg(args, signed char *) = val.s;
2216 *va_arg(args, unsigned char *) = val.u;
2220 *va_arg(args, short *) = val.s;
2222 *va_arg(args, unsigned short *) = val.u;
2226 *va_arg(args, long *) = val.s;
2228 *va_arg(args, unsigned long *) = val.u;
2232 *va_arg(args, long long *) = val.s;
2234 *va_arg(args, unsigned long long *) = val.u;
2238 *va_arg(args, size_t *) = val.u;
2242 *va_arg(args, int *) = val.s;
2244 *va_arg(args, unsigned int *) = val.u;
2256 EXPORT_SYMBOL(vsscanf);
2259 * sscanf - Unformat a buffer into a list of arguments
2260 * @buf: input buffer
2261 * @fmt: formatting of buffer
2262 * @...: resulting arguments
2264 int sscanf(const char *buf, const char *fmt, ...)
2269 va_start(args, fmt);
2270 i = vsscanf(buf, fmt, args);
2275 EXPORT_SYMBOL(sscanf);