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 /* See comment in put_dec_full9 for choice of constants */
247 static noinline_for_stack
248 void put_dec_full4(char *buf, unsigned q)
251 r = (q * 0xccd) >> 15;
252 buf[0] = (q - 10 * r) + '0';
253 q = (r * 0xcd) >> 11;
254 buf[1] = (r - 10 * q) + '0';
255 r = (q * 0xcd) >> 11;
256 buf[2] = (q - 10 * r) + '0';
261 * Call put_dec_full4 on x % 10000, return x / 10000.
262 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
263 * holds for all x < 1,128,869,999. The largest value this
264 * helper will ever be asked to convert is 1,125,520,955.
265 * (d1 in the put_dec code, assuming n is all-ones).
268 unsigned put_dec_helper4(char *buf, unsigned x)
270 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
272 put_dec_full4(buf, x - q * 10000);
276 /* Based on code by Douglas W. Jones found at
277 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
278 * (with permission from the author).
279 * Performs no 64-bit division and hence should be fast on 32-bit machines.
282 char *put_dec(char *buf, unsigned long long n)
284 uint32_t d3, d2, d1, q, h;
286 if (n < 100*1000*1000)
287 return put_dec_trunc8(buf, n);
289 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
292 d3 = (h >> 16); /* implicit "& 0xffff" */
294 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
295 q = put_dec_helper4(buf, q);
297 q += 7671 * d3 + 9496 * d2 + 6 * d1;
298 q = put_dec_helper4(buf+4, q);
300 q += 4749 * d3 + 42 * d2;
301 q = put_dec_helper4(buf+8, q);
306 buf = put_dec_trunc8(buf, q);
307 else while (buf[-1] == '0')
316 * Convert passed number to decimal string.
317 * Returns the length of string. On buffer overflow, returns 0.
319 * If speed is not important, use snprintf(). It's easy to read the code.
321 int num_to_str(char *buf, int size, unsigned long long num)
323 char tmp[sizeof(num) * 3];
326 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
331 len = put_dec(tmp, num) - tmp;
336 for (idx = 0; idx < len; ++idx)
337 buf[idx] = tmp[len - idx - 1];
341 #define ZEROPAD 1 /* pad with zero */
342 #define SIGN 2 /* unsigned/signed long */
343 #define PLUS 4 /* show plus */
344 #define SPACE 8 /* space if plus */
345 #define LEFT 16 /* left justified */
346 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
347 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
350 FORMAT_TYPE_NONE, /* Just a string part */
352 FORMAT_TYPE_PRECISION,
356 FORMAT_TYPE_PERCENT_CHAR,
358 FORMAT_TYPE_LONG_LONG,
373 u8 type; /* format_type enum */
374 u8 flags; /* flags to number() */
375 u8 base; /* number base, 8, 10 or 16 only */
376 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
377 s16 field_width; /* width of output field */
378 s16 precision; /* # of digits/chars */
381 static noinline_for_stack
382 char *number(char *buf, char *end, unsigned long long num,
383 struct printf_spec spec)
385 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
386 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
391 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
393 bool is_zero = num == 0LL;
395 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
396 * produces same digits or (maybe lowercased) letters */
397 locase = (spec.flags & SMALL);
398 if (spec.flags & LEFT)
399 spec.flags &= ~ZEROPAD;
401 if (spec.flags & SIGN) {
402 if ((signed long long)num < 0) {
404 num = -(signed long long)num;
406 } else if (spec.flags & PLUS) {
409 } else if (spec.flags & SPACE) {
416 spec.field_width -= 2;
421 /* generate full string in tmp[], in reverse order */
424 tmp[i++] = digits[num] | locase;
425 /* Generic code, for any base:
427 tmp[i++] = (digits[do_div(num,base)] | locase);
430 else if (spec.base != 10) { /* 8 or 16 */
431 int mask = spec.base - 1;
437 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
440 } else { /* base 10 */
441 i = put_dec(tmp, num) - tmp;
444 /* printing 100 using %2d gives "100", not "00" */
445 if (i > spec.precision)
447 /* leading space padding */
448 spec.field_width -= spec.precision;
449 if (!(spec.flags & (ZEROPAD+LEFT))) {
450 while (--spec.field_width >= 0) {
462 /* "0x" / "0" prefix */
464 if (spec.base == 16 || !is_zero) {
469 if (spec.base == 16) {
471 *buf = ('X' | locase);
475 /* zero or space padding */
476 if (!(spec.flags & LEFT)) {
477 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
478 while (--spec.field_width >= 0) {
484 /* hmm even more zero padding? */
485 while (i <= --spec.precision) {
490 /* actual digits of result */
496 /* trailing space padding */
497 while (--spec.field_width >= 0) {
506 static noinline_for_stack
507 char *string(char *buf, char *end, const char *s, struct printf_spec spec)
511 if ((unsigned long)s < PAGE_SIZE)
514 len = strnlen(s, spec.precision);
516 if (!(spec.flags & LEFT)) {
517 while (len < spec.field_width--) {
523 for (i = 0; i < len; ++i) {
528 while (len < spec.field_width--) {
537 static noinline_for_stack
538 char *symbol_string(char *buf, char *end, void *ptr,
539 struct printf_spec spec, char ext)
541 unsigned long value = (unsigned long) ptr;
542 #ifdef CONFIG_KALLSYMS
543 char sym[KSYM_SYMBOL_LEN];
545 sprint_backtrace(sym, value);
546 else if (ext != 'f' && ext != 's')
547 sprint_symbol(sym, value);
549 sprint_symbol_no_offset(sym, value);
551 return string(buf, end, sym, spec);
553 spec.field_width = 2 * sizeof(void *);
554 spec.flags |= SPECIAL | SMALL | ZEROPAD;
557 return number(buf, end, value, spec);
561 static noinline_for_stack
562 char *resource_string(char *buf, char *end, struct resource *res,
563 struct printf_spec spec, const char *fmt)
565 #ifndef IO_RSRC_PRINTK_SIZE
566 #define IO_RSRC_PRINTK_SIZE 6
569 #ifndef MEM_RSRC_PRINTK_SIZE
570 #define MEM_RSRC_PRINTK_SIZE 10
572 static const struct printf_spec io_spec = {
574 .field_width = IO_RSRC_PRINTK_SIZE,
576 .flags = SPECIAL | SMALL | ZEROPAD,
578 static const struct printf_spec mem_spec = {
580 .field_width = MEM_RSRC_PRINTK_SIZE,
582 .flags = SPECIAL | SMALL | ZEROPAD,
584 static const struct printf_spec bus_spec = {
588 .flags = SMALL | ZEROPAD,
590 static const struct printf_spec dec_spec = {
595 static const struct printf_spec str_spec = {
600 static const struct printf_spec flag_spec = {
603 .flags = SPECIAL | SMALL,
606 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
607 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
608 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
609 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
610 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
611 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
612 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
613 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
615 char *p = sym, *pend = sym + sizeof(sym);
616 int decode = (fmt[0] == 'R') ? 1 : 0;
617 const struct printf_spec *specp;
620 if (res->flags & IORESOURCE_IO) {
621 p = string(p, pend, "io ", str_spec);
623 } else if (res->flags & IORESOURCE_MEM) {
624 p = string(p, pend, "mem ", str_spec);
626 } else if (res->flags & IORESOURCE_IRQ) {
627 p = string(p, pend, "irq ", str_spec);
629 } else if (res->flags & IORESOURCE_DMA) {
630 p = string(p, pend, "dma ", str_spec);
632 } else if (res->flags & IORESOURCE_BUS) {
633 p = string(p, pend, "bus ", str_spec);
636 p = string(p, pend, "??? ", str_spec);
640 p = number(p, pend, res->start, *specp);
641 if (res->start != res->end) {
643 p = number(p, pend, res->end, *specp);
646 if (res->flags & IORESOURCE_MEM_64)
647 p = string(p, pend, " 64bit", str_spec);
648 if (res->flags & IORESOURCE_PREFETCH)
649 p = string(p, pend, " pref", str_spec);
650 if (res->flags & IORESOURCE_WINDOW)
651 p = string(p, pend, " window", str_spec);
652 if (res->flags & IORESOURCE_DISABLED)
653 p = string(p, pend, " disabled", str_spec);
655 p = string(p, pend, " flags ", str_spec);
656 p = number(p, pend, res->flags, flag_spec);
661 return string(buf, end, sym, spec);
664 static noinline_for_stack
665 char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
668 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
669 negative value, fallback to the default */
672 if (spec.field_width == 0)
673 /* nothing to print */
676 if (ZERO_OR_NULL_PTR(addr))
678 return string(buf, end, NULL, spec);
695 if (spec.field_width > 0)
696 len = min_t(int, spec.field_width, 64);
698 for (i = 0; i < len && buf < end - 1; i++) {
699 buf = hex_byte_pack(buf, addr[i]);
701 if (buf < end && separator && i != len - 1)
708 static noinline_for_stack
709 char *mac_address_string(char *buf, char *end, u8 *addr,
710 struct printf_spec spec, const char *fmt)
712 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
716 bool reversed = false;
732 for (i = 0; i < 6; i++) {
734 p = hex_byte_pack(p, addr[5 - i]);
736 p = hex_byte_pack(p, addr[i]);
738 if (fmt[0] == 'M' && i != 5)
743 return string(buf, end, mac_addr, spec);
746 static noinline_for_stack
747 char *ip4_string(char *p, const u8 *addr, const char *fmt)
750 bool leading_zeros = (fmt[0] == 'i');
775 for (i = 0; i < 4; i++) {
776 char temp[3]; /* hold each IP quad in reverse order */
777 int digits = put_dec_trunc8(temp, addr[index]) - temp;
784 /* reverse the digits in the quad */
796 static noinline_for_stack
797 char *ip6_compressed_string(char *p, const char *addr)
800 unsigned char zerolength[8];
805 bool needcolon = false;
809 memcpy(&in6, addr, sizeof(struct in6_addr));
811 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
813 memset(zerolength, 0, sizeof(zerolength));
820 /* find position of longest 0 run */
821 for (i = 0; i < range; i++) {
822 for (j = i; j < range; j++) {
823 if (in6.s6_addr16[j] != 0)
828 for (i = 0; i < range; i++) {
829 if (zerolength[i] > longest) {
830 longest = zerolength[i];
834 if (longest == 1) /* don't compress a single 0 */
838 for (i = 0; i < range; i++) {
840 if (needcolon || i == 0)
851 /* hex u16 without leading 0s */
852 word = ntohs(in6.s6_addr16[i]);
857 p = hex_byte_pack(p, hi);
859 *p++ = hex_asc_lo(hi);
860 p = hex_byte_pack(p, lo);
863 p = hex_byte_pack(p, lo);
865 *p++ = hex_asc_lo(lo);
872 p = ip4_string(p, &in6.s6_addr[12], "I4");
879 static noinline_for_stack
880 char *ip6_string(char *p, const char *addr, const char *fmt)
884 for (i = 0; i < 8; i++) {
885 p = hex_byte_pack(p, *addr++);
886 p = hex_byte_pack(p, *addr++);
887 if (fmt[0] == 'I' && i != 7)
895 static noinline_for_stack
896 char *ip6_addr_string(char *buf, char *end, const u8 *addr,
897 struct printf_spec spec, const char *fmt)
899 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
901 if (fmt[0] == 'I' && fmt[2] == 'c')
902 ip6_compressed_string(ip6_addr, addr);
904 ip6_string(ip6_addr, addr, fmt);
906 return string(buf, end, ip6_addr, spec);
909 static noinline_for_stack
910 char *ip4_addr_string(char *buf, char *end, const u8 *addr,
911 struct printf_spec spec, const char *fmt)
913 char ip4_addr[sizeof("255.255.255.255")];
915 ip4_string(ip4_addr, addr, fmt);
917 return string(buf, end, ip4_addr, spec);
920 static noinline_for_stack
921 char *uuid_string(char *buf, char *end, const u8 *addr,
922 struct printf_spec spec, const char *fmt)
924 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
927 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
928 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
929 const u8 *index = be;
934 uc = true; /* fall-through */
943 for (i = 0; i < 16; i++) {
944 p = hex_byte_pack(p, addr[index[i]]);
964 return string(buf, end, uuid, spec);
968 char *netdev_feature_string(char *buf, char *end, const u8 *addr,
969 struct printf_spec spec)
971 spec.flags |= SPECIAL | SMALL | ZEROPAD;
972 if (spec.field_width == -1)
973 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
976 return number(buf, end, *(const netdev_features_t *)addr, spec);
979 int kptr_restrict __read_mostly;
982 * Show a '%p' thing. A kernel extension is that the '%p' is followed
983 * by an extra set of alphanumeric characters that are extended format
986 * Right now we handle:
988 * - 'F' For symbolic function descriptor pointers with offset
989 * - 'f' For simple symbolic function names without offset
990 * - 'S' For symbolic direct pointers with offset
991 * - 's' For symbolic direct pointers without offset
992 * - 'B' For backtraced symbolic direct pointers with offset
993 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
994 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
995 * - 'M' For a 6-byte MAC address, it prints the address in the
996 * usual colon-separated hex notation
997 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
998 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
999 * with a dash-separated hex notation
1000 * - '[mM]R For a 6-byte MAC address, Reverse order (Bluetooth)
1001 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1002 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1003 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1004 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1005 * IPv6 omits the colons (01020304...0f)
1006 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1007 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
1008 * - 'I6c' for IPv6 addresses printed as specified by
1009 * http://tools.ietf.org/html/rfc5952
1010 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1011 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1012 * Options for %pU are:
1013 * b big endian lower case hex (default)
1014 * B big endian UPPER case hex
1015 * l little endian lower case hex
1016 * L little endian UPPER case hex
1017 * big endian output byte order is:
1018 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1019 * little endian output byte order is:
1020 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1021 * - 'V' For a struct va_format which contains a format string * and va_list *,
1022 * call vsnprintf(->format, *->va_list).
1023 * Implements a "recursive vsnprintf".
1024 * Do not use this feature without some mechanism to verify the
1025 * correctness of the format string and va_list arguments.
1026 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1027 * - 'NF' For a netdev_features_t
1028 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1029 * a certain separator (' ' by default):
1033 * The maximum supported length is 64 bytes of the input. Consider
1034 * to use print_hex_dump() for the larger input.
1036 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1037 * function pointers are really function descriptors, which contain a
1038 * pointer to the real address.
1040 static noinline_for_stack
1041 char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1042 struct printf_spec spec)
1044 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1046 if (!ptr && *fmt != 'K') {
1048 * Print (null) with the same width as a pointer so it makes
1049 * tabular output look nice.
1051 if (spec.field_width == -1)
1052 spec.field_width = default_width;
1053 return string(buf, end, "(null)", spec);
1059 ptr = dereference_function_descriptor(ptr);
1064 return symbol_string(buf, end, ptr, spec, *fmt);
1067 return resource_string(buf, end, ptr, spec, fmt);
1069 return hex_string(buf, end, ptr, spec, fmt);
1070 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1071 case 'm': /* Contiguous: 000102030405 */
1073 /* [mM]R (Reverse order; Bluetooth) */
1074 return mac_address_string(buf, end, ptr, spec, fmt);
1075 case 'I': /* Formatted IP supported
1077 * 6: 0001:0203:...:0708
1078 * 6c: 1::708 or 1::1.2.3.4
1080 case 'i': /* Contiguous:
1081 * 4: 001.002.003.004
1086 return ip6_addr_string(buf, end, ptr, spec, fmt);
1088 return ip4_addr_string(buf, end, ptr, spec, fmt);
1092 return uuid_string(buf, end, ptr, spec, fmt);
1097 va_copy(va, *((struct va_format *)ptr)->va);
1098 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1099 ((struct va_format *)ptr)->fmt, va);
1105 * %pK cannot be used in IRQ context because its test
1106 * for CAP_SYSLOG would be meaningless.
1108 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1110 if (spec.field_width == -1)
1111 spec.field_width = default_width;
1112 return string(buf, end, "pK-error", spec);
1114 if (!((kptr_restrict == 0) ||
1115 (kptr_restrict == 1 &&
1116 has_capability_noaudit(current, CAP_SYSLOG))))
1122 return netdev_feature_string(buf, end, ptr, spec);
1126 spec.flags |= SMALL;
1127 if (spec.field_width == -1) {
1128 spec.field_width = default_width;
1129 spec.flags |= ZEROPAD;
1133 return number(buf, end, (unsigned long) ptr, spec);
1137 * Helper function to decode printf style format.
1138 * Each call decode a token from the format and return the
1139 * number of characters read (or likely the delta where it wants
1140 * to go on the next call).
1141 * The decoded token is returned through the parameters
1143 * 'h', 'l', or 'L' for integer fields
1144 * 'z' support added 23/7/1999 S.H.
1145 * 'z' changed to 'Z' --davidm 1/25/99
1146 * 't' added for ptrdiff_t
1148 * @fmt: the format string
1149 * @type of the token returned
1150 * @flags: various flags such as +, -, # tokens..
1151 * @field_width: overwritten width
1152 * @base: base of the number (octal, hex, ...)
1153 * @precision: precision of a number
1154 * @qualifier: qualifier of a number (long, size_t, ...)
1156 static noinline_for_stack
1157 int format_decode(const char *fmt, struct printf_spec *spec)
1159 const char *start = fmt;
1161 /* we finished early by reading the field width */
1162 if (spec->type == FORMAT_TYPE_WIDTH) {
1163 if (spec->field_width < 0) {
1164 spec->field_width = -spec->field_width;
1165 spec->flags |= LEFT;
1167 spec->type = FORMAT_TYPE_NONE;
1171 /* we finished early by reading the precision */
1172 if (spec->type == FORMAT_TYPE_PRECISION) {
1173 if (spec->precision < 0)
1174 spec->precision = 0;
1176 spec->type = FORMAT_TYPE_NONE;
1181 spec->type = FORMAT_TYPE_NONE;
1183 for (; *fmt ; ++fmt) {
1188 /* Return the current non-format string */
1189 if (fmt != start || !*fmt)
1195 while (1) { /* this also skips first '%' */
1201 case '-': spec->flags |= LEFT; break;
1202 case '+': spec->flags |= PLUS; break;
1203 case ' ': spec->flags |= SPACE; break;
1204 case '#': spec->flags |= SPECIAL; break;
1205 case '0': spec->flags |= ZEROPAD; break;
1206 default: found = false;
1213 /* get field width */
1214 spec->field_width = -1;
1217 spec->field_width = skip_atoi(&fmt);
1218 else if (*fmt == '*') {
1219 /* it's the next argument */
1220 spec->type = FORMAT_TYPE_WIDTH;
1221 return ++fmt - start;
1225 /* get the precision */
1226 spec->precision = -1;
1229 if (isdigit(*fmt)) {
1230 spec->precision = skip_atoi(&fmt);
1231 if (spec->precision < 0)
1232 spec->precision = 0;
1233 } else if (*fmt == '*') {
1234 /* it's the next argument */
1235 spec->type = FORMAT_TYPE_PRECISION;
1236 return ++fmt - start;
1241 /* get the conversion qualifier */
1242 spec->qualifier = -1;
1243 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1244 _tolower(*fmt) == 'z' || *fmt == 't') {
1245 spec->qualifier = *fmt++;
1246 if (unlikely(spec->qualifier == *fmt)) {
1247 if (spec->qualifier == 'l') {
1248 spec->qualifier = 'L';
1250 } else if (spec->qualifier == 'h') {
1251 spec->qualifier = 'H';
1261 spec->type = FORMAT_TYPE_CHAR;
1262 return ++fmt - start;
1265 spec->type = FORMAT_TYPE_STR;
1266 return ++fmt - start;
1269 spec->type = FORMAT_TYPE_PTR;
1274 spec->type = FORMAT_TYPE_NRCHARS;
1275 return ++fmt - start;
1278 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1279 return ++fmt - start;
1281 /* integer number formats - set up the flags and "break" */
1287 spec->flags |= SMALL;
1295 spec->flags |= SIGN;
1300 spec->type = FORMAT_TYPE_INVALID;
1304 if (spec->qualifier == 'L')
1305 spec->type = FORMAT_TYPE_LONG_LONG;
1306 else if (spec->qualifier == 'l') {
1307 if (spec->flags & SIGN)
1308 spec->type = FORMAT_TYPE_LONG;
1310 spec->type = FORMAT_TYPE_ULONG;
1311 } else if (_tolower(spec->qualifier) == 'z') {
1312 spec->type = FORMAT_TYPE_SIZE_T;
1313 } else if (spec->qualifier == 't') {
1314 spec->type = FORMAT_TYPE_PTRDIFF;
1315 } else if (spec->qualifier == 'H') {
1316 if (spec->flags & SIGN)
1317 spec->type = FORMAT_TYPE_BYTE;
1319 spec->type = FORMAT_TYPE_UBYTE;
1320 } else if (spec->qualifier == 'h') {
1321 if (spec->flags & SIGN)
1322 spec->type = FORMAT_TYPE_SHORT;
1324 spec->type = FORMAT_TYPE_USHORT;
1326 if (spec->flags & SIGN)
1327 spec->type = FORMAT_TYPE_INT;
1329 spec->type = FORMAT_TYPE_UINT;
1332 return ++fmt - start;
1336 * vsnprintf - Format a string and place it in a buffer
1337 * @buf: The buffer to place the result into
1338 * @size: The size of the buffer, including the trailing null space
1339 * @fmt: The format string to use
1340 * @args: Arguments for the format string
1342 * This function follows C99 vsnprintf, but has some extensions:
1343 * %pS output the name of a text symbol with offset
1344 * %ps output the name of a text symbol without offset
1345 * %pF output the name of a function pointer with its offset
1346 * %pf output the name of a function pointer without its offset
1347 * %pB output the name of a backtrace symbol with its offset
1348 * %pR output the address range in a struct resource with decoded flags
1349 * %pr output the address range in a struct resource with raw flags
1350 * %pM output a 6-byte MAC address with colons
1351 * %pm output a 6-byte MAC address without colons
1352 * %pI4 print an IPv4 address without leading zeros
1353 * %pi4 print an IPv4 address with leading zeros
1354 * %pI6 print an IPv6 address with colons
1355 * %pi6 print an IPv6 address without colons
1356 * %pI6c print an IPv6 address as specified by RFC 5952
1357 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1359 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1360 * bytes of the input)
1363 * ** Please update Documentation/printk-formats.txt when making changes **
1365 * The return value is the number of characters which would
1366 * be generated for the given input, excluding the trailing
1367 * '\0', as per ISO C99. If you want to have the exact
1368 * number of characters written into @buf as return value
1369 * (not including the trailing '\0'), use vscnprintf(). If the
1370 * return is greater than or equal to @size, the resulting
1371 * string is truncated.
1373 * If you're not already dealing with a va_list consider using snprintf().
1375 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1377 unsigned long long num;
1379 struct printf_spec spec = {0};
1381 /* Reject out-of-range values early. Large positive sizes are
1382 used for unknown buffer sizes. */
1383 if (WARN_ON_ONCE((int) size < 0))
1389 /* Make sure end is always >= buf */
1396 const char *old_fmt = fmt;
1397 int read = format_decode(fmt, &spec);
1401 switch (spec.type) {
1402 case FORMAT_TYPE_NONE: {
1405 if (copy > end - str)
1407 memcpy(str, old_fmt, copy);
1413 case FORMAT_TYPE_WIDTH:
1414 spec.field_width = va_arg(args, int);
1417 case FORMAT_TYPE_PRECISION:
1418 spec.precision = va_arg(args, int);
1421 case FORMAT_TYPE_CHAR: {
1424 if (!(spec.flags & LEFT)) {
1425 while (--spec.field_width > 0) {
1432 c = (unsigned char) va_arg(args, int);
1436 while (--spec.field_width > 0) {
1444 case FORMAT_TYPE_STR:
1445 str = string(str, end, va_arg(args, char *), spec);
1448 case FORMAT_TYPE_PTR:
1449 str = pointer(fmt+1, str, end, va_arg(args, void *),
1451 while (isalnum(*fmt))
1455 case FORMAT_TYPE_PERCENT_CHAR:
1461 case FORMAT_TYPE_INVALID:
1467 case FORMAT_TYPE_NRCHARS: {
1468 u8 qualifier = spec.qualifier;
1470 if (qualifier == 'l') {
1471 long *ip = va_arg(args, long *);
1473 } else if (_tolower(qualifier) == 'z') {
1474 size_t *ip = va_arg(args, size_t *);
1477 int *ip = va_arg(args, int *);
1484 switch (spec.type) {
1485 case FORMAT_TYPE_LONG_LONG:
1486 num = va_arg(args, long long);
1488 case FORMAT_TYPE_ULONG:
1489 num = va_arg(args, unsigned long);
1491 case FORMAT_TYPE_LONG:
1492 num = va_arg(args, long);
1494 case FORMAT_TYPE_SIZE_T:
1495 num = va_arg(args, size_t);
1497 case FORMAT_TYPE_PTRDIFF:
1498 num = va_arg(args, ptrdiff_t);
1500 case FORMAT_TYPE_UBYTE:
1501 num = (unsigned char) va_arg(args, int);
1503 case FORMAT_TYPE_BYTE:
1504 num = (signed char) va_arg(args, int);
1506 case FORMAT_TYPE_USHORT:
1507 num = (unsigned short) va_arg(args, int);
1509 case FORMAT_TYPE_SHORT:
1510 num = (short) va_arg(args, int);
1512 case FORMAT_TYPE_INT:
1513 num = (int) va_arg(args, int);
1516 num = va_arg(args, unsigned int);
1519 str = number(str, end, num, spec);
1530 /* the trailing null byte doesn't count towards the total */
1534 EXPORT_SYMBOL(vsnprintf);
1537 * vscnprintf - Format a string and place it in a buffer
1538 * @buf: The buffer to place the result into
1539 * @size: The size of the buffer, including the trailing null space
1540 * @fmt: The format string to use
1541 * @args: Arguments for the format string
1543 * The return value is the number of characters which have been written into
1544 * the @buf not including the trailing '\0'. If @size is == 0 the function
1547 * If you're not already dealing with a va_list consider using scnprintf().
1549 * See the vsnprintf() documentation for format string extensions over C99.
1551 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1555 i = vsnprintf(buf, size, fmt, args);
1557 if (likely(i < size))
1563 EXPORT_SYMBOL(vscnprintf);
1566 * snprintf - Format a string and place it in a buffer
1567 * @buf: The buffer to place the result into
1568 * @size: The size of the buffer, including the trailing null space
1569 * @fmt: The format string to use
1570 * @...: Arguments for the format string
1572 * The return value is the number of characters which would be
1573 * generated for the given input, excluding the trailing null,
1574 * as per ISO C99. If the return is greater than or equal to
1575 * @size, the resulting string is truncated.
1577 * See the vsnprintf() documentation for format string extensions over C99.
1579 int snprintf(char *buf, size_t size, const char *fmt, ...)
1584 va_start(args, fmt);
1585 i = vsnprintf(buf, size, fmt, args);
1590 EXPORT_SYMBOL(snprintf);
1593 * scnprintf - Format a string and place it in a buffer
1594 * @buf: The buffer to place the result into
1595 * @size: The size of the buffer, including the trailing null space
1596 * @fmt: The format string to use
1597 * @...: Arguments for the format string
1599 * The return value is the number of characters written into @buf not including
1600 * the trailing '\0'. If @size is == 0 the function returns 0.
1603 int scnprintf(char *buf, size_t size, const char *fmt, ...)
1608 va_start(args, fmt);
1609 i = vscnprintf(buf, size, fmt, args);
1614 EXPORT_SYMBOL(scnprintf);
1617 * vsprintf - Format a string and place it in a buffer
1618 * @buf: The buffer to place the result into
1619 * @fmt: The format string to use
1620 * @args: Arguments for the format string
1622 * The function returns the number of characters written
1623 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1626 * If you're not already dealing with a va_list consider using sprintf().
1628 * See the vsnprintf() documentation for format string extensions over C99.
1630 int vsprintf(char *buf, const char *fmt, va_list args)
1632 return vsnprintf(buf, INT_MAX, fmt, args);
1634 EXPORT_SYMBOL(vsprintf);
1637 * sprintf - Format a string and place it in a buffer
1638 * @buf: The buffer to place the result into
1639 * @fmt: The format string to use
1640 * @...: Arguments for the format string
1642 * The function returns the number of characters written
1643 * into @buf. Use snprintf() or scnprintf() in order to avoid
1646 * See the vsnprintf() documentation for format string extensions over C99.
1648 int sprintf(char *buf, const char *fmt, ...)
1653 va_start(args, fmt);
1654 i = vsnprintf(buf, INT_MAX, fmt, args);
1659 EXPORT_SYMBOL(sprintf);
1661 #ifdef CONFIG_BINARY_PRINTF
1664 * vbin_printf() - VA arguments to binary data
1665 * bstr_printf() - Binary data to text string
1669 * vbin_printf - Parse a format string and place args' binary value in a buffer
1670 * @bin_buf: The buffer to place args' binary value
1671 * @size: The size of the buffer(by words(32bits), not characters)
1672 * @fmt: The format string to use
1673 * @args: Arguments for the format string
1675 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1678 * The return value is the number of words(32bits) which would be generated for
1682 * If the return value is greater than @size, the resulting bin_buf is NOT
1683 * valid for bstr_printf().
1685 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1687 struct printf_spec spec = {0};
1690 str = (char *)bin_buf;
1691 end = (char *)(bin_buf + size);
1693 #define save_arg(type) \
1695 if (sizeof(type) == 8) { \
1696 unsigned long long value; \
1697 str = PTR_ALIGN(str, sizeof(u32)); \
1698 value = va_arg(args, unsigned long long); \
1699 if (str + sizeof(type) <= end) { \
1700 *(u32 *)str = *(u32 *)&value; \
1701 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1704 unsigned long value; \
1705 str = PTR_ALIGN(str, sizeof(type)); \
1706 value = va_arg(args, int); \
1707 if (str + sizeof(type) <= end) \
1708 *(typeof(type) *)str = (type)value; \
1710 str += sizeof(type); \
1714 int read = format_decode(fmt, &spec);
1718 switch (spec.type) {
1719 case FORMAT_TYPE_NONE:
1720 case FORMAT_TYPE_INVALID:
1721 case FORMAT_TYPE_PERCENT_CHAR:
1724 case FORMAT_TYPE_WIDTH:
1725 case FORMAT_TYPE_PRECISION:
1729 case FORMAT_TYPE_CHAR:
1733 case FORMAT_TYPE_STR: {
1734 const char *save_str = va_arg(args, char *);
1737 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1738 || (unsigned long)save_str < PAGE_SIZE)
1739 save_str = "(null)";
1740 len = strlen(save_str) + 1;
1741 if (str + len < end)
1742 memcpy(str, save_str, len);
1747 case FORMAT_TYPE_PTR:
1749 /* skip all alphanumeric pointer suffixes */
1750 while (isalnum(*fmt))
1754 case FORMAT_TYPE_NRCHARS: {
1755 /* skip %n 's argument */
1756 u8 qualifier = spec.qualifier;
1758 if (qualifier == 'l')
1759 skip_arg = va_arg(args, long *);
1760 else if (_tolower(qualifier) == 'z')
1761 skip_arg = va_arg(args, size_t *);
1763 skip_arg = va_arg(args, int *);
1768 switch (spec.type) {
1770 case FORMAT_TYPE_LONG_LONG:
1771 save_arg(long long);
1773 case FORMAT_TYPE_ULONG:
1774 case FORMAT_TYPE_LONG:
1775 save_arg(unsigned long);
1777 case FORMAT_TYPE_SIZE_T:
1780 case FORMAT_TYPE_PTRDIFF:
1781 save_arg(ptrdiff_t);
1783 case FORMAT_TYPE_UBYTE:
1784 case FORMAT_TYPE_BYTE:
1787 case FORMAT_TYPE_USHORT:
1788 case FORMAT_TYPE_SHORT:
1797 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1800 EXPORT_SYMBOL_GPL(vbin_printf);
1803 * bstr_printf - Format a string from binary arguments and place it in a buffer
1804 * @buf: The buffer to place the result into
1805 * @size: The size of the buffer, including the trailing null space
1806 * @fmt: The format string to use
1807 * @bin_buf: Binary arguments for the format string
1809 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1810 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1811 * a binary buffer that generated by vbin_printf.
1813 * The format follows C99 vsnprintf, but has some extensions:
1814 * see vsnprintf comment for details.
1816 * The return value is the number of characters which would
1817 * be generated for the given input, excluding the trailing
1818 * '\0', as per ISO C99. If you want to have the exact
1819 * number of characters written into @buf as return value
1820 * (not including the trailing '\0'), use vscnprintf(). If the
1821 * return is greater than or equal to @size, the resulting
1822 * string is truncated.
1824 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1826 struct printf_spec spec = {0};
1828 const char *args = (const char *)bin_buf;
1830 if (WARN_ON_ONCE((int) size < 0))
1836 #define get_arg(type) \
1838 typeof(type) value; \
1839 if (sizeof(type) == 8) { \
1840 args = PTR_ALIGN(args, sizeof(u32)); \
1841 *(u32 *)&value = *(u32 *)args; \
1842 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1844 args = PTR_ALIGN(args, sizeof(type)); \
1845 value = *(typeof(type) *)args; \
1847 args += sizeof(type); \
1851 /* Make sure end is always >= buf */
1858 const char *old_fmt = fmt;
1859 int read = format_decode(fmt, &spec);
1863 switch (spec.type) {
1864 case FORMAT_TYPE_NONE: {
1867 if (copy > end - str)
1869 memcpy(str, old_fmt, copy);
1875 case FORMAT_TYPE_WIDTH:
1876 spec.field_width = get_arg(int);
1879 case FORMAT_TYPE_PRECISION:
1880 spec.precision = get_arg(int);
1883 case FORMAT_TYPE_CHAR: {
1886 if (!(spec.flags & LEFT)) {
1887 while (--spec.field_width > 0) {
1893 c = (unsigned char) get_arg(char);
1897 while (--spec.field_width > 0) {
1905 case FORMAT_TYPE_STR: {
1906 const char *str_arg = args;
1907 args += strlen(str_arg) + 1;
1908 str = string(str, end, (char *)str_arg, spec);
1912 case FORMAT_TYPE_PTR:
1913 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1914 while (isalnum(*fmt))
1918 case FORMAT_TYPE_PERCENT_CHAR:
1919 case FORMAT_TYPE_INVALID:
1925 case FORMAT_TYPE_NRCHARS:
1930 unsigned long long num;
1932 switch (spec.type) {
1934 case FORMAT_TYPE_LONG_LONG:
1935 num = get_arg(long long);
1937 case FORMAT_TYPE_ULONG:
1938 case FORMAT_TYPE_LONG:
1939 num = get_arg(unsigned long);
1941 case FORMAT_TYPE_SIZE_T:
1942 num = get_arg(size_t);
1944 case FORMAT_TYPE_PTRDIFF:
1945 num = get_arg(ptrdiff_t);
1947 case FORMAT_TYPE_UBYTE:
1948 num = get_arg(unsigned char);
1950 case FORMAT_TYPE_BYTE:
1951 num = get_arg(signed char);
1953 case FORMAT_TYPE_USHORT:
1954 num = get_arg(unsigned short);
1956 case FORMAT_TYPE_SHORT:
1957 num = get_arg(short);
1959 case FORMAT_TYPE_UINT:
1960 num = get_arg(unsigned int);
1966 str = number(str, end, num, spec);
1968 } /* switch(spec.type) */
1980 /* the trailing null byte doesn't count towards the total */
1983 EXPORT_SYMBOL_GPL(bstr_printf);
1986 * bprintf - Parse a format string and place args' binary value in a buffer
1987 * @bin_buf: The buffer to place args' binary value
1988 * @size: The size of the buffer(by words(32bits), not characters)
1989 * @fmt: The format string to use
1990 * @...: Arguments for the format string
1992 * The function returns the number of words(u32) written
1995 int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2000 va_start(args, fmt);
2001 ret = vbin_printf(bin_buf, size, fmt, args);
2006 EXPORT_SYMBOL_GPL(bprintf);
2008 #endif /* CONFIG_BINARY_PRINTF */
2011 * vsscanf - Unformat a buffer into a list of arguments
2012 * @buf: input buffer
2013 * @fmt: format of buffer
2016 int vsscanf(const char *buf, const char *fmt, va_list args)
2018 const char *str = buf;
2027 while (*fmt && *str) {
2028 /* skip any white space in format */
2029 /* white space in format matchs any amount of
2030 * white space, including none, in the input.
2032 if (isspace(*fmt)) {
2033 fmt = skip_spaces(++fmt);
2034 str = skip_spaces(str);
2037 /* anything that is not a conversion must match exactly */
2038 if (*fmt != '%' && *fmt) {
2039 if (*fmt++ != *str++)
2048 /* skip this conversion.
2049 * advance both strings to next white space
2052 while (!isspace(*fmt) && *fmt != '%' && *fmt)
2054 while (!isspace(*str) && *str)
2059 /* get field width */
2062 field_width = skip_atoi(&fmt);
2064 /* get conversion qualifier */
2066 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2067 _tolower(*fmt) == 'z') {
2069 if (unlikely(qualifier == *fmt)) {
2070 if (qualifier == 'h') {
2073 } else if (qualifier == 'l') {
2089 char *s = (char *)va_arg(args, char*);
2090 if (field_width == -1)
2094 } while (--field_width > 0 && *str);
2100 char *s = (char *)va_arg(args, char *);
2101 if (field_width == -1)
2102 field_width = SHRT_MAX;
2103 /* first, skip leading white space in buffer */
2104 str = skip_spaces(str);
2106 /* now copy until next white space */
2107 while (*str && !isspace(*str) && field_width--)
2114 /* return number of characters read so far */
2116 int *i = (int *)va_arg(args, int*);
2134 /* looking for '%' in str */
2139 /* invalid format; stop here */
2143 /* have some sort of integer conversion.
2144 * first, skip white space in buffer.
2146 str = skip_spaces(str);
2149 if (is_sign && digit == '-')
2153 || (base == 16 && !isxdigit(digit))
2154 || (base == 10 && !isdigit(digit))
2155 || (base == 8 && (!isdigit(digit) || digit > '7'))
2156 || (base == 0 && !isdigit(digit)))
2159 switch (qualifier) {
2160 case 'H': /* that's 'hh' in format */
2162 signed char *s = (signed char *)va_arg(args, signed char *);
2163 *s = (signed char)simple_strtol(str, &next, base);
2165 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2166 *s = (unsigned char)simple_strtoul(str, &next, base);
2171 short *s = (short *)va_arg(args, short *);
2172 *s = (short)simple_strtol(str, &next, base);
2174 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2175 *s = (unsigned short)simple_strtoul(str, &next, base);
2180 long *l = (long *)va_arg(args, long *);
2181 *l = simple_strtol(str, &next, base);
2183 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2184 *l = simple_strtoul(str, &next, base);
2189 long long *l = (long long *)va_arg(args, long long *);
2190 *l = simple_strtoll(str, &next, base);
2192 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2193 *l = simple_strtoull(str, &next, base);
2199 size_t *s = (size_t *)va_arg(args, size_t *);
2200 *s = (size_t)simple_strtoul(str, &next, base);
2205 int *i = (int *)va_arg(args, int *);
2206 *i = (int)simple_strtol(str, &next, base);
2208 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2209 *i = (unsigned int)simple_strtoul(str, &next, base);
2221 * Now we've come all the way through so either the input string or the
2222 * format ended. In the former case, there can be a %n at the current
2223 * position in the format that needs to be filled.
2225 if (*fmt == '%' && *(fmt + 1) == 'n') {
2226 int *p = (int *)va_arg(args, int *);
2232 EXPORT_SYMBOL(vsscanf);
2235 * sscanf - Unformat a buffer into a list of arguments
2236 * @buf: input buffer
2237 * @fmt: formatting of buffer
2238 * @...: resulting arguments
2240 int sscanf(const char *buf, const char *fmt, ...)
2245 va_start(args, fmt);
2246 i = vsscanf(buf, fmt, args);
2251 EXPORT_SYMBOL(sscanf);