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/clk.h>
21 #include <linux/clk-provider.h>
22 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/kernel.h>
27 #include <linux/kallsyms.h>
28 #include <linux/math64.h>
29 #include <linux/uaccess.h>
30 #include <linux/ioport.h>
31 #include <linux/dcache.h>
32 #include <linux/cred.h>
33 #include <linux/uuid.h>
35 #include <net/addrconf.h>
37 #include <linux/blkdev.h>
40 #include "../mm/internal.h" /* For the trace_print_flags arrays */
42 #include <asm/page.h> /* for PAGE_SIZE */
43 #include <asm/sections.h> /* for dereference_function_descriptor() */
44 #include <asm/byteorder.h> /* cpu_to_le16 */
46 #include <linux/string_helpers.h>
50 * simple_strtoull - convert a string to an unsigned long long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
55 * This function is obsolete. Please use kstrtoull instead.
57 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
59 unsigned long long result
;
62 cp
= _parse_integer_fixup_radix(cp
, &base
);
63 rv
= _parse_integer(cp
, base
, &result
);
65 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
72 EXPORT_SYMBOL(simple_strtoull
);
75 * simple_strtoul - convert a string to an unsigned long
76 * @cp: The start of the string
77 * @endp: A pointer to the end of the parsed string will be placed here
78 * @base: The number base to use
80 * This function is obsolete. Please use kstrtoul instead.
82 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
84 return simple_strtoull(cp
, endp
, base
);
86 EXPORT_SYMBOL(simple_strtoul
);
89 * simple_strtol - convert a string to a signed long
90 * @cp: The start of the string
91 * @endp: A pointer to the end of the parsed string will be placed here
92 * @base: The number base to use
94 * This function is obsolete. Please use kstrtol instead.
96 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
99 return -simple_strtoul(cp
+ 1, endp
, base
);
101 return simple_strtoul(cp
, endp
, base
);
103 EXPORT_SYMBOL(simple_strtol
);
106 * simple_strtoll - convert a string to a signed long long
107 * @cp: The start of the string
108 * @endp: A pointer to the end of the parsed string will be placed here
109 * @base: The number base to use
111 * This function is obsolete. Please use kstrtoll instead.
113 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
116 return -simple_strtoull(cp
+ 1, endp
, base
);
118 return simple_strtoull(cp
, endp
, base
);
120 EXPORT_SYMBOL(simple_strtoll
);
122 static noinline_for_stack
123 int skip_atoi(const char **s
)
128 i
= i
*10 + *((*s
)++) - '0';
129 } while (isdigit(**s
));
135 * Decimal conversion is by far the most typical, and is used for
136 * /proc and /sys data. This directly impacts e.g. top performance
137 * with many processes running. We optimize it for speed by emitting
138 * two characters at a time, using a 200 byte lookup table. This
139 * roughly halves the number of multiplications compared to computing
140 * the digits one at a time. Implementation strongly inspired by the
141 * previous version, which in turn used ideas described at
142 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
143 * from the author, Douglas W. Jones).
145 * It turns out there is precisely one 26 bit fixed-point
146 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
147 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
148 * range happens to be somewhat larger (x <= 1073741898), but that's
149 * irrelevant for our purpose.
151 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
152 * need a 32x32->64 bit multiply, so we simply use the same constant.
154 * For dividing a number in the range [100, 10^4-1] by 100, there are
155 * several options. The simplest is (x * 0x147b) >> 19, which is valid
156 * for all x <= 43698.
159 static const u16 decpair
[100] = {
160 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
161 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
162 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
163 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
164 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
165 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
166 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
167 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
168 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
169 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
170 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
175 * This will print a single '0' even if r == 0, since we would
176 * immediately jump to out_r where two 0s would be written but only
177 * one of them accounted for in buf. This is needed by ip4_string
178 * below. All other callers pass a non-zero value of r.
180 static noinline_for_stack
181 char *put_dec_trunc8(char *buf
, unsigned r
)
189 /* 100 <= r < 10^8 */
190 q
= (r
* (u64
)0x28f5c29) >> 32;
191 *((u16
*)buf
) = decpair
[r
- 100*q
];
198 /* 100 <= q < 10^6 */
199 r
= (q
* (u64
)0x28f5c29) >> 32;
200 *((u16
*)buf
) = decpair
[q
- 100*r
];
207 /* 100 <= r < 10^4 */
208 q
= (r
* 0x147b) >> 19;
209 *((u16
*)buf
) = decpair
[r
- 100*q
];
216 *((u16
*)buf
) = decpair
[r
];
217 buf
+= r
< 10 ? 1 : 2;
221 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
222 static noinline_for_stack
223 char *put_dec_full8(char *buf
, unsigned r
)
228 q
= (r
* (u64
)0x28f5c29) >> 32;
229 *((u16
*)buf
) = decpair
[r
- 100*q
];
233 r
= (q
* (u64
)0x28f5c29) >> 32;
234 *((u16
*)buf
) = decpair
[q
- 100*r
];
238 q
= (r
* 0x147b) >> 19;
239 *((u16
*)buf
) = decpair
[r
- 100*q
];
243 *((u16
*)buf
) = decpair
[q
];
248 static noinline_for_stack
249 char *put_dec(char *buf
, unsigned long long n
)
251 if (n
>= 100*1000*1000)
252 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
253 /* 1 <= n <= 1.6e11 */
254 if (n
>= 100*1000*1000)
255 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
257 return put_dec_trunc8(buf
, n
);
260 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
263 put_dec_full4(char *buf
, unsigned r
)
268 q
= (r
* 0x147b) >> 19;
269 *((u16
*)buf
) = decpair
[r
- 100*q
];
272 *((u16
*)buf
) = decpair
[q
];
276 * Call put_dec_full4 on x % 10000, return x / 10000.
277 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
278 * holds for all x < 1,128,869,999. The largest value this
279 * helper will ever be asked to convert is 1,125,520,955.
280 * (second call in the put_dec code, assuming n is all-ones).
282 static noinline_for_stack
283 unsigned put_dec_helper4(char *buf
, unsigned x
)
285 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
287 put_dec_full4(buf
, x
- q
* 10000);
291 /* Based on code by Douglas W. Jones found at
292 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
293 * (with permission from the author).
294 * Performs no 64-bit division and hence should be fast on 32-bit machines.
297 char *put_dec(char *buf
, unsigned long long n
)
299 uint32_t d3
, d2
, d1
, q
, h
;
301 if (n
< 100*1000*1000)
302 return put_dec_trunc8(buf
, n
);
304 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
307 d3
= (h
>> 16); /* implicit "& 0xffff" */
309 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
310 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
311 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
312 q
= put_dec_helper4(buf
, q
);
314 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
315 q
= put_dec_helper4(buf
+4, q
);
317 q
+= 4749 * d3
+ 42 * d2
;
318 q
= put_dec_helper4(buf
+8, q
);
323 buf
= put_dec_trunc8(buf
, q
);
324 else while (buf
[-1] == '0')
333 * Convert passed number to decimal string.
334 * Returns the length of string. On buffer overflow, returns 0.
336 * If speed is not important, use snprintf(). It's easy to read the code.
338 int num_to_str(char *buf
, int size
, unsigned long long num
)
340 /* put_dec requires 2-byte alignment of the buffer. */
341 char tmp
[sizeof(num
) * 3] __aligned(2);
344 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
349 len
= put_dec(tmp
, num
) - tmp
;
354 for (idx
= 0; idx
< len
; ++idx
)
355 buf
[idx
] = tmp
[len
- idx
- 1];
359 #define SIGN 1 /* unsigned/signed, must be 1 */
360 #define LEFT 2 /* left justified */
361 #define PLUS 4 /* show plus */
362 #define SPACE 8 /* space if plus */
363 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
364 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
365 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
368 FORMAT_TYPE_NONE
, /* Just a string part */
370 FORMAT_TYPE_PRECISION
,
374 FORMAT_TYPE_PERCENT_CHAR
,
376 FORMAT_TYPE_LONG_LONG
,
390 unsigned int type
:8; /* format_type enum */
391 signed int field_width
:24; /* width of output field */
392 unsigned int flags
:8; /* flags to number() */
393 unsigned int base
:8; /* number base, 8, 10 or 16 only */
394 signed int precision
:16; /* # of digits/chars */
396 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
397 #define PRECISION_MAX ((1 << 15) - 1)
399 static noinline_for_stack
400 char *number(char *buf
, char *end
, unsigned long long num
,
401 struct printf_spec spec
)
403 /* put_dec requires 2-byte alignment of the buffer. */
404 char tmp
[3 * sizeof(num
)] __aligned(2);
407 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
409 bool is_zero
= num
== 0LL;
410 int field_width
= spec
.field_width
;
411 int precision
= spec
.precision
;
413 BUILD_BUG_ON(sizeof(struct printf_spec
) != 8);
415 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
416 * produces same digits or (maybe lowercased) letters */
417 locase
= (spec
.flags
& SMALL
);
418 if (spec
.flags
& LEFT
)
419 spec
.flags
&= ~ZEROPAD
;
421 if (spec
.flags
& SIGN
) {
422 if ((signed long long)num
< 0) {
424 num
= -(signed long long)num
;
426 } else if (spec
.flags
& PLUS
) {
429 } else if (spec
.flags
& SPACE
) {
441 /* generate full string in tmp[], in reverse order */
444 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
445 else if (spec
.base
!= 10) { /* 8 or 16 */
446 int mask
= spec
.base
- 1;
452 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
455 } else { /* base 10 */
456 i
= put_dec(tmp
, num
) - tmp
;
459 /* printing 100 using %2d gives "100", not "00" */
462 /* leading space padding */
463 field_width
-= precision
;
464 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
465 while (--field_width
>= 0) {
477 /* "0x" / "0" prefix */
479 if (spec
.base
== 16 || !is_zero
) {
484 if (spec
.base
== 16) {
486 *buf
= ('X' | locase
);
490 /* zero or space padding */
491 if (!(spec
.flags
& LEFT
)) {
492 char c
= ' ' + (spec
.flags
& ZEROPAD
);
493 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
494 while (--field_width
>= 0) {
500 /* hmm even more zero padding? */
501 while (i
<= --precision
) {
506 /* actual digits of result */
512 /* trailing space padding */
513 while (--field_width
>= 0) {
522 static noinline_for_stack
523 char *special_hex_number(char *buf
, char *end
, unsigned long long num
, int size
)
525 struct printf_spec spec
;
527 spec
.type
= FORMAT_TYPE_PTR
;
528 spec
.field_width
= 2 + 2 * size
; /* 0x + hex */
529 spec
.flags
= SPECIAL
| SMALL
| ZEROPAD
;
533 return number(buf
, end
, num
, spec
);
536 static void move_right(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
);
555 * Handle field width padding for a string.
556 * @buf: current buffer position
557 * @n: length of string
558 * @end: end of output buffer
559 * @spec: for field width and flags
560 * Returns: new buffer position after padding.
562 static noinline_for_stack
563 char *widen_string(char *buf
, int n
, char *end
, struct printf_spec spec
)
567 if (likely(n
>= spec
.field_width
))
569 /* we want to pad the sucker */
570 spaces
= spec
.field_width
- n
;
571 if (!(spec
.flags
& LEFT
)) {
572 move_right(buf
- n
, end
, n
, spaces
);
583 static noinline_for_stack
584 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
587 size_t lim
= spec
.precision
;
589 if ((unsigned long)s
< PAGE_SIZE
)
601 return widen_string(buf
, len
, end
, spec
);
604 static noinline_for_stack
605 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
608 const char *array
[4], *s
;
609 const struct dentry
*p
;
614 case '2': case '3': case '4':
615 depth
= fmt
[1] - '0';
622 for (i
= 0; i
< depth
; i
++, d
= p
) {
623 p
= ACCESS_ONCE(d
->d_parent
);
624 array
[i
] = ACCESS_ONCE(d
->d_name
.name
);
633 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
645 return widen_string(buf
, n
, end
, spec
);
649 static noinline_for_stack
650 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
651 struct printf_spec spec
, const char *fmt
)
653 struct gendisk
*hd
= bdev
->bd_disk
;
655 buf
= string(buf
, end
, hd
->disk_name
, spec
);
656 if (bdev
->bd_part
->partno
) {
657 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
662 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
668 static noinline_for_stack
669 char *symbol_string(char *buf
, char *end
, void *ptr
,
670 struct printf_spec spec
, const char *fmt
)
673 #ifdef CONFIG_KALLSYMS
674 char sym
[KSYM_SYMBOL_LEN
];
678 ptr
= __builtin_extract_return_addr(ptr
);
679 value
= (unsigned long)ptr
;
681 #ifdef CONFIG_KALLSYMS
683 sprint_backtrace(sym
, value
);
684 else if (*fmt
!= 'f' && *fmt
!= 's')
685 sprint_symbol(sym
, value
);
687 sprint_symbol_no_offset(sym
, value
);
689 return string(buf
, end
, sym
, spec
);
691 return special_hex_number(buf
, end
, value
, sizeof(void *));
695 static noinline_for_stack
696 char *resource_string(char *buf
, char *end
, struct resource
*res
,
697 struct printf_spec spec
, const char *fmt
)
699 #ifndef IO_RSRC_PRINTK_SIZE
700 #define IO_RSRC_PRINTK_SIZE 6
703 #ifndef MEM_RSRC_PRINTK_SIZE
704 #define MEM_RSRC_PRINTK_SIZE 10
706 static const struct printf_spec io_spec
= {
708 .field_width
= IO_RSRC_PRINTK_SIZE
,
710 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
712 static const struct printf_spec mem_spec
= {
714 .field_width
= MEM_RSRC_PRINTK_SIZE
,
716 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
718 static const struct printf_spec bus_spec
= {
722 .flags
= SMALL
| ZEROPAD
,
724 static const struct printf_spec dec_spec
= {
729 static const struct printf_spec str_spec
= {
734 static const struct printf_spec flag_spec
= {
737 .flags
= SPECIAL
| SMALL
,
740 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
741 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
742 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
743 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
744 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
745 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
746 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
747 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
749 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
750 int decode
= (fmt
[0] == 'R') ? 1 : 0;
751 const struct printf_spec
*specp
;
754 if (res
->flags
& IORESOURCE_IO
) {
755 p
= string(p
, pend
, "io ", str_spec
);
757 } else if (res
->flags
& IORESOURCE_MEM
) {
758 p
= string(p
, pend
, "mem ", str_spec
);
760 } else if (res
->flags
& IORESOURCE_IRQ
) {
761 p
= string(p
, pend
, "irq ", str_spec
);
763 } else if (res
->flags
& IORESOURCE_DMA
) {
764 p
= string(p
, pend
, "dma ", str_spec
);
766 } else if (res
->flags
& IORESOURCE_BUS
) {
767 p
= string(p
, pend
, "bus ", str_spec
);
770 p
= string(p
, pend
, "??? ", str_spec
);
774 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
775 p
= string(p
, pend
, "size ", str_spec
);
776 p
= number(p
, pend
, resource_size(res
), *specp
);
778 p
= number(p
, pend
, res
->start
, *specp
);
779 if (res
->start
!= res
->end
) {
781 p
= number(p
, pend
, res
->end
, *specp
);
785 if (res
->flags
& IORESOURCE_MEM_64
)
786 p
= string(p
, pend
, " 64bit", str_spec
);
787 if (res
->flags
& IORESOURCE_PREFETCH
)
788 p
= string(p
, pend
, " pref", str_spec
);
789 if (res
->flags
& IORESOURCE_WINDOW
)
790 p
= string(p
, pend
, " window", str_spec
);
791 if (res
->flags
& IORESOURCE_DISABLED
)
792 p
= string(p
, pend
, " disabled", str_spec
);
794 p
= string(p
, pend
, " flags ", str_spec
);
795 p
= number(p
, pend
, res
->flags
, flag_spec
);
800 return string(buf
, end
, sym
, spec
);
803 static noinline_for_stack
804 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
807 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
808 negative value, fallback to the default */
811 if (spec
.field_width
== 0)
812 /* nothing to print */
815 if (ZERO_OR_NULL_PTR(addr
))
817 return string(buf
, end
, NULL
, spec
);
834 if (spec
.field_width
> 0)
835 len
= min_t(int, spec
.field_width
, 64);
837 for (i
= 0; i
< len
; ++i
) {
839 *buf
= hex_asc_hi(addr
[i
]);
842 *buf
= hex_asc_lo(addr
[i
]);
845 if (separator
&& i
!= len
- 1) {
855 static noinline_for_stack
856 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
857 struct printf_spec spec
, const char *fmt
)
859 const int CHUNKSZ
= 32;
860 int nr_bits
= max_t(int, spec
.field_width
, 0);
864 /* reused to print numbers */
865 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
867 chunksz
= nr_bits
& (CHUNKSZ
- 1);
871 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
872 for (; i
>= 0; i
-= CHUNKSZ
) {
876 chunkmask
= ((1ULL << chunksz
) - 1);
877 word
= i
/ BITS_PER_LONG
;
878 bit
= i
% BITS_PER_LONG
;
879 val
= (bitmap
[word
] >> bit
) & chunkmask
;
888 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
889 buf
= number(buf
, end
, val
, spec
);
896 static noinline_for_stack
897 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
898 struct printf_spec spec
, const char *fmt
)
900 int nr_bits
= max_t(int, spec
.field_width
, 0);
901 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
905 /* reused to print numbers */
906 spec
= (struct printf_spec
){ .base
= 10 };
908 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
909 while (cur
< nr_bits
) {
911 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
912 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
922 buf
= number(buf
, end
, rbot
, spec
);
928 buf
= number(buf
, end
, rtop
, spec
);
936 static noinline_for_stack
937 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
938 struct printf_spec spec
, const char *fmt
)
940 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
944 bool reversed
= false;
960 for (i
= 0; i
< 6; i
++) {
962 p
= hex_byte_pack(p
, addr
[5 - i
]);
964 p
= hex_byte_pack(p
, addr
[i
]);
966 if (fmt
[0] == 'M' && i
!= 5)
971 return string(buf
, end
, mac_addr
, spec
);
974 static noinline_for_stack
975 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
978 bool leading_zeros
= (fmt
[0] == 'i');
1003 for (i
= 0; i
< 4; i
++) {
1004 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1005 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1006 if (leading_zeros
) {
1012 /* reverse the digits in the quad */
1014 *p
++ = temp
[digits
];
1024 static noinline_for_stack
1025 char *ip6_compressed_string(char *p
, const char *addr
)
1028 unsigned char zerolength
[8];
1033 bool needcolon
= false;
1035 struct in6_addr in6
;
1037 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1039 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1041 memset(zerolength
, 0, sizeof(zerolength
));
1048 /* find position of longest 0 run */
1049 for (i
= 0; i
< range
; i
++) {
1050 for (j
= i
; j
< range
; j
++) {
1051 if (in6
.s6_addr16
[j
] != 0)
1056 for (i
= 0; i
< range
; i
++) {
1057 if (zerolength
[i
] > longest
) {
1058 longest
= zerolength
[i
];
1062 if (longest
== 1) /* don't compress a single 0 */
1066 for (i
= 0; i
< range
; i
++) {
1067 if (i
== colonpos
) {
1068 if (needcolon
|| i
== 0)
1079 /* hex u16 without leading 0s */
1080 word
= ntohs(in6
.s6_addr16
[i
]);
1085 p
= hex_byte_pack(p
, hi
);
1087 *p
++ = hex_asc_lo(hi
);
1088 p
= hex_byte_pack(p
, lo
);
1091 p
= hex_byte_pack(p
, lo
);
1093 *p
++ = hex_asc_lo(lo
);
1100 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1107 static noinline_for_stack
1108 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1112 for (i
= 0; i
< 8; i
++) {
1113 p
= hex_byte_pack(p
, *addr
++);
1114 p
= hex_byte_pack(p
, *addr
++);
1115 if (fmt
[0] == 'I' && i
!= 7)
1123 static noinline_for_stack
1124 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1125 struct printf_spec spec
, const char *fmt
)
1127 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1129 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1130 ip6_compressed_string(ip6_addr
, addr
);
1132 ip6_string(ip6_addr
, addr
, fmt
);
1134 return string(buf
, end
, ip6_addr
, spec
);
1137 static noinline_for_stack
1138 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1139 struct printf_spec spec
, const char *fmt
)
1141 char ip4_addr
[sizeof("255.255.255.255")];
1143 ip4_string(ip4_addr
, addr
, fmt
);
1145 return string(buf
, end
, ip4_addr
, spec
);
1148 static noinline_for_stack
1149 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1150 struct printf_spec spec
, const char *fmt
)
1152 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1153 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1154 sizeof(":12345") + sizeof("/123456789") +
1155 sizeof("%1234567890")];
1156 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1157 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1158 char fmt6
[2] = { fmt
[0], '6' };
1162 while (isalpha(*++fmt
)) {
1179 if (have_p
|| have_s
|| have_f
) {
1184 if (fmt6
[0] == 'I' && have_c
)
1185 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1187 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1189 if (have_p
|| have_s
|| have_f
)
1194 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1198 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1199 IPV6_FLOWINFO_MASK
), spec
);
1203 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1207 return string(buf
, end
, ip6_addr
, spec
);
1210 static noinline_for_stack
1211 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1212 struct printf_spec spec
, const char *fmt
)
1214 bool have_p
= false;
1215 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1216 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1217 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1218 char fmt4
[3] = { fmt
[0], '4', 0 };
1221 while (isalpha(*++fmt
)) {
1235 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1238 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1242 return string(buf
, end
, ip4_addr
, spec
);
1245 static noinline_for_stack
1246 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1251 unsigned int flags
= 0;
1254 if (spec
.field_width
== 0)
1255 return buf
; /* nothing to print */
1257 if (ZERO_OR_NULL_PTR(addr
))
1258 return string(buf
, end
, NULL
, spec
); /* NULL pointer */
1262 switch (fmt
[count
++]) {
1264 flags
|= ESCAPE_ANY
;
1267 flags
|= ESCAPE_SPECIAL
;
1270 flags
|= ESCAPE_HEX
;
1273 flags
|= ESCAPE_NULL
;
1276 flags
|= ESCAPE_OCTAL
;
1282 flags
|= ESCAPE_SPACE
;
1291 flags
= ESCAPE_ANY_NP
;
1293 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1296 * string_escape_mem() writes as many characters as it can to
1297 * the given buffer, and returns the total size of the output
1298 * had the buffer been big enough.
1300 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1305 static noinline_for_stack
1306 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1307 struct printf_spec spec
, const char *fmt
)
1309 char uuid
[UUID_STRING_LEN
+ 1];
1312 const u8
*index
= uuid_index
;
1317 uc
= true; /* fall-through */
1326 for (i
= 0; i
< 16; i
++) {
1328 p
= hex_byte_pack_upper(p
, addr
[index
[i
]]);
1330 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1343 return string(buf
, end
, uuid
, spec
);
1346 static noinline_for_stack
1347 char *netdev_bits(char *buf
, char *end
, const void *addr
, const char *fmt
)
1349 unsigned long long num
;
1354 num
= *(const netdev_features_t
*)addr
;
1355 size
= sizeof(netdev_features_t
);
1358 num
= (unsigned long)addr
;
1359 size
= sizeof(unsigned long);
1363 return special_hex_number(buf
, end
, num
, size
);
1366 static noinline_for_stack
1367 char *address_val(char *buf
, char *end
, const void *addr
, const char *fmt
)
1369 unsigned long long num
;
1374 num
= *(const dma_addr_t
*)addr
;
1375 size
= sizeof(dma_addr_t
);
1379 num
= *(const phys_addr_t
*)addr
;
1380 size
= sizeof(phys_addr_t
);
1384 return special_hex_number(buf
, end
, num
, size
);
1387 static noinline_for_stack
1388 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1391 if (!IS_ENABLED(CONFIG_HAVE_CLK
) || !clk
)
1392 return string(buf
, end
, NULL
, spec
);
1396 return number(buf
, end
, clk_get_rate(clk
), spec
);
1400 #ifdef CONFIG_COMMON_CLK
1401 return string(buf
, end
, __clk_get_name(clk
), spec
);
1403 return special_hex_number(buf
, end
, (unsigned long)clk
, sizeof(unsigned long));
1409 char *format_flags(char *buf
, char *end
, unsigned long flags
,
1410 const struct trace_print_flags
*names
)
1413 const struct printf_spec strspec
= {
1417 const struct printf_spec numspec
= {
1418 .flags
= SPECIAL
|SMALL
,
1424 for ( ; flags
&& names
->name
; names
++) {
1426 if ((flags
& mask
) != mask
)
1429 buf
= string(buf
, end
, names
->name
, strspec
);
1440 buf
= number(buf
, end
, flags
, numspec
);
1445 static noinline_for_stack
1446 char *flags_string(char *buf
, char *end
, void *flags_ptr
, const char *fmt
)
1448 unsigned long flags
;
1449 const struct trace_print_flags
*names
;
1453 flags
= *(unsigned long *)flags_ptr
;
1454 /* Remove zone id */
1455 flags
&= (1UL << NR_PAGEFLAGS
) - 1;
1456 names
= pageflag_names
;
1459 flags
= *(unsigned long *)flags_ptr
;
1460 names
= vmaflag_names
;
1463 flags
= *(gfp_t
*)flags_ptr
;
1464 names
= gfpflag_names
;
1467 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt
[1]);
1471 return format_flags(buf
, end
, flags
, names
);
1474 static const char *device_node_name_for_depth(const struct device_node
*np
, int depth
)
1476 for ( ; np
&& depth
; depth
--)
1479 return kbasename(np
->full_name
);
1482 static noinline_for_stack
1483 char *device_node_gen_full_name(const struct device_node
*np
, char *buf
, char *end
)
1486 const struct device_node
*parent
= np
->parent
;
1487 static const struct printf_spec strspec
= {
1492 /* special case for root node */
1494 return string(buf
, end
, "/", strspec
);
1496 for (depth
= 0; parent
->parent
; depth
++)
1497 parent
= parent
->parent
;
1499 for ( ; depth
>= 0; depth
--) {
1500 buf
= string(buf
, end
, "/", strspec
);
1501 buf
= string(buf
, end
, device_node_name_for_depth(np
, depth
),
1507 static noinline_for_stack
1508 char *device_node_string(char *buf
, char *end
, struct device_node
*dn
,
1509 struct printf_spec spec
, const char *fmt
)
1511 char tbuf
[sizeof("xxxx") + 1];
1514 char *buf_start
= buf
;
1515 struct property
*prop
;
1516 bool has_mult
, pass
;
1517 static const struct printf_spec num_spec
= {
1524 struct printf_spec str_spec
= spec
;
1525 str_spec
.field_width
= -1;
1527 if (!IS_ENABLED(CONFIG_OF
))
1528 return string(buf
, end
, "(!OF)", spec
);
1530 if ((unsigned long)dn
< PAGE_SIZE
)
1531 return string(buf
, end
, "(null)", spec
);
1533 /* simple case without anything any more format specifiers */
1535 if (fmt
[0] == '\0' || strcspn(fmt
,"fnpPFcC") > 0)
1538 for (pass
= false; strspn(fmt
,"fnpPFcC"); fmt
++, pass
= true) {
1546 case 'f': /* full_name */
1547 buf
= device_node_gen_full_name(dn
, buf
, end
);
1549 case 'n': /* name */
1550 buf
= string(buf
, end
, dn
->name
, str_spec
);
1552 case 'p': /* phandle */
1553 buf
= number(buf
, end
, (unsigned int)dn
->phandle
, num_spec
);
1555 case 'P': /* path-spec */
1556 p
= kbasename(of_node_full_name(dn
));
1559 buf
= string(buf
, end
, p
, str_spec
);
1561 case 'F': /* flags */
1562 tbuf
[0] = of_node_check_flag(dn
, OF_DYNAMIC
) ? 'D' : '-';
1563 tbuf
[1] = of_node_check_flag(dn
, OF_DETACHED
) ? 'd' : '-';
1564 tbuf
[2] = of_node_check_flag(dn
, OF_POPULATED
) ? 'P' : '-';
1565 tbuf
[3] = of_node_check_flag(dn
, OF_POPULATED_BUS
) ? 'B' : '-';
1567 buf
= string(buf
, end
, tbuf
, str_spec
);
1569 case 'c': /* major compatible string */
1570 ret
= of_property_read_string(dn
, "compatible", &p
);
1572 buf
= string(buf
, end
, p
, str_spec
);
1574 case 'C': /* full compatible string */
1576 of_property_for_each_string(dn
, "compatible", prop
, p
) {
1578 buf
= string(buf
, end
, ",", str_spec
);
1579 buf
= string(buf
, end
, "\"", str_spec
);
1580 buf
= string(buf
, end
, p
, str_spec
);
1581 buf
= string(buf
, end
, "\"", str_spec
);
1591 return widen_string(buf
, buf
- buf_start
, end
, spec
);
1594 #ifdef CONFIG_KMSG_IDS
1596 unsigned long long __jhash_string(const char *str
);
1598 static noinline_for_stack
1599 char *jhash_string(char *buf
, char *end
, const char *str
, const char *fmt
)
1601 struct printf_spec spec
;
1602 unsigned long long num
;
1604 num
= __jhash_string(str
);
1606 spec
.type
= FORMAT_TYPE_PTR
;
1607 spec
.field_width
= 6;
1608 spec
.flags
= SMALL
| ZEROPAD
;
1610 spec
.precision
= -1;
1612 return number(buf
, end
, num
, spec
);
1617 int kptr_restrict __read_mostly
;
1620 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1621 * by an extra set of alphanumeric characters that are extended format
1624 * Please update scripts/checkpatch.pl when adding/removing conversion
1625 * characters. (Search for "check for vsprintf extension").
1627 * Right now we handle:
1629 * - 'F' For symbolic function descriptor pointers with offset
1630 * - 'f' For simple symbolic function names without offset
1631 * - 'S' For symbolic direct pointers with offset
1632 * - 's' For symbolic direct pointers without offset
1633 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1634 * - 'B' For backtraced symbolic direct pointers with offset
1635 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1636 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1637 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1638 * width which must be explicitly specified either as part of the
1639 * format string '%32b[l]' or through '%*b[l]', [l] selects
1640 * range-list format instead of hex format
1641 * - 'M' For a 6-byte MAC address, it prints the address in the
1642 * usual colon-separated hex notation
1643 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1644 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1645 * with a dash-separated hex notation
1646 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1647 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1648 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1649 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1651 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1652 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1653 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1654 * IPv6 omits the colons (01020304...0f)
1655 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1657 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1658 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1659 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1660 * - 'I[6S]c' for IPv6 addresses printed as specified by
1661 * http://tools.ietf.org/html/rfc5952
1662 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1663 * of the following flags (see string_escape_mem() for the
1666 * c - ESCAPE_SPECIAL
1672 * By default ESCAPE_ANY_NP is used.
1673 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1674 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1675 * Options for %pU are:
1676 * b big endian lower case hex (default)
1677 * B big endian UPPER case hex
1678 * l little endian lower case hex
1679 * L little endian UPPER case hex
1680 * big endian output byte order is:
1681 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1682 * little endian output byte order is:
1683 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1684 * - 'V' For a struct va_format which contains a format string * and va_list *,
1685 * call vsnprintf(->format, *->va_list).
1686 * Implements a "recursive vsnprintf".
1687 * Do not use this feature without some mechanism to verify the
1688 * correctness of the format string and va_list arguments.
1689 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1690 * - 'NF' For a netdev_features_t
1691 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1692 * a certain separator (' ' by default):
1696 * The maximum supported length is 64 bytes of the input. Consider
1697 * to use print_hex_dump() for the larger input.
1698 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1699 * (default assumed to be phys_addr_t, passed by reference)
1700 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1701 * - 'D[234]' Same as 'd' but for a struct file
1702 * - 'g' For block_device name (gendisk + partition number)
1703 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1704 * (legacy clock framework) of the clock
1705 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1706 * (legacy clock framework) of the clock
1707 * - 'Cr' For a clock, it prints the current rate of the clock
1708 * - 'G' For flags to be printed as a collection of symbolic strings that would
1709 * construct the specific value. Supported flags given by option:
1710 * p page flags (see struct page) given as pointer to unsigned long
1711 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1712 * v vma flags (VM_*) given as pointer to unsigned long
1713 * - 'j' Kernel message catalog jhash for System z
1714 * - 'O' For a kobject based struct. Must be one of the following:
1715 * - 'OF[fnpPcCF]' For a device tree object
1716 * Without any optional arguments prints the full_name
1717 * f device node full_name
1718 * n device node name
1719 * p device node phandle
1720 * P device node path spec (name + @unit)
1721 * F device node flags
1722 * c major compatible string
1723 * C full compatible string
1725 * ** Please update also Documentation/printk-formats.txt when making changes **
1727 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1728 * function pointers are really function descriptors, which contain a
1729 * pointer to the real address.
1731 static noinline_for_stack
1732 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1733 struct printf_spec spec
)
1735 const int default_width
= 2 * sizeof(void *);
1737 if (!ptr
&& *fmt
!= 'K') {
1739 * Print (null) with the same width as a pointer so it makes
1740 * tabular output look nice.
1742 if (spec
.field_width
== -1)
1743 spec
.field_width
= default_width
;
1744 return string(buf
, end
, "(null)", spec
);
1750 ptr
= dereference_function_descriptor(ptr
);
1755 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1758 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1760 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1764 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
1766 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
1768 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1769 case 'm': /* Contiguous: 000102030405 */
1771 /* [mM]R (Reverse order; Bluetooth) */
1772 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1773 case 'I': /* Formatted IP supported
1775 * 6: 0001:0203:...:0708
1776 * 6c: 1::708 or 1::1.2.3.4
1778 case 'i': /* Contiguous:
1779 * 4: 001.002.003.004
1784 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1786 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1789 struct sockaddr raw
;
1790 struct sockaddr_in v4
;
1791 struct sockaddr_in6 v6
;
1794 switch (sa
->raw
.sa_family
) {
1796 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1798 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1800 return string(buf
, end
, "(invalid address)", spec
);
1805 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
1807 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1812 va_copy(va
, *((struct va_format
*)ptr
)->va
);
1813 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
1814 ((struct va_format
*)ptr
)->fmt
, va
);
1819 switch (kptr_restrict
) {
1821 /* Always print %pK values */
1824 const struct cred
*cred
;
1827 * kptr_restrict==1 cannot be used in IRQ context
1828 * because its test for CAP_SYSLOG would be meaningless.
1830 if (in_irq() || in_serving_softirq() || in_nmi()) {
1831 if (spec
.field_width
== -1)
1832 spec
.field_width
= default_width
;
1833 return string(buf
, end
, "pK-error", spec
);
1837 * Only print the real pointer value if the current
1838 * process has CAP_SYSLOG and is running with the
1839 * same credentials it started with. This is because
1840 * access to files is checked at open() time, but %pK
1841 * checks permission at read() time. We don't want to
1842 * leak pointer values if a binary opens a file using
1843 * %pK and then elevates privileges before reading it.
1845 cred
= current_cred();
1846 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
1847 !uid_eq(cred
->euid
, cred
->uid
) ||
1848 !gid_eq(cred
->egid
, cred
->gid
))
1854 /* Always print 0's for %pK */
1861 return netdev_bits(buf
, end
, ptr
, fmt
);
1863 return address_val(buf
, end
, ptr
, fmt
);
1865 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
1867 return clock(buf
, end
, ptr
, spec
, fmt
);
1869 return dentry_name(buf
, end
,
1870 ((const struct file
*)ptr
)->f_path
.dentry
,
1874 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
1878 return flags_string(buf
, end
, ptr
, fmt
);
1882 return device_node_string(buf
, end
, ptr
, spec
, fmt
+ 1);
1884 #ifdef CONFIG_KMSG_IDS
1886 return jhash_string(buf
, end
, ptr
, fmt
);
1889 spec
.flags
|= SMALL
;
1890 if (spec
.field_width
== -1) {
1891 spec
.field_width
= default_width
;
1892 spec
.flags
|= ZEROPAD
;
1896 return number(buf
, end
, (unsigned long) ptr
, spec
);
1900 * Helper function to decode printf style format.
1901 * Each call decode a token from the format and return the
1902 * number of characters read (or likely the delta where it wants
1903 * to go on the next call).
1904 * The decoded token is returned through the parameters
1906 * 'h', 'l', or 'L' for integer fields
1907 * 'z' support added 23/7/1999 S.H.
1908 * 'z' changed to 'Z' --davidm 1/25/99
1909 * 'Z' changed to 'z' --adobriyan 2017-01-25
1910 * 't' added for ptrdiff_t
1912 * @fmt: the format string
1913 * @type of the token returned
1914 * @flags: various flags such as +, -, # tokens..
1915 * @field_width: overwritten width
1916 * @base: base of the number (octal, hex, ...)
1917 * @precision: precision of a number
1918 * @qualifier: qualifier of a number (long, size_t, ...)
1920 static noinline_for_stack
1921 int format_decode(const char *fmt
, struct printf_spec
*spec
)
1923 const char *start
= fmt
;
1926 /* we finished early by reading the field width */
1927 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
1928 if (spec
->field_width
< 0) {
1929 spec
->field_width
= -spec
->field_width
;
1930 spec
->flags
|= LEFT
;
1932 spec
->type
= FORMAT_TYPE_NONE
;
1936 /* we finished early by reading the precision */
1937 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
1938 if (spec
->precision
< 0)
1939 spec
->precision
= 0;
1941 spec
->type
= FORMAT_TYPE_NONE
;
1946 spec
->type
= FORMAT_TYPE_NONE
;
1948 for (; *fmt
; ++fmt
) {
1953 /* Return the current non-format string */
1954 if (fmt
!= start
|| !*fmt
)
1960 while (1) { /* this also skips first '%' */
1966 case '-': spec
->flags
|= LEFT
; break;
1967 case '+': spec
->flags
|= PLUS
; break;
1968 case ' ': spec
->flags
|= SPACE
; break;
1969 case '#': spec
->flags
|= SPECIAL
; break;
1970 case '0': spec
->flags
|= ZEROPAD
; break;
1971 default: found
= false;
1978 /* get field width */
1979 spec
->field_width
= -1;
1982 spec
->field_width
= skip_atoi(&fmt
);
1983 else if (*fmt
== '*') {
1984 /* it's the next argument */
1985 spec
->type
= FORMAT_TYPE_WIDTH
;
1986 return ++fmt
- start
;
1990 /* get the precision */
1991 spec
->precision
= -1;
1994 if (isdigit(*fmt
)) {
1995 spec
->precision
= skip_atoi(&fmt
);
1996 if (spec
->precision
< 0)
1997 spec
->precision
= 0;
1998 } else if (*fmt
== '*') {
1999 /* it's the next argument */
2000 spec
->type
= FORMAT_TYPE_PRECISION
;
2001 return ++fmt
- start
;
2006 /* get the conversion qualifier */
2008 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2009 *fmt
== 'z' || *fmt
== 't') {
2011 if (unlikely(qualifier
== *fmt
)) {
2012 if (qualifier
== 'l') {
2015 } else if (qualifier
== 'h') {
2026 spec
->type
= FORMAT_TYPE_CHAR
;
2027 return ++fmt
- start
;
2030 spec
->type
= FORMAT_TYPE_STR
;
2031 return ++fmt
- start
;
2034 spec
->type
= FORMAT_TYPE_PTR
;
2035 return ++fmt
- start
;
2038 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
2039 return ++fmt
- start
;
2041 /* integer number formats - set up the flags and "break" */
2047 spec
->flags
|= SMALL
;
2055 spec
->flags
|= SIGN
;
2061 * Since %n poses a greater security risk than
2062 * utility, treat it as any other invalid or
2063 * unsupported format specifier.
2068 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
2069 spec
->type
= FORMAT_TYPE_INVALID
;
2073 if (qualifier
== 'L')
2074 spec
->type
= FORMAT_TYPE_LONG_LONG
;
2075 else if (qualifier
== 'l') {
2076 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
2077 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
2078 } else if (qualifier
== 'z') {
2079 spec
->type
= FORMAT_TYPE_SIZE_T
;
2080 } else if (qualifier
== 't') {
2081 spec
->type
= FORMAT_TYPE_PTRDIFF
;
2082 } else if (qualifier
== 'H') {
2083 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
2084 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
2085 } else if (qualifier
== 'h') {
2086 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
2087 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
2089 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
2090 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
2093 return ++fmt
- start
;
2097 set_field_width(struct printf_spec
*spec
, int width
)
2099 spec
->field_width
= width
;
2100 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
2101 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
2106 set_precision(struct printf_spec
*spec
, int prec
)
2108 spec
->precision
= prec
;
2109 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
2110 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
2115 * vsnprintf - Format a string and place it in a buffer
2116 * @buf: The buffer to place the result into
2117 * @size: The size of the buffer, including the trailing null space
2118 * @fmt: The format string to use
2119 * @args: Arguments for the format string
2121 * This function generally follows C99 vsnprintf, but has some
2122 * extensions and a few limitations:
2124 * - ``%n`` is unsupported
2125 * - ``%p*`` is handled by pointer()
2127 * See pointer() or Documentation/printk-formats.txt for more
2128 * extensive description.
2130 * **Please update the documentation in both places when making changes**
2132 * The return value is the number of characters which would
2133 * be generated for the given input, excluding the trailing
2134 * '\0', as per ISO C99. If you want to have the exact
2135 * number of characters written into @buf as return value
2136 * (not including the trailing '\0'), use vscnprintf(). If the
2137 * return is greater than or equal to @size, the resulting
2138 * string is truncated.
2140 * If you're not already dealing with a va_list consider using snprintf().
2142 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2144 unsigned long long num
;
2146 struct printf_spec spec
= {0};
2148 /* Reject out-of-range values early. Large positive sizes are
2149 used for unknown buffer sizes. */
2150 if (WARN_ON_ONCE(size
> INT_MAX
))
2156 /* Make sure end is always >= buf */
2163 const char *old_fmt
= fmt
;
2164 int read
= format_decode(fmt
, &spec
);
2168 switch (spec
.type
) {
2169 case FORMAT_TYPE_NONE
: {
2172 if (copy
> end
- str
)
2174 memcpy(str
, old_fmt
, copy
);
2180 case FORMAT_TYPE_WIDTH
:
2181 set_field_width(&spec
, va_arg(args
, int));
2184 case FORMAT_TYPE_PRECISION
:
2185 set_precision(&spec
, va_arg(args
, int));
2188 case FORMAT_TYPE_CHAR
: {
2191 if (!(spec
.flags
& LEFT
)) {
2192 while (--spec
.field_width
> 0) {
2199 c
= (unsigned char) va_arg(args
, int);
2203 while (--spec
.field_width
> 0) {
2211 case FORMAT_TYPE_STR
:
2212 str
= string(str
, end
, va_arg(args
, char *), spec
);
2215 case FORMAT_TYPE_PTR
:
2216 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2218 while (isalnum(*fmt
))
2222 case FORMAT_TYPE_PERCENT_CHAR
:
2228 case FORMAT_TYPE_INVALID
:
2230 * Presumably the arguments passed gcc's type
2231 * checking, but there is no safe or sane way
2232 * for us to continue parsing the format and
2233 * fetching from the va_list; the remaining
2234 * specifiers and arguments would be out of
2240 switch (spec
.type
) {
2241 case FORMAT_TYPE_LONG_LONG
:
2242 num
= va_arg(args
, long long);
2244 case FORMAT_TYPE_ULONG
:
2245 num
= va_arg(args
, unsigned long);
2247 case FORMAT_TYPE_LONG
:
2248 num
= va_arg(args
, long);
2250 case FORMAT_TYPE_SIZE_T
:
2251 if (spec
.flags
& SIGN
)
2252 num
= va_arg(args
, ssize_t
);
2254 num
= va_arg(args
, size_t);
2256 case FORMAT_TYPE_PTRDIFF
:
2257 num
= va_arg(args
, ptrdiff_t);
2259 case FORMAT_TYPE_UBYTE
:
2260 num
= (unsigned char) va_arg(args
, int);
2262 case FORMAT_TYPE_BYTE
:
2263 num
= (signed char) va_arg(args
, int);
2265 case FORMAT_TYPE_USHORT
:
2266 num
= (unsigned short) va_arg(args
, int);
2268 case FORMAT_TYPE_SHORT
:
2269 num
= (short) va_arg(args
, int);
2271 case FORMAT_TYPE_INT
:
2272 num
= (int) va_arg(args
, int);
2275 num
= va_arg(args
, unsigned int);
2278 str
= number(str
, end
, num
, spec
);
2290 /* the trailing null byte doesn't count towards the total */
2294 EXPORT_SYMBOL(vsnprintf
);
2297 * vscnprintf - Format a string and place it in a buffer
2298 * @buf: The buffer to place the result into
2299 * @size: The size of the buffer, including the trailing null space
2300 * @fmt: The format string to use
2301 * @args: Arguments for the format string
2303 * The return value is the number of characters which have been written into
2304 * the @buf not including the trailing '\0'. If @size is == 0 the function
2307 * If you're not already dealing with a va_list consider using scnprintf().
2309 * See the vsnprintf() documentation for format string extensions over C99.
2311 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2315 i
= vsnprintf(buf
, size
, fmt
, args
);
2317 if (likely(i
< size
))
2323 EXPORT_SYMBOL(vscnprintf
);
2326 * snprintf - Format a string and place it in a buffer
2327 * @buf: The buffer to place the result into
2328 * @size: The size of the buffer, including the trailing null space
2329 * @fmt: The format string to use
2330 * @...: Arguments for the format string
2332 * The return value is the number of characters which would be
2333 * generated for the given input, excluding the trailing null,
2334 * as per ISO C99. If the return is greater than or equal to
2335 * @size, the resulting string is truncated.
2337 * See the vsnprintf() documentation for format string extensions over C99.
2339 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2344 va_start(args
, fmt
);
2345 i
= vsnprintf(buf
, size
, fmt
, args
);
2350 EXPORT_SYMBOL(snprintf
);
2353 * scnprintf - Format a string and place it in a buffer
2354 * @buf: The buffer to place the result into
2355 * @size: The size of the buffer, including the trailing null space
2356 * @fmt: The format string to use
2357 * @...: Arguments for the format string
2359 * The return value is the number of characters written into @buf not including
2360 * the trailing '\0'. If @size is == 0 the function returns 0.
2363 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2368 va_start(args
, fmt
);
2369 i
= vscnprintf(buf
, size
, fmt
, args
);
2374 EXPORT_SYMBOL(scnprintf
);
2377 * vsprintf - Format a string and place it in a buffer
2378 * @buf: The buffer to place the result into
2379 * @fmt: The format string to use
2380 * @args: Arguments for the format string
2382 * The function returns the number of characters written
2383 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2386 * If you're not already dealing with a va_list consider using sprintf().
2388 * See the vsnprintf() documentation for format string extensions over C99.
2390 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2392 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2394 EXPORT_SYMBOL(vsprintf
);
2397 * sprintf - Format a string and place it in a buffer
2398 * @buf: The buffer to place the result into
2399 * @fmt: The format string to use
2400 * @...: Arguments for the format string
2402 * The function returns the number of characters written
2403 * into @buf. Use snprintf() or scnprintf() in order to avoid
2406 * See the vsnprintf() documentation for format string extensions over C99.
2408 int sprintf(char *buf
, const char *fmt
, ...)
2413 va_start(args
, fmt
);
2414 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2419 EXPORT_SYMBOL(sprintf
);
2421 #ifdef CONFIG_BINARY_PRINTF
2424 * vbin_printf() - VA arguments to binary data
2425 * bstr_printf() - Binary data to text string
2429 * vbin_printf - Parse a format string and place args' binary value in a buffer
2430 * @bin_buf: The buffer to place args' binary value
2431 * @size: The size of the buffer(by words(32bits), not characters)
2432 * @fmt: The format string to use
2433 * @args: Arguments for the format string
2435 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2438 * The return value is the number of words(32bits) which would be generated for
2442 * If the return value is greater than @size, the resulting bin_buf is NOT
2443 * valid for bstr_printf().
2445 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2447 struct printf_spec spec
= {0};
2450 str
= (char *)bin_buf
;
2451 end
= (char *)(bin_buf
+ size
);
2453 #define save_arg(type) \
2455 if (sizeof(type) == 8) { \
2456 unsigned long long value; \
2457 str = PTR_ALIGN(str, sizeof(u32)); \
2458 value = va_arg(args, unsigned long long); \
2459 if (str + sizeof(type) <= end) { \
2460 *(u32 *)str = *(u32 *)&value; \
2461 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2464 unsigned long value; \
2465 str = PTR_ALIGN(str, sizeof(type)); \
2466 value = va_arg(args, int); \
2467 if (str + sizeof(type) <= end) \
2468 *(typeof(type) *)str = (type)value; \
2470 str += sizeof(type); \
2474 int read
= format_decode(fmt
, &spec
);
2478 switch (spec
.type
) {
2479 case FORMAT_TYPE_NONE
:
2480 case FORMAT_TYPE_PERCENT_CHAR
:
2482 case FORMAT_TYPE_INVALID
:
2485 case FORMAT_TYPE_WIDTH
:
2486 case FORMAT_TYPE_PRECISION
:
2490 case FORMAT_TYPE_CHAR
:
2494 case FORMAT_TYPE_STR
: {
2495 const char *save_str
= va_arg(args
, char *);
2498 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
2499 || (unsigned long)save_str
< PAGE_SIZE
)
2500 save_str
= "(null)";
2501 len
= strlen(save_str
) + 1;
2502 if (str
+ len
< end
)
2503 memcpy(str
, save_str
, len
);
2508 case FORMAT_TYPE_PTR
:
2510 /* skip all alphanumeric pointer suffixes */
2511 while (isalnum(*fmt
))
2516 switch (spec
.type
) {
2518 case FORMAT_TYPE_LONG_LONG
:
2519 save_arg(long long);
2521 case FORMAT_TYPE_ULONG
:
2522 case FORMAT_TYPE_LONG
:
2523 save_arg(unsigned long);
2525 case FORMAT_TYPE_SIZE_T
:
2528 case FORMAT_TYPE_PTRDIFF
:
2529 save_arg(ptrdiff_t);
2531 case FORMAT_TYPE_UBYTE
:
2532 case FORMAT_TYPE_BYTE
:
2535 case FORMAT_TYPE_USHORT
:
2536 case FORMAT_TYPE_SHORT
:
2546 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2549 EXPORT_SYMBOL_GPL(vbin_printf
);
2552 * bstr_printf - Format a string from binary arguments and place it in a buffer
2553 * @buf: The buffer to place the result into
2554 * @size: The size of the buffer, including the trailing null space
2555 * @fmt: The format string to use
2556 * @bin_buf: Binary arguments for the format string
2558 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2559 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2560 * a binary buffer that generated by vbin_printf.
2562 * The format follows C99 vsnprintf, but has some extensions:
2563 * see vsnprintf comment for details.
2565 * The return value is the number of characters which would
2566 * be generated for the given input, excluding the trailing
2567 * '\0', as per ISO C99. If you want to have the exact
2568 * number of characters written into @buf as return value
2569 * (not including the trailing '\0'), use vscnprintf(). If the
2570 * return is greater than or equal to @size, the resulting
2571 * string is truncated.
2573 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2575 struct printf_spec spec
= {0};
2577 const char *args
= (const char *)bin_buf
;
2579 if (WARN_ON_ONCE(size
> INT_MAX
))
2585 #define get_arg(type) \
2587 typeof(type) value; \
2588 if (sizeof(type) == 8) { \
2589 args = PTR_ALIGN(args, sizeof(u32)); \
2590 *(u32 *)&value = *(u32 *)args; \
2591 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2593 args = PTR_ALIGN(args, sizeof(type)); \
2594 value = *(typeof(type) *)args; \
2596 args += sizeof(type); \
2600 /* Make sure end is always >= buf */
2607 const char *old_fmt
= fmt
;
2608 int read
= format_decode(fmt
, &spec
);
2612 switch (spec
.type
) {
2613 case FORMAT_TYPE_NONE
: {
2616 if (copy
> end
- str
)
2618 memcpy(str
, old_fmt
, copy
);
2624 case FORMAT_TYPE_WIDTH
:
2625 set_field_width(&spec
, get_arg(int));
2628 case FORMAT_TYPE_PRECISION
:
2629 set_precision(&spec
, get_arg(int));
2632 case FORMAT_TYPE_CHAR
: {
2635 if (!(spec
.flags
& LEFT
)) {
2636 while (--spec
.field_width
> 0) {
2642 c
= (unsigned char) get_arg(char);
2646 while (--spec
.field_width
> 0) {
2654 case FORMAT_TYPE_STR
: {
2655 const char *str_arg
= args
;
2656 args
+= strlen(str_arg
) + 1;
2657 str
= string(str
, end
, (char *)str_arg
, spec
);
2661 case FORMAT_TYPE_PTR
:
2662 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
2663 while (isalnum(*fmt
))
2667 case FORMAT_TYPE_PERCENT_CHAR
:
2673 case FORMAT_TYPE_INVALID
:
2677 unsigned long long num
;
2679 switch (spec
.type
) {
2681 case FORMAT_TYPE_LONG_LONG
:
2682 num
= get_arg(long long);
2684 case FORMAT_TYPE_ULONG
:
2685 case FORMAT_TYPE_LONG
:
2686 num
= get_arg(unsigned long);
2688 case FORMAT_TYPE_SIZE_T
:
2689 num
= get_arg(size_t);
2691 case FORMAT_TYPE_PTRDIFF
:
2692 num
= get_arg(ptrdiff_t);
2694 case FORMAT_TYPE_UBYTE
:
2695 num
= get_arg(unsigned char);
2697 case FORMAT_TYPE_BYTE
:
2698 num
= get_arg(signed char);
2700 case FORMAT_TYPE_USHORT
:
2701 num
= get_arg(unsigned short);
2703 case FORMAT_TYPE_SHORT
:
2704 num
= get_arg(short);
2706 case FORMAT_TYPE_UINT
:
2707 num
= get_arg(unsigned int);
2713 str
= number(str
, end
, num
, spec
);
2715 } /* switch(spec.type) */
2728 /* the trailing null byte doesn't count towards the total */
2731 EXPORT_SYMBOL_GPL(bstr_printf
);
2734 * bprintf - Parse a format string and place args' binary value in a buffer
2735 * @bin_buf: The buffer to place args' binary value
2736 * @size: The size of the buffer(by words(32bits), not characters)
2737 * @fmt: The format string to use
2738 * @...: Arguments for the format string
2740 * The function returns the number of words(u32) written
2743 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2748 va_start(args
, fmt
);
2749 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2754 EXPORT_SYMBOL_GPL(bprintf
);
2756 #endif /* CONFIG_BINARY_PRINTF */
2759 * vsscanf - Unformat a buffer into a list of arguments
2760 * @buf: input buffer
2761 * @fmt: format of buffer
2764 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
2766 const char *str
= buf
;
2774 unsigned long long u
;
2780 /* skip any white space in format */
2781 /* white space in format matchs any amount of
2782 * white space, including none, in the input.
2784 if (isspace(*fmt
)) {
2785 fmt
= skip_spaces(++fmt
);
2786 str
= skip_spaces(str
);
2789 /* anything that is not a conversion must match exactly */
2790 if (*fmt
!= '%' && *fmt
) {
2791 if (*fmt
++ != *str
++)
2800 /* skip this conversion.
2801 * advance both strings to next white space
2806 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
) {
2807 /* '%*[' not yet supported, invalid format */
2812 while (!isspace(*str
) && *str
)
2817 /* get field width */
2819 if (isdigit(*fmt
)) {
2820 field_width
= skip_atoi(&fmt
);
2821 if (field_width
<= 0)
2825 /* get conversion qualifier */
2827 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2830 if (unlikely(qualifier
== *fmt
)) {
2831 if (qualifier
== 'h') {
2834 } else if (qualifier
== 'l') {
2845 /* return number of characters read so far */
2846 *va_arg(args
, int *) = str
- buf
;
2860 char *s
= (char *)va_arg(args
, char*);
2861 if (field_width
== -1)
2865 } while (--field_width
> 0 && *str
);
2871 char *s
= (char *)va_arg(args
, char *);
2872 if (field_width
== -1)
2873 field_width
= SHRT_MAX
;
2874 /* first, skip leading white space in buffer */
2875 str
= skip_spaces(str
);
2877 /* now copy until next white space */
2878 while (*str
&& !isspace(*str
) && field_width
--)
2885 * Warning: This implementation of the '[' conversion specifier
2886 * deviates from its glibc counterpart in the following ways:
2887 * (1) It does NOT support ranges i.e. '-' is NOT a special
2889 * (2) It cannot match the closing bracket ']' itself
2890 * (3) A field width is required
2891 * (4) '%*[' (discard matching input) is currently not supported
2894 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2895 * buf1, buf2, buf3);
2901 char *s
= (char *)va_arg(args
, char *);
2902 DECLARE_BITMAP(set
, 256) = {0};
2903 unsigned int len
= 0;
2904 bool negate
= (*fmt
== '^');
2906 /* field width is required */
2907 if (field_width
== -1)
2913 for ( ; *fmt
&& *fmt
!= ']'; ++fmt
, ++len
)
2914 set_bit((u8
)*fmt
, set
);
2916 /* no ']' or no character set found */
2922 bitmap_complement(set
, set
, 256);
2923 /* exclude null '\0' byte */
2927 /* match must be non-empty */
2928 if (!test_bit((u8
)*str
, set
))
2931 while (test_bit((u8
)*str
, set
) && field_width
--)
2951 /* looking for '%' in str */
2956 /* invalid format; stop here */
2960 /* have some sort of integer conversion.
2961 * first, skip white space in buffer.
2963 str
= skip_spaces(str
);
2966 if (is_sign
&& digit
== '-')
2970 || (base
== 16 && !isxdigit(digit
))
2971 || (base
== 10 && !isdigit(digit
))
2972 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
2973 || (base
== 0 && !isdigit(digit
)))
2977 val
.s
= qualifier
!= 'L' ?
2978 simple_strtol(str
, &next
, base
) :
2979 simple_strtoll(str
, &next
, base
);
2981 val
.u
= qualifier
!= 'L' ?
2982 simple_strtoul(str
, &next
, base
) :
2983 simple_strtoull(str
, &next
, base
);
2985 if (field_width
> 0 && next
- str
> field_width
) {
2987 _parse_integer_fixup_radix(str
, &base
);
2988 while (next
- str
> field_width
) {
2990 val
.s
= div_s64(val
.s
, base
);
2992 val
.u
= div_u64(val
.u
, base
);
2997 switch (qualifier
) {
2998 case 'H': /* that's 'hh' in format */
3000 *va_arg(args
, signed char *) = val
.s
;
3002 *va_arg(args
, unsigned char *) = val
.u
;
3006 *va_arg(args
, short *) = val
.s
;
3008 *va_arg(args
, unsigned short *) = val
.u
;
3012 *va_arg(args
, long *) = val
.s
;
3014 *va_arg(args
, unsigned long *) = val
.u
;
3018 *va_arg(args
, long long *) = val
.s
;
3020 *va_arg(args
, unsigned long long *) = val
.u
;
3023 *va_arg(args
, size_t *) = val
.u
;
3027 *va_arg(args
, int *) = val
.s
;
3029 *va_arg(args
, unsigned int *) = val
.u
;
3041 EXPORT_SYMBOL(vsscanf
);
3044 * sscanf - Unformat a buffer into a list of arguments
3045 * @buf: input buffer
3046 * @fmt: formatting of buffer
3047 * @...: resulting arguments
3049 int sscanf(const char *buf
, const char *fmt
, ...)
3054 va_start(args
, fmt
);
3055 i
= vsscanf(buf
, fmt
, args
);
3060 EXPORT_SYMBOL(sscanf
);