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 <linux/dcache.h>
30 #include <net/addrconf.h>
32 #include <asm/page.h> /* for PAGE_SIZE */
33 #include <asm/sections.h> /* for dereference_function_descriptor() */
38 * simple_strtoull - convert a string to an unsigned long long
39 * @cp: The start of the string
40 * @endp: A pointer to the end of the parsed string will be placed here
41 * @base: The number base to use
43 * This function is obsolete. Please use kstrtoull instead.
45 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
47 unsigned long long result
;
50 cp
= _parse_integer_fixup_radix(cp
, &base
);
51 rv
= _parse_integer(cp
, base
, &result
);
53 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
60 EXPORT_SYMBOL(simple_strtoull
);
63 * simple_strtoul - convert a string to an unsigned long
64 * @cp: The start of the string
65 * @endp: A pointer to the end of the parsed string will be placed here
66 * @base: The number base to use
68 * This function is obsolete. Please use kstrtoul instead.
70 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
72 return simple_strtoull(cp
, endp
, base
);
74 EXPORT_SYMBOL(simple_strtoul
);
77 * simple_strtol - convert a string to a signed long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
82 * This function is obsolete. Please use kstrtol instead.
84 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
87 return -simple_strtoul(cp
+ 1, endp
, base
);
89 return simple_strtoul(cp
, endp
, base
);
91 EXPORT_SYMBOL(simple_strtol
);
94 * simple_strtoll - convert a string to a signed long long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
99 * This function is obsolete. Please use kstrtoll instead.
101 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
104 return -simple_strtoull(cp
+ 1, endp
, base
);
106 return simple_strtoull(cp
, endp
, base
);
108 EXPORT_SYMBOL(simple_strtoll
);
110 static noinline_for_stack
111 int skip_atoi(const char **s
)
116 i
= i
*10 + *((*s
)++) - '0';
121 /* Decimal conversion is by far the most typical, and is used
122 * for /proc and /sys data. This directly impacts e.g. top performance
123 * with many processes running. We optimize it for speed
124 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
125 * (with permission from the author, Douglas W. Jones).
128 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
129 /* Formats correctly any integer in [0, 999999999] */
130 static noinline_for_stack
131 char *put_dec_full9(char *buf
, unsigned q
)
136 * Possible ways to approx. divide by 10
137 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
138 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
139 * (x * 0x6667) >> 18 x < 43699
140 * (x * 0x3334) >> 17 x < 16389
141 * (x * 0x199a) >> 16 x < 16389
142 * (x * 0x0ccd) >> 15 x < 16389
143 * (x * 0x0667) >> 14 x < 2739
144 * (x * 0x0334) >> 13 x < 1029
145 * (x * 0x019a) >> 12 x < 1029
146 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
147 * (x * 0x0067) >> 10 x < 179
148 * (x * 0x0034) >> 9 x < 69 same
149 * (x * 0x001a) >> 8 x < 69 same
150 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
151 * (x * 0x0007) >> 6 x < 19
152 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
154 r
= (q
* (uint64_t)0x1999999a) >> 32;
155 *buf
++ = (q
- 10 * r
) + '0'; /* 1 */
156 q
= (r
* (uint64_t)0x1999999a) >> 32;
157 *buf
++ = (r
- 10 * q
) + '0'; /* 2 */
158 r
= (q
* (uint64_t)0x1999999a) >> 32;
159 *buf
++ = (q
- 10 * r
) + '0'; /* 3 */
160 q
= (r
* (uint64_t)0x1999999a) >> 32;
161 *buf
++ = (r
- 10 * q
) + '0'; /* 4 */
162 r
= (q
* (uint64_t)0x1999999a) >> 32;
163 *buf
++ = (q
- 10 * r
) + '0'; /* 5 */
164 /* Now value is under 10000, can avoid 64-bit multiply */
165 q
= (r
* 0x199a) >> 16;
166 *buf
++ = (r
- 10 * q
) + '0'; /* 6 */
167 r
= (q
* 0xcd) >> 11;
168 *buf
++ = (q
- 10 * r
) + '0'; /* 7 */
169 q
= (r
* 0xcd) >> 11;
170 *buf
++ = (r
- 10 * q
) + '0'; /* 8 */
171 *buf
++ = q
+ '0'; /* 9 */
176 /* Similar to above but do not pad with zeros.
177 * Code can be easily arranged to print 9 digits too, but our callers
178 * always call put_dec_full9() instead when the number has 9 decimal digits.
180 static noinline_for_stack
181 char *put_dec_trunc8(char *buf
, unsigned r
)
185 /* Copy of previous function's body with added early returns */
188 r
= (r
* (uint64_t)0x1999999a) >> 32;
192 q
= (r
* 0x199a) >> 16; /* r <= 9999 */
193 *buf
++ = (r
- 10 * q
) + '0';
196 r
= (q
* 0xcd) >> 11; /* q <= 999 */
197 *buf
++ = (q
- 10 * r
) + '0';
200 q
= (r
* 0xcd) >> 11; /* r <= 99 */
201 *buf
++ = (r
- 10 * q
) + '0';
204 *buf
++ = q
+ '0'; /* q <= 9 */
208 /* There are two algorithms to print larger numbers.
209 * One is generic: divide by 1000000000 and repeatedly print
210 * groups of (up to) 9 digits. It's conceptually simple,
211 * but requires a (unsigned long long) / 1000000000 division.
213 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
214 * manipulates them cleverly and generates groups of 4 decimal digits.
215 * It so happens that it does NOT require long long division.
217 * If long is > 32 bits, division of 64-bit values is relatively easy,
218 * and we will use the first algorithm.
219 * If long long is > 64 bits (strange architecture with VERY large long long),
220 * second algorithm can't be used, and we again use the first one.
222 * Else (if long is 32 bits and long long is 64 bits) we use second one.
225 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
227 /* First algorithm: generic */
230 char *put_dec(char *buf
, unsigned long long n
)
232 if (n
>= 100*1000*1000) {
233 while (n
>= 1000*1000*1000)
234 buf
= put_dec_full9(buf
, do_div(n
, 1000*1000*1000));
235 if (n
>= 100*1000*1000)
236 return put_dec_full9(buf
, n
);
238 return put_dec_trunc8(buf
, n
);
243 /* Second algorithm: valid only for 64-bit long longs */
245 /* See comment in put_dec_full9 for choice of constants */
246 static noinline_for_stack
247 void put_dec_full4(char *buf
, unsigned q
)
250 r
= (q
* 0xccd) >> 15;
251 buf
[0] = (q
- 10 * r
) + '0';
252 q
= (r
* 0xcd) >> 11;
253 buf
[1] = (r
- 10 * q
) + '0';
254 r
= (q
* 0xcd) >> 11;
255 buf
[2] = (q
- 10 * r
) + '0';
260 * Call put_dec_full4 on x % 10000, return x / 10000.
261 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
262 * holds for all x < 1,128,869,999. The largest value this
263 * helper will ever be asked to convert is 1,125,520,955.
264 * (d1 in the put_dec code, assuming n is all-ones).
267 unsigned put_dec_helper4(char *buf
, unsigned x
)
269 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
271 put_dec_full4(buf
, x
- q
* 10000);
275 /* Based on code by Douglas W. Jones found at
276 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
277 * (with permission from the author).
278 * Performs no 64-bit division and hence should be fast on 32-bit machines.
281 char *put_dec(char *buf
, unsigned long long n
)
283 uint32_t d3
, d2
, d1
, q
, h
;
285 if (n
< 100*1000*1000)
286 return put_dec_trunc8(buf
, n
);
288 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
291 d3
= (h
>> 16); /* implicit "& 0xffff" */
293 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
294 q
= put_dec_helper4(buf
, q
);
296 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
297 q
= put_dec_helper4(buf
+4, q
);
299 q
+= 4749 * d3
+ 42 * d2
;
300 q
= put_dec_helper4(buf
+8, q
);
305 buf
= put_dec_trunc8(buf
, q
);
306 else while (buf
[-1] == '0')
315 * Convert passed number to decimal string.
316 * Returns the length of string. On buffer overflow, returns 0.
318 * If speed is not important, use snprintf(). It's easy to read the code.
320 int num_to_str(char *buf
, int size
, unsigned long long num
)
322 char tmp
[sizeof(num
) * 3];
325 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
330 len
= put_dec(tmp
, num
) - tmp
;
335 for (idx
= 0; idx
< len
; ++idx
)
336 buf
[idx
] = tmp
[len
- idx
- 1];
340 #define ZEROPAD 1 /* pad with zero */
341 #define SIGN 2 /* unsigned/signed long */
342 #define PLUS 4 /* show plus */
343 #define SPACE 8 /* space if plus */
344 #define LEFT 16 /* left justified */
345 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
346 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
349 FORMAT_TYPE_NONE
, /* Just a string part */
351 FORMAT_TYPE_PRECISION
,
355 FORMAT_TYPE_PERCENT_CHAR
,
357 FORMAT_TYPE_LONG_LONG
,
372 u8 type
; /* format_type enum */
373 u8 flags
; /* flags to number() */
374 u8 base
; /* number base, 8, 10 or 16 only */
375 u8 qualifier
; /* number qualifier, one of 'hHlLtzZ' */
376 s16 field_width
; /* width of output field */
377 s16 precision
; /* # of digits/chars */
380 static noinline_for_stack
381 char *number(char *buf
, char *end
, unsigned long long num
,
382 struct printf_spec spec
)
384 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
385 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
390 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
392 bool is_zero
= num
== 0LL;
394 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
395 * produces same digits or (maybe lowercased) letters */
396 locase
= (spec
.flags
& SMALL
);
397 if (spec
.flags
& LEFT
)
398 spec
.flags
&= ~ZEROPAD
;
400 if (spec
.flags
& SIGN
) {
401 if ((signed long long)num
< 0) {
403 num
= -(signed long long)num
;
405 } else if (spec
.flags
& PLUS
) {
408 } else if (spec
.flags
& SPACE
) {
415 spec
.field_width
-= 2;
420 /* generate full string in tmp[], in reverse order */
423 tmp
[i
++] = digits
[num
] | locase
;
424 /* Generic code, for any base:
426 tmp[i++] = (digits[do_div(num,base)] | locase);
429 else if (spec
.base
!= 10) { /* 8 or 16 */
430 int mask
= spec
.base
- 1;
436 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
439 } else { /* base 10 */
440 i
= put_dec(tmp
, num
) - tmp
;
443 /* printing 100 using %2d gives "100", not "00" */
444 if (i
> spec
.precision
)
446 /* leading space padding */
447 spec
.field_width
-= spec
.precision
;
448 if (!(spec
.flags
& (ZEROPAD
+LEFT
))) {
449 while (--spec
.field_width
>= 0) {
461 /* "0x" / "0" prefix */
463 if (spec
.base
== 16 || !is_zero
) {
468 if (spec
.base
== 16) {
470 *buf
= ('X' | locase
);
474 /* zero or space padding */
475 if (!(spec
.flags
& LEFT
)) {
476 char c
= (spec
.flags
& ZEROPAD
) ? '0' : ' ';
477 while (--spec
.field_width
>= 0) {
483 /* hmm even more zero padding? */
484 while (i
<= --spec
.precision
) {
489 /* actual digits of result */
495 /* trailing space padding */
496 while (--spec
.field_width
>= 0) {
505 static noinline_for_stack
506 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
510 if ((unsigned long)s
< PAGE_SIZE
)
513 len
= strnlen(s
, spec
.precision
);
515 if (!(spec
.flags
& LEFT
)) {
516 while (len
< spec
.field_width
--) {
522 for (i
= 0; i
< len
; ++i
) {
527 while (len
< spec
.field_width
--) {
536 static void widen(char *buf
, char *end
, unsigned len
, unsigned spaces
)
539 if (buf
>= end
) /* nowhere to put anything */
542 if (size
<= spaces
) {
543 memset(buf
, ' ', size
);
547 if (len
> size
- spaces
)
549 memmove(buf
+ spaces
, buf
, len
);
551 memset(buf
, ' ', spaces
);
554 static noinline_for_stack
555 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
558 const char *array
[4], *s
;
559 const struct dentry
*p
;
564 case '2': case '3': case '4':
565 depth
= fmt
[1] - '0';
572 for (i
= 0; i
< depth
; i
++, d
= p
) {
573 p
= ACCESS_ONCE(d
->d_parent
);
574 array
[i
] = ACCESS_ONCE(d
->d_name
.name
);
583 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
595 if (n
< spec
.field_width
) {
596 /* we want to pad the sucker */
597 unsigned spaces
= spec
.field_width
- n
;
598 if (!(spec
.flags
& LEFT
)) {
599 widen(buf
- n
, end
, n
, spaces
);
611 static noinline_for_stack
612 char *symbol_string(char *buf
, char *end
, void *ptr
,
613 struct printf_spec spec
, const char *fmt
)
616 #ifdef CONFIG_KALLSYMS
617 char sym
[KSYM_SYMBOL_LEN
];
621 ptr
= __builtin_extract_return_addr(ptr
);
622 value
= (unsigned long)ptr
;
624 #ifdef CONFIG_KALLSYMS
626 sprint_backtrace(sym
, value
);
627 else if (*fmt
!= 'f' && *fmt
!= 's')
628 sprint_symbol(sym
, value
);
630 sprint_symbol_no_offset(sym
, value
);
632 return string(buf
, end
, sym
, spec
);
634 spec
.field_width
= 2 * sizeof(void *);
635 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
638 return number(buf
, end
, value
, spec
);
642 static noinline_for_stack
643 char *resource_string(char *buf
, char *end
, struct resource
*res
,
644 struct printf_spec spec
, const char *fmt
)
646 #ifndef IO_RSRC_PRINTK_SIZE
647 #define IO_RSRC_PRINTK_SIZE 6
650 #ifndef MEM_RSRC_PRINTK_SIZE
651 #define MEM_RSRC_PRINTK_SIZE 10
653 static const struct printf_spec io_spec
= {
655 .field_width
= IO_RSRC_PRINTK_SIZE
,
657 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
659 static const struct printf_spec mem_spec
= {
661 .field_width
= MEM_RSRC_PRINTK_SIZE
,
663 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
665 static const struct printf_spec bus_spec
= {
669 .flags
= SMALL
| ZEROPAD
,
671 static const struct printf_spec dec_spec
= {
676 static const struct printf_spec str_spec
= {
681 static const struct printf_spec flag_spec
= {
684 .flags
= SPECIAL
| SMALL
,
687 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
688 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
689 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
690 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
691 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
692 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
693 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
694 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
696 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
697 int decode
= (fmt
[0] == 'R') ? 1 : 0;
698 const struct printf_spec
*specp
;
701 if (res
->flags
& IORESOURCE_IO
) {
702 p
= string(p
, pend
, "io ", str_spec
);
704 } else if (res
->flags
& IORESOURCE_MEM
) {
705 p
= string(p
, pend
, "mem ", str_spec
);
707 } else if (res
->flags
& IORESOURCE_IRQ
) {
708 p
= string(p
, pend
, "irq ", str_spec
);
710 } else if (res
->flags
& IORESOURCE_DMA
) {
711 p
= string(p
, pend
, "dma ", str_spec
);
713 } else if (res
->flags
& IORESOURCE_BUS
) {
714 p
= string(p
, pend
, "bus ", str_spec
);
717 p
= string(p
, pend
, "??? ", str_spec
);
721 p
= number(p
, pend
, res
->start
, *specp
);
722 if (res
->start
!= res
->end
) {
724 p
= number(p
, pend
, res
->end
, *specp
);
727 if (res
->flags
& IORESOURCE_MEM_64
)
728 p
= string(p
, pend
, " 64bit", str_spec
);
729 if (res
->flags
& IORESOURCE_PREFETCH
)
730 p
= string(p
, pend
, " pref", str_spec
);
731 if (res
->flags
& IORESOURCE_WINDOW
)
732 p
= string(p
, pend
, " window", str_spec
);
733 if (res
->flags
& IORESOURCE_DISABLED
)
734 p
= string(p
, pend
, " disabled", str_spec
);
736 p
= string(p
, pend
, " flags ", str_spec
);
737 p
= number(p
, pend
, res
->flags
, flag_spec
);
742 return string(buf
, end
, sym
, spec
);
745 static noinline_for_stack
746 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
749 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
750 negative value, fallback to the default */
753 if (spec
.field_width
== 0)
754 /* nothing to print */
757 if (ZERO_OR_NULL_PTR(addr
))
759 return string(buf
, end
, NULL
, spec
);
776 if (spec
.field_width
> 0)
777 len
= min_t(int, spec
.field_width
, 64);
779 for (i
= 0; i
< len
&& buf
< end
- 1; i
++) {
780 buf
= hex_byte_pack(buf
, addr
[i
]);
782 if (buf
< end
&& separator
&& i
!= len
- 1)
789 static noinline_for_stack
790 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
791 struct printf_spec spec
, const char *fmt
)
793 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
797 bool reversed
= false;
813 for (i
= 0; i
< 6; i
++) {
815 p
= hex_byte_pack(p
, addr
[5 - i
]);
817 p
= hex_byte_pack(p
, addr
[i
]);
819 if (fmt
[0] == 'M' && i
!= 5)
824 return string(buf
, end
, mac_addr
, spec
);
827 static noinline_for_stack
828 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
831 bool leading_zeros
= (fmt
[0] == 'i');
856 for (i
= 0; i
< 4; i
++) {
857 char temp
[3]; /* hold each IP quad in reverse order */
858 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
865 /* reverse the digits in the quad */
877 static noinline_for_stack
878 char *ip6_compressed_string(char *p
, const char *addr
)
881 unsigned char zerolength
[8];
886 bool needcolon
= false;
890 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
892 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
894 memset(zerolength
, 0, sizeof(zerolength
));
901 /* find position of longest 0 run */
902 for (i
= 0; i
< range
; i
++) {
903 for (j
= i
; j
< range
; j
++) {
904 if (in6
.s6_addr16
[j
] != 0)
909 for (i
= 0; i
< range
; i
++) {
910 if (zerolength
[i
] > longest
) {
911 longest
= zerolength
[i
];
915 if (longest
== 1) /* don't compress a single 0 */
919 for (i
= 0; i
< range
; i
++) {
921 if (needcolon
|| i
== 0)
932 /* hex u16 without leading 0s */
933 word
= ntohs(in6
.s6_addr16
[i
]);
938 p
= hex_byte_pack(p
, hi
);
940 *p
++ = hex_asc_lo(hi
);
941 p
= hex_byte_pack(p
, lo
);
944 p
= hex_byte_pack(p
, lo
);
946 *p
++ = hex_asc_lo(lo
);
953 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
960 static noinline_for_stack
961 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
965 for (i
= 0; i
< 8; i
++) {
966 p
= hex_byte_pack(p
, *addr
++);
967 p
= hex_byte_pack(p
, *addr
++);
968 if (fmt
[0] == 'I' && i
!= 7)
976 static noinline_for_stack
977 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
978 struct printf_spec spec
, const char *fmt
)
980 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
982 if (fmt
[0] == 'I' && fmt
[2] == 'c')
983 ip6_compressed_string(ip6_addr
, addr
);
985 ip6_string(ip6_addr
, addr
, fmt
);
987 return string(buf
, end
, ip6_addr
, spec
);
990 static noinline_for_stack
991 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
992 struct printf_spec spec
, const char *fmt
)
994 char ip4_addr
[sizeof("255.255.255.255")];
996 ip4_string(ip4_addr
, addr
, fmt
);
998 return string(buf
, end
, ip4_addr
, spec
);
1001 static noinline_for_stack
1002 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1003 struct printf_spec spec
, const char *fmt
)
1005 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1006 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1007 sizeof(":12345") + sizeof("/123456789") +
1008 sizeof("%1234567890")];
1009 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1010 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1011 char fmt6
[2] = { fmt
[0], '6' };
1015 while (isalpha(*++fmt
)) {
1032 if (have_p
|| have_s
|| have_f
) {
1037 if (fmt6
[0] == 'I' && have_c
)
1038 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1040 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1042 if (have_p
|| have_s
|| have_f
)
1047 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1051 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1052 IPV6_FLOWINFO_MASK
), spec
);
1056 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1060 return string(buf
, end
, ip6_addr
, spec
);
1063 static noinline_for_stack
1064 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1065 struct printf_spec spec
, const char *fmt
)
1067 bool have_p
= false;
1068 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1069 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1070 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1071 char fmt4
[3] = { fmt
[0], '4', 0 };
1074 while (isalpha(*++fmt
)) {
1088 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1091 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1095 return string(buf
, end
, ip4_addr
, spec
);
1098 static noinline_for_stack
1099 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1100 struct printf_spec spec
, const char *fmt
)
1102 char uuid
[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1105 static const u8 be
[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1106 static const u8 le
[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1107 const u8
*index
= be
;
1112 uc
= true; /* fall-through */
1121 for (i
= 0; i
< 16; i
++) {
1122 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1142 return string(buf
, end
, uuid
, spec
);
1146 char *netdev_feature_string(char *buf
, char *end
, const u8
*addr
,
1147 struct printf_spec spec
)
1149 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
1150 if (spec
.field_width
== -1)
1151 spec
.field_width
= 2 + 2 * sizeof(netdev_features_t
);
1154 return number(buf
, end
, *(const netdev_features_t
*)addr
, spec
);
1157 int kptr_restrict __read_mostly
;
1160 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1161 * by an extra set of alphanumeric characters that are extended format
1164 * Right now we handle:
1166 * - 'F' For symbolic function descriptor pointers with offset
1167 * - 'f' For simple symbolic function names without offset
1168 * - 'S' For symbolic direct pointers with offset
1169 * - 's' For symbolic direct pointers without offset
1170 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1171 * - 'B' For backtraced symbolic direct pointers with offset
1172 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1173 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1174 * - 'M' For a 6-byte MAC address, it prints the address in the
1175 * usual colon-separated hex notation
1176 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1177 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1178 * with a dash-separated hex notation
1179 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1180 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1181 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1182 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1184 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1185 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1186 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1187 * IPv6 omits the colons (01020304...0f)
1188 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1190 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1191 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1192 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1193 * - 'I[6S]c' for IPv6 addresses printed as specified by
1194 * http://tools.ietf.org/html/rfc5952
1195 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1196 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1197 * Options for %pU are:
1198 * b big endian lower case hex (default)
1199 * B big endian UPPER case hex
1200 * l little endian lower case hex
1201 * L little endian UPPER case hex
1202 * big endian output byte order is:
1203 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1204 * little endian output byte order is:
1205 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1206 * - 'V' For a struct va_format which contains a format string * and va_list *,
1207 * call vsnprintf(->format, *->va_list).
1208 * Implements a "recursive vsnprintf".
1209 * Do not use this feature without some mechanism to verify the
1210 * correctness of the format string and va_list arguments.
1211 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1212 * - 'NF' For a netdev_features_t
1213 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1214 * a certain separator (' ' by default):
1218 * The maximum supported length is 64 bytes of the input. Consider
1219 * to use print_hex_dump() for the larger input.
1220 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
1222 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1223 * function pointers are really function descriptors, which contain a
1224 * pointer to the real address.
1226 static noinline_for_stack
1227 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1228 struct printf_spec spec
)
1230 int default_width
= 2 * sizeof(void *) + (spec
.flags
& SPECIAL
? 2 : 0);
1232 if (!ptr
&& *fmt
!= 'K') {
1234 * Print (null) with the same width as a pointer so it makes
1235 * tabular output look nice.
1237 if (spec
.field_width
== -1)
1238 spec
.field_width
= default_width
;
1239 return string(buf
, end
, "(null)", spec
);
1245 ptr
= dereference_function_descriptor(ptr
);
1250 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1253 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1255 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1256 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1257 case 'm': /* Contiguous: 000102030405 */
1259 /* [mM]R (Reverse order; Bluetooth) */
1260 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1261 case 'I': /* Formatted IP supported
1263 * 6: 0001:0203:...:0708
1264 * 6c: 1::708 or 1::1.2.3.4
1266 case 'i': /* Contiguous:
1267 * 4: 001.002.003.004
1272 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1274 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1277 struct sockaddr raw
;
1278 struct sockaddr_in v4
;
1279 struct sockaddr_in6 v6
;
1282 switch (sa
->raw
.sa_family
) {
1284 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1286 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1288 return string(buf
, end
, "(invalid address)", spec
);
1293 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1298 va_copy(va
, *((struct va_format
*)ptr
)->va
);
1299 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
1300 ((struct va_format
*)ptr
)->fmt
, va
);
1306 * %pK cannot be used in IRQ context because its test
1307 * for CAP_SYSLOG would be meaningless.
1309 if (kptr_restrict
&& (in_irq() || in_serving_softirq() ||
1311 if (spec
.field_width
== -1)
1312 spec
.field_width
= default_width
;
1313 return string(buf
, end
, "pK-error", spec
);
1315 if (!((kptr_restrict
== 0) ||
1316 (kptr_restrict
== 1 &&
1317 has_capability_noaudit(current
, CAP_SYSLOG
))))
1323 return netdev_feature_string(buf
, end
, ptr
, spec
);
1327 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
1328 spec
.field_width
= sizeof(phys_addr_t
) * 2 + 2;
1330 return number(buf
, end
,
1331 (unsigned long long) *((phys_addr_t
*)ptr
), spec
);
1333 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
1335 return dentry_name(buf
, end
,
1336 ((const struct file
*)ptr
)->f_path
.dentry
,
1339 spec
.flags
|= SMALL
;
1340 if (spec
.field_width
== -1) {
1341 spec
.field_width
= default_width
;
1342 spec
.flags
|= ZEROPAD
;
1346 return number(buf
, end
, (unsigned long) ptr
, spec
);
1350 * Helper function to decode printf style format.
1351 * Each call decode a token from the format and return the
1352 * number of characters read (or likely the delta where it wants
1353 * to go on the next call).
1354 * The decoded token is returned through the parameters
1356 * 'h', 'l', or 'L' for integer fields
1357 * 'z' support added 23/7/1999 S.H.
1358 * 'z' changed to 'Z' --davidm 1/25/99
1359 * 't' added for ptrdiff_t
1361 * @fmt: the format string
1362 * @type of the token returned
1363 * @flags: various flags such as +, -, # tokens..
1364 * @field_width: overwritten width
1365 * @base: base of the number (octal, hex, ...)
1366 * @precision: precision of a number
1367 * @qualifier: qualifier of a number (long, size_t, ...)
1369 static noinline_for_stack
1370 int format_decode(const char *fmt
, struct printf_spec
*spec
)
1372 const char *start
= fmt
;
1374 /* we finished early by reading the field width */
1375 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
1376 if (spec
->field_width
< 0) {
1377 spec
->field_width
= -spec
->field_width
;
1378 spec
->flags
|= LEFT
;
1380 spec
->type
= FORMAT_TYPE_NONE
;
1384 /* we finished early by reading the precision */
1385 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
1386 if (spec
->precision
< 0)
1387 spec
->precision
= 0;
1389 spec
->type
= FORMAT_TYPE_NONE
;
1394 spec
->type
= FORMAT_TYPE_NONE
;
1396 for (; *fmt
; ++fmt
) {
1401 /* Return the current non-format string */
1402 if (fmt
!= start
|| !*fmt
)
1408 while (1) { /* this also skips first '%' */
1414 case '-': spec
->flags
|= LEFT
; break;
1415 case '+': spec
->flags
|= PLUS
; break;
1416 case ' ': spec
->flags
|= SPACE
; break;
1417 case '#': spec
->flags
|= SPECIAL
; break;
1418 case '0': spec
->flags
|= ZEROPAD
; break;
1419 default: found
= false;
1426 /* get field width */
1427 spec
->field_width
= -1;
1430 spec
->field_width
= skip_atoi(&fmt
);
1431 else if (*fmt
== '*') {
1432 /* it's the next argument */
1433 spec
->type
= FORMAT_TYPE_WIDTH
;
1434 return ++fmt
- start
;
1438 /* get the precision */
1439 spec
->precision
= -1;
1442 if (isdigit(*fmt
)) {
1443 spec
->precision
= skip_atoi(&fmt
);
1444 if (spec
->precision
< 0)
1445 spec
->precision
= 0;
1446 } else if (*fmt
== '*') {
1447 /* it's the next argument */
1448 spec
->type
= FORMAT_TYPE_PRECISION
;
1449 return ++fmt
- start
;
1454 /* get the conversion qualifier */
1455 spec
->qualifier
= -1;
1456 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
1457 _tolower(*fmt
) == 'z' || *fmt
== 't') {
1458 spec
->qualifier
= *fmt
++;
1459 if (unlikely(spec
->qualifier
== *fmt
)) {
1460 if (spec
->qualifier
== 'l') {
1461 spec
->qualifier
= 'L';
1463 } else if (spec
->qualifier
== 'h') {
1464 spec
->qualifier
= 'H';
1474 spec
->type
= FORMAT_TYPE_CHAR
;
1475 return ++fmt
- start
;
1478 spec
->type
= FORMAT_TYPE_STR
;
1479 return ++fmt
- start
;
1482 spec
->type
= FORMAT_TYPE_PTR
;
1487 spec
->type
= FORMAT_TYPE_NRCHARS
;
1488 return ++fmt
- start
;
1491 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
1492 return ++fmt
- start
;
1494 /* integer number formats - set up the flags and "break" */
1500 spec
->flags
|= SMALL
;
1508 spec
->flags
|= SIGN
;
1513 spec
->type
= FORMAT_TYPE_INVALID
;
1517 if (spec
->qualifier
== 'L')
1518 spec
->type
= FORMAT_TYPE_LONG_LONG
;
1519 else if (spec
->qualifier
== 'l') {
1520 if (spec
->flags
& SIGN
)
1521 spec
->type
= FORMAT_TYPE_LONG
;
1523 spec
->type
= FORMAT_TYPE_ULONG
;
1524 } else if (_tolower(spec
->qualifier
) == 'z') {
1525 spec
->type
= FORMAT_TYPE_SIZE_T
;
1526 } else if (spec
->qualifier
== 't') {
1527 spec
->type
= FORMAT_TYPE_PTRDIFF
;
1528 } else if (spec
->qualifier
== 'H') {
1529 if (spec
->flags
& SIGN
)
1530 spec
->type
= FORMAT_TYPE_BYTE
;
1532 spec
->type
= FORMAT_TYPE_UBYTE
;
1533 } else if (spec
->qualifier
== 'h') {
1534 if (spec
->flags
& SIGN
)
1535 spec
->type
= FORMAT_TYPE_SHORT
;
1537 spec
->type
= FORMAT_TYPE_USHORT
;
1539 if (spec
->flags
& SIGN
)
1540 spec
->type
= FORMAT_TYPE_INT
;
1542 spec
->type
= FORMAT_TYPE_UINT
;
1545 return ++fmt
- start
;
1549 * vsnprintf - Format a string and place it in a buffer
1550 * @buf: The buffer to place the result into
1551 * @size: The size of the buffer, including the trailing null space
1552 * @fmt: The format string to use
1553 * @args: Arguments for the format string
1555 * This function follows C99 vsnprintf, but has some extensions:
1556 * %pS output the name of a text symbol with offset
1557 * %ps output the name of a text symbol without offset
1558 * %pF output the name of a function pointer with its offset
1559 * %pf output the name of a function pointer without its offset
1560 * %pB output the name of a backtrace symbol with its offset
1561 * %pR output the address range in a struct resource with decoded flags
1562 * %pr output the address range in a struct resource with raw flags
1563 * %pM output a 6-byte MAC address with colons
1564 * %pMR output a 6-byte MAC address with colons in reversed order
1565 * %pMF output a 6-byte MAC address with dashes
1566 * %pm output a 6-byte MAC address without colons
1567 * %pmR output a 6-byte MAC address without colons in reversed order
1568 * %pI4 print an IPv4 address without leading zeros
1569 * %pi4 print an IPv4 address with leading zeros
1570 * %pI6 print an IPv6 address with colons
1571 * %pi6 print an IPv6 address without colons
1572 * %pI6c print an IPv6 address as specified by RFC 5952
1573 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1574 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1575 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1577 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1578 * bytes of the input)
1581 * ** Please update Documentation/printk-formats.txt when making changes **
1583 * The return value is the number of characters which would
1584 * be generated for the given input, excluding the trailing
1585 * '\0', as per ISO C99. If you want to have the exact
1586 * number of characters written into @buf as return value
1587 * (not including the trailing '\0'), use vscnprintf(). If the
1588 * return is greater than or equal to @size, the resulting
1589 * string is truncated.
1591 * If you're not already dealing with a va_list consider using snprintf().
1593 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1595 unsigned long long num
;
1597 struct printf_spec spec
= {0};
1599 /* Reject out-of-range values early. Large positive sizes are
1600 used for unknown buffer sizes. */
1601 if (WARN_ON_ONCE((int) size
< 0))
1607 /* Make sure end is always >= buf */
1614 const char *old_fmt
= fmt
;
1615 int read
= format_decode(fmt
, &spec
);
1619 switch (spec
.type
) {
1620 case FORMAT_TYPE_NONE
: {
1623 if (copy
> end
- str
)
1625 memcpy(str
, old_fmt
, copy
);
1631 case FORMAT_TYPE_WIDTH
:
1632 spec
.field_width
= va_arg(args
, int);
1635 case FORMAT_TYPE_PRECISION
:
1636 spec
.precision
= va_arg(args
, int);
1639 case FORMAT_TYPE_CHAR
: {
1642 if (!(spec
.flags
& LEFT
)) {
1643 while (--spec
.field_width
> 0) {
1650 c
= (unsigned char) va_arg(args
, int);
1654 while (--spec
.field_width
> 0) {
1662 case FORMAT_TYPE_STR
:
1663 str
= string(str
, end
, va_arg(args
, char *), spec
);
1666 case FORMAT_TYPE_PTR
:
1667 str
= pointer(fmt
+1, str
, end
, va_arg(args
, void *),
1669 while (isalnum(*fmt
))
1673 case FORMAT_TYPE_PERCENT_CHAR
:
1679 case FORMAT_TYPE_INVALID
:
1685 case FORMAT_TYPE_NRCHARS
: {
1686 u8 qualifier
= spec
.qualifier
;
1688 if (qualifier
== 'l') {
1689 long *ip
= va_arg(args
, long *);
1691 } else if (_tolower(qualifier
) == 'z') {
1692 size_t *ip
= va_arg(args
, size_t *);
1695 int *ip
= va_arg(args
, int *);
1702 switch (spec
.type
) {
1703 case FORMAT_TYPE_LONG_LONG
:
1704 num
= va_arg(args
, long long);
1706 case FORMAT_TYPE_ULONG
:
1707 num
= va_arg(args
, unsigned long);
1709 case FORMAT_TYPE_LONG
:
1710 num
= va_arg(args
, long);
1712 case FORMAT_TYPE_SIZE_T
:
1713 if (spec
.flags
& SIGN
)
1714 num
= va_arg(args
, ssize_t
);
1716 num
= va_arg(args
, size_t);
1718 case FORMAT_TYPE_PTRDIFF
:
1719 num
= va_arg(args
, ptrdiff_t);
1721 case FORMAT_TYPE_UBYTE
:
1722 num
= (unsigned char) va_arg(args
, int);
1724 case FORMAT_TYPE_BYTE
:
1725 num
= (signed char) va_arg(args
, int);
1727 case FORMAT_TYPE_USHORT
:
1728 num
= (unsigned short) va_arg(args
, int);
1730 case FORMAT_TYPE_SHORT
:
1731 num
= (short) va_arg(args
, int);
1733 case FORMAT_TYPE_INT
:
1734 num
= (int) va_arg(args
, int);
1737 num
= va_arg(args
, unsigned int);
1740 str
= number(str
, end
, num
, spec
);
1751 /* the trailing null byte doesn't count towards the total */
1755 EXPORT_SYMBOL(vsnprintf
);
1758 * vscnprintf - Format a string and place it in a buffer
1759 * @buf: The buffer to place the result into
1760 * @size: The size of the buffer, including the trailing null space
1761 * @fmt: The format string to use
1762 * @args: Arguments for the format string
1764 * The return value is the number of characters which have been written into
1765 * the @buf not including the trailing '\0'. If @size is == 0 the function
1768 * If you're not already dealing with a va_list consider using scnprintf().
1770 * See the vsnprintf() documentation for format string extensions over C99.
1772 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1776 i
= vsnprintf(buf
, size
, fmt
, args
);
1778 if (likely(i
< size
))
1784 EXPORT_SYMBOL(vscnprintf
);
1787 * snprintf - Format a string and place it in a buffer
1788 * @buf: The buffer to place the result into
1789 * @size: The size of the buffer, including the trailing null space
1790 * @fmt: The format string to use
1791 * @...: Arguments for the format string
1793 * The return value is the number of characters which would be
1794 * generated for the given input, excluding the trailing null,
1795 * as per ISO C99. If the return is greater than or equal to
1796 * @size, the resulting string is truncated.
1798 * See the vsnprintf() documentation for format string extensions over C99.
1800 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
1805 va_start(args
, fmt
);
1806 i
= vsnprintf(buf
, size
, fmt
, args
);
1811 EXPORT_SYMBOL(snprintf
);
1814 * scnprintf - Format a string and place it in a buffer
1815 * @buf: The buffer to place the result into
1816 * @size: The size of the buffer, including the trailing null space
1817 * @fmt: The format string to use
1818 * @...: Arguments for the format string
1820 * The return value is the number of characters written into @buf not including
1821 * the trailing '\0'. If @size is == 0 the function returns 0.
1824 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
1829 va_start(args
, fmt
);
1830 i
= vscnprintf(buf
, size
, fmt
, args
);
1835 EXPORT_SYMBOL(scnprintf
);
1838 * vsprintf - Format a string and place it in a buffer
1839 * @buf: The buffer to place the result into
1840 * @fmt: The format string to use
1841 * @args: Arguments for the format string
1843 * The function returns the number of characters written
1844 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1847 * If you're not already dealing with a va_list consider using sprintf().
1849 * See the vsnprintf() documentation for format string extensions over C99.
1851 int vsprintf(char *buf
, const char *fmt
, va_list args
)
1853 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
1855 EXPORT_SYMBOL(vsprintf
);
1858 * sprintf - Format a string and place it in a buffer
1859 * @buf: The buffer to place the result into
1860 * @fmt: The format string to use
1861 * @...: Arguments for the format string
1863 * The function returns the number of characters written
1864 * into @buf. Use snprintf() or scnprintf() in order to avoid
1867 * See the vsnprintf() documentation for format string extensions over C99.
1869 int sprintf(char *buf
, const char *fmt
, ...)
1874 va_start(args
, fmt
);
1875 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
1880 EXPORT_SYMBOL(sprintf
);
1882 #ifdef CONFIG_BINARY_PRINTF
1885 * vbin_printf() - VA arguments to binary data
1886 * bstr_printf() - Binary data to text string
1890 * vbin_printf - Parse a format string and place args' binary value in a buffer
1891 * @bin_buf: The buffer to place args' binary value
1892 * @size: The size of the buffer(by words(32bits), not characters)
1893 * @fmt: The format string to use
1894 * @args: Arguments for the format string
1896 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1899 * The return value is the number of words(32bits) which would be generated for
1903 * If the return value is greater than @size, the resulting bin_buf is NOT
1904 * valid for bstr_printf().
1906 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
1908 struct printf_spec spec
= {0};
1911 str
= (char *)bin_buf
;
1912 end
= (char *)(bin_buf
+ size
);
1914 #define save_arg(type) \
1916 if (sizeof(type) == 8) { \
1917 unsigned long long value; \
1918 str = PTR_ALIGN(str, sizeof(u32)); \
1919 value = va_arg(args, unsigned long long); \
1920 if (str + sizeof(type) <= end) { \
1921 *(u32 *)str = *(u32 *)&value; \
1922 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1925 unsigned long value; \
1926 str = PTR_ALIGN(str, sizeof(type)); \
1927 value = va_arg(args, int); \
1928 if (str + sizeof(type) <= end) \
1929 *(typeof(type) *)str = (type)value; \
1931 str += sizeof(type); \
1935 int read
= format_decode(fmt
, &spec
);
1939 switch (spec
.type
) {
1940 case FORMAT_TYPE_NONE
:
1941 case FORMAT_TYPE_INVALID
:
1942 case FORMAT_TYPE_PERCENT_CHAR
:
1945 case FORMAT_TYPE_WIDTH
:
1946 case FORMAT_TYPE_PRECISION
:
1950 case FORMAT_TYPE_CHAR
:
1954 case FORMAT_TYPE_STR
: {
1955 const char *save_str
= va_arg(args
, char *);
1958 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
1959 || (unsigned long)save_str
< PAGE_SIZE
)
1960 save_str
= "(null)";
1961 len
= strlen(save_str
) + 1;
1962 if (str
+ len
< end
)
1963 memcpy(str
, save_str
, len
);
1968 case FORMAT_TYPE_PTR
:
1970 /* skip all alphanumeric pointer suffixes */
1971 while (isalnum(*fmt
))
1975 case FORMAT_TYPE_NRCHARS
: {
1976 /* skip %n 's argument */
1977 u8 qualifier
= spec
.qualifier
;
1979 if (qualifier
== 'l')
1980 skip_arg
= va_arg(args
, long *);
1981 else if (_tolower(qualifier
) == 'z')
1982 skip_arg
= va_arg(args
, size_t *);
1984 skip_arg
= va_arg(args
, int *);
1989 switch (spec
.type
) {
1991 case FORMAT_TYPE_LONG_LONG
:
1992 save_arg(long long);
1994 case FORMAT_TYPE_ULONG
:
1995 case FORMAT_TYPE_LONG
:
1996 save_arg(unsigned long);
1998 case FORMAT_TYPE_SIZE_T
:
2001 case FORMAT_TYPE_PTRDIFF
:
2002 save_arg(ptrdiff_t);
2004 case FORMAT_TYPE_UBYTE
:
2005 case FORMAT_TYPE_BYTE
:
2008 case FORMAT_TYPE_USHORT
:
2009 case FORMAT_TYPE_SHORT
:
2018 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2021 EXPORT_SYMBOL_GPL(vbin_printf
);
2024 * bstr_printf - Format a string from binary arguments and place it in a buffer
2025 * @buf: The buffer to place the result into
2026 * @size: The size of the buffer, including the trailing null space
2027 * @fmt: The format string to use
2028 * @bin_buf: Binary arguments for the format string
2030 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2031 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2032 * a binary buffer that generated by vbin_printf.
2034 * The format follows C99 vsnprintf, but has some extensions:
2035 * see vsnprintf comment for details.
2037 * The return value is the number of characters which would
2038 * be generated for the given input, excluding the trailing
2039 * '\0', as per ISO C99. If you want to have the exact
2040 * number of characters written into @buf as return value
2041 * (not including the trailing '\0'), use vscnprintf(). If the
2042 * return is greater than or equal to @size, the resulting
2043 * string is truncated.
2045 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2047 struct printf_spec spec
= {0};
2049 const char *args
= (const char *)bin_buf
;
2051 if (WARN_ON_ONCE((int) size
< 0))
2057 #define get_arg(type) \
2059 typeof(type) value; \
2060 if (sizeof(type) == 8) { \
2061 args = PTR_ALIGN(args, sizeof(u32)); \
2062 *(u32 *)&value = *(u32 *)args; \
2063 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2065 args = PTR_ALIGN(args, sizeof(type)); \
2066 value = *(typeof(type) *)args; \
2068 args += sizeof(type); \
2072 /* Make sure end is always >= buf */
2079 const char *old_fmt
= fmt
;
2080 int read
= format_decode(fmt
, &spec
);
2084 switch (spec
.type
) {
2085 case FORMAT_TYPE_NONE
: {
2088 if (copy
> end
- str
)
2090 memcpy(str
, old_fmt
, copy
);
2096 case FORMAT_TYPE_WIDTH
:
2097 spec
.field_width
= get_arg(int);
2100 case FORMAT_TYPE_PRECISION
:
2101 spec
.precision
= get_arg(int);
2104 case FORMAT_TYPE_CHAR
: {
2107 if (!(spec
.flags
& LEFT
)) {
2108 while (--spec
.field_width
> 0) {
2114 c
= (unsigned char) get_arg(char);
2118 while (--spec
.field_width
> 0) {
2126 case FORMAT_TYPE_STR
: {
2127 const char *str_arg
= args
;
2128 args
+= strlen(str_arg
) + 1;
2129 str
= string(str
, end
, (char *)str_arg
, spec
);
2133 case FORMAT_TYPE_PTR
:
2134 str
= pointer(fmt
+1, str
, end
, get_arg(void *), spec
);
2135 while (isalnum(*fmt
))
2139 case FORMAT_TYPE_PERCENT_CHAR
:
2140 case FORMAT_TYPE_INVALID
:
2146 case FORMAT_TYPE_NRCHARS
:
2151 unsigned long long num
;
2153 switch (spec
.type
) {
2155 case FORMAT_TYPE_LONG_LONG
:
2156 num
= get_arg(long long);
2158 case FORMAT_TYPE_ULONG
:
2159 case FORMAT_TYPE_LONG
:
2160 num
= get_arg(unsigned long);
2162 case FORMAT_TYPE_SIZE_T
:
2163 num
= get_arg(size_t);
2165 case FORMAT_TYPE_PTRDIFF
:
2166 num
= get_arg(ptrdiff_t);
2168 case FORMAT_TYPE_UBYTE
:
2169 num
= get_arg(unsigned char);
2171 case FORMAT_TYPE_BYTE
:
2172 num
= get_arg(signed char);
2174 case FORMAT_TYPE_USHORT
:
2175 num
= get_arg(unsigned short);
2177 case FORMAT_TYPE_SHORT
:
2178 num
= get_arg(short);
2180 case FORMAT_TYPE_UINT
:
2181 num
= get_arg(unsigned int);
2187 str
= number(str
, end
, num
, spec
);
2189 } /* switch(spec.type) */
2201 /* the trailing null byte doesn't count towards the total */
2204 EXPORT_SYMBOL_GPL(bstr_printf
);
2207 * bprintf - Parse a format string and place args' binary value in a buffer
2208 * @bin_buf: The buffer to place args' binary value
2209 * @size: The size of the buffer(by words(32bits), not characters)
2210 * @fmt: The format string to use
2211 * @...: Arguments for the format string
2213 * The function returns the number of words(u32) written
2216 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2221 va_start(args
, fmt
);
2222 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2227 EXPORT_SYMBOL_GPL(bprintf
);
2229 #endif /* CONFIG_BINARY_PRINTF */
2232 * vsscanf - Unformat a buffer into a list of arguments
2233 * @buf: input buffer
2234 * @fmt: format of buffer
2237 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
2239 const char *str
= buf
;
2247 unsigned long long u
;
2253 /* skip any white space in format */
2254 /* white space in format matchs any amount of
2255 * white space, including none, in the input.
2257 if (isspace(*fmt
)) {
2258 fmt
= skip_spaces(++fmt
);
2259 str
= skip_spaces(str
);
2262 /* anything that is not a conversion must match exactly */
2263 if (*fmt
!= '%' && *fmt
) {
2264 if (*fmt
++ != *str
++)
2273 /* skip this conversion.
2274 * advance both strings to next white space
2279 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
)
2281 while (!isspace(*str
) && *str
)
2286 /* get field width */
2288 if (isdigit(*fmt
)) {
2289 field_width
= skip_atoi(&fmt
);
2290 if (field_width
<= 0)
2294 /* get conversion qualifier */
2296 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2297 _tolower(*fmt
) == 'z') {
2299 if (unlikely(qualifier
== *fmt
)) {
2300 if (qualifier
== 'h') {
2303 } else if (qualifier
== 'l') {
2314 /* return number of characters read so far */
2315 *va_arg(args
, int *) = str
- buf
;
2329 char *s
= (char *)va_arg(args
, char*);
2330 if (field_width
== -1)
2334 } while (--field_width
> 0 && *str
);
2340 char *s
= (char *)va_arg(args
, char *);
2341 if (field_width
== -1)
2342 field_width
= SHRT_MAX
;
2343 /* first, skip leading white space in buffer */
2344 str
= skip_spaces(str
);
2346 /* now copy until next white space */
2347 while (*str
&& !isspace(*str
) && field_width
--)
2367 /* looking for '%' in str */
2372 /* invalid format; stop here */
2376 /* have some sort of integer conversion.
2377 * first, skip white space in buffer.
2379 str
= skip_spaces(str
);
2382 if (is_sign
&& digit
== '-')
2386 || (base
== 16 && !isxdigit(digit
))
2387 || (base
== 10 && !isdigit(digit
))
2388 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
2389 || (base
== 0 && !isdigit(digit
)))
2393 val
.s
= qualifier
!= 'L' ?
2394 simple_strtol(str
, &next
, base
) :
2395 simple_strtoll(str
, &next
, base
);
2397 val
.u
= qualifier
!= 'L' ?
2398 simple_strtoul(str
, &next
, base
) :
2399 simple_strtoull(str
, &next
, base
);
2401 if (field_width
> 0 && next
- str
> field_width
) {
2403 _parse_integer_fixup_radix(str
, &base
);
2404 while (next
- str
> field_width
) {
2406 val
.s
= div_s64(val
.s
, base
);
2408 val
.u
= div_u64(val
.u
, base
);
2413 switch (qualifier
) {
2414 case 'H': /* that's 'hh' in format */
2416 *va_arg(args
, signed char *) = val
.s
;
2418 *va_arg(args
, unsigned char *) = val
.u
;
2422 *va_arg(args
, short *) = val
.s
;
2424 *va_arg(args
, unsigned short *) = val
.u
;
2428 *va_arg(args
, long *) = val
.s
;
2430 *va_arg(args
, unsigned long *) = val
.u
;
2434 *va_arg(args
, long long *) = val
.s
;
2436 *va_arg(args
, unsigned long long *) = val
.u
;
2440 *va_arg(args
, size_t *) = val
.u
;
2444 *va_arg(args
, int *) = val
.s
;
2446 *va_arg(args
, unsigned int *) = val
.u
;
2458 EXPORT_SYMBOL(vsscanf
);
2461 * sscanf - Unformat a buffer into a list of arguments
2462 * @buf: input buffer
2463 * @fmt: formatting of buffer
2464 * @...: resulting arguments
2466 int sscanf(const char *buf
, const char *fmt
, ...)
2471 va_start(args
, fmt
);
2472 i
= vsscanf(buf
, fmt
, args
);
2477 EXPORT_SYMBOL(sscanf
);