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>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/ctype.h>
24 #include <linux/kernel.h>
25 #include <linux/kallsyms.h>
26 #include <linux/uaccess.h>
27 #include <linux/ioport.h>
28 #include <net/addrconf.h>
30 #include <asm/page.h> /* for PAGE_SIZE */
31 #include <asm/div64.h>
32 #include <asm/sections.h> /* for dereference_function_descriptor() */
34 /* Works only for digits and letters, but small and fast */
35 #define TOLOWER(x) ((x) | 0x20)
37 static unsigned int simple_guess_base(const char *cp
)
40 if (TOLOWER(cp
[1]) == 'x' && isxdigit(cp
[2]))
50 * simple_strtoul - convert a string to an unsigned 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 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
57 unsigned long result
= 0;
60 base
= simple_guess_base(cp
);
62 if (base
== 16 && cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
65 while (isxdigit(*cp
)) {
68 value
= isdigit(*cp
) ? *cp
- '0' : TOLOWER(*cp
) - 'a' + 10;
71 result
= result
* base
+ value
;
79 EXPORT_SYMBOL(simple_strtoul
);
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
87 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
90 return -simple_strtoul(cp
+ 1, endp
, base
);
91 return simple_strtoul(cp
, endp
, base
);
93 EXPORT_SYMBOL(simple_strtol
);
96 * simple_strtoull - convert a string to an unsigned long long
97 * @cp: The start of the string
98 * @endp: A pointer to the end of the parsed string will be placed here
99 * @base: The number base to use
101 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
103 unsigned long long result
= 0;
106 base
= simple_guess_base(cp
);
108 if (base
== 16 && cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
111 while (isxdigit(*cp
)) {
114 value
= isdigit(*cp
) ? *cp
- '0' : TOLOWER(*cp
) - 'a' + 10;
117 result
= result
* base
+ value
;
125 EXPORT_SYMBOL(simple_strtoull
);
128 * simple_strtoll - convert a string to a signed long long
129 * @cp: The start of the string
130 * @endp: A pointer to the end of the parsed string will be placed here
131 * @base: The number base to use
133 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
136 return -simple_strtoull(cp
+ 1, endp
, base
);
137 return simple_strtoull(cp
, endp
, base
);
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
154 * echo will append a newline to the tail.
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
162 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
173 val
= simple_strtoul(cp
, &tail
, base
);
176 if ((*tail
== '\0') ||
177 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
184 EXPORT_SYMBOL(strict_strtoul
);
187 * strict_strtol - convert a string to a long strictly
188 * @cp: The string to be converted
189 * @base: The number base to use
190 * @res: The converted result value
192 * strict_strtol is similiar to strict_strtoul, but it allows the first
193 * character of a string is '-'.
195 * It returns 0 if conversion is successful and *res is set to the converted
196 * value, otherwise it returns -EINVAL and *res is set to 0.
198 int strict_strtol(const char *cp
, unsigned int base
, long *res
)
202 ret
= strict_strtoul(cp
+ 1, base
, (unsigned long *)res
);
206 ret
= strict_strtoul(cp
, base
, (unsigned long *)res
);
211 EXPORT_SYMBOL(strict_strtol
);
214 * strict_strtoull - convert a string to an unsigned long long strictly
215 * @cp: The string to be converted
216 * @base: The number base to use
217 * @res: The converted result value
219 * strict_strtoull converts a string to an unsigned long long only if the
220 * string is really an unsigned long long string, any string containing
221 * any invalid char at the tail will be rejected and -EINVAL is returned,
222 * only a newline char at the tail is acceptible because people generally
223 * change a module parameter in the following way:
225 * echo 1024 > /sys/module/e1000/parameters/copybreak
227 * echo will append a newline to the tail of the string.
229 * It returns 0 if conversion is successful and *res is set to the converted
230 * value, otherwise it returns -EINVAL and *res is set to 0.
232 * simple_strtoull just ignores the successive invalid characters and
233 * return the converted value of prefix part of the string.
235 int strict_strtoull(const char *cp
, unsigned int base
, unsigned long long *res
)
238 unsigned long long val
;
246 val
= simple_strtoull(cp
, &tail
, base
);
249 if ((*tail
== '\0') ||
250 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
257 EXPORT_SYMBOL(strict_strtoull
);
260 * strict_strtoll - convert a string to a long long strictly
261 * @cp: The string to be converted
262 * @base: The number base to use
263 * @res: The converted result value
265 * strict_strtoll is similiar to strict_strtoull, but it allows the first
266 * character of a string is '-'.
268 * It returns 0 if conversion is successful and *res is set to the converted
269 * value, otherwise it returns -EINVAL and *res is set to 0.
271 int strict_strtoll(const char *cp
, unsigned int base
, long long *res
)
275 ret
= strict_strtoull(cp
+ 1, base
, (unsigned long long *)res
);
279 ret
= strict_strtoull(cp
, base
, (unsigned long long *)res
);
284 EXPORT_SYMBOL(strict_strtoll
);
286 static int skip_atoi(const char **s
)
291 i
= i
*10 + *((*s
)++) - '0';
295 /* Decimal conversion is by far the most typical, and is used
296 * for /proc and /sys data. This directly impacts e.g. top performance
297 * with many processes running. We optimize it for speed
299 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
300 * (with permission from the author, Douglas W. Jones). */
302 /* Formats correctly any integer in [0,99999].
303 * Outputs from one to five digits depending on input.
304 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
305 static char* put_dec_trunc(char *buf
, unsigned q
)
307 unsigned d3
, d2
, d1
, d0
;
312 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
313 q
= (d0
* 0xcd) >> 11;
315 *buf
++ = d0
+ '0'; /* least significant digit */
316 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
318 q
= (d1
* 0xcd) >> 11;
320 *buf
++ = d1
+ '0'; /* next digit */
323 if ((d2
!= 0) || (d3
!= 0)) {
326 *buf
++ = d2
+ '0'; /* next digit */
330 q
= (d3
* 0xcd) >> 11;
332 *buf
++ = d3
+ '0'; /* next digit */
334 *buf
++ = q
+ '0'; /* most sign. digit */
340 /* Same with if's removed. Always emits five digits */
341 static char* put_dec_full(char *buf
, unsigned q
)
343 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
344 /* but anyway, gcc produces better code with full-sized ints */
345 unsigned d3
, d2
, d1
, d0
;
350 /* Possible ways to approx. divide by 10 */
351 /* gcc -O2 replaces multiply with shifts and adds */
352 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
353 // (x * 0x67) >> 10: 1100111
354 // (x * 0x34) >> 9: 110100 - same
355 // (x * 0x1a) >> 8: 11010 - same
356 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
358 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
359 q
= (d0
* 0xcd) >> 11;
362 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
363 q
= (d1
* 0xcd) >> 11;
373 q
= (d3
* 0xcd) >> 11; /* - shorter code */
374 /* q = (d3 * 0x67) >> 10; - would also work */
380 /* No inlining helps gcc to use registers better */
381 static noinline
char* put_dec(char *buf
, unsigned long long num
)
386 return put_dec_trunc(buf
, num
);
387 rem
= do_div(num
, 100000);
388 buf
= put_dec_full(buf
, rem
);
392 #define ZEROPAD 1 /* pad with zero */
393 #define SIGN 2 /* unsigned/signed long */
394 #define PLUS 4 /* show plus */
395 #define SPACE 8 /* space if plus */
396 #define LEFT 16 /* left justified */
397 #define SMALL 32 /* Must be 32 == 0x20 */
398 #define SPECIAL 64 /* 0x */
401 FORMAT_TYPE_NONE
, /* Just a string part */
403 FORMAT_TYPE_PRECISION
,
407 FORMAT_TYPE_PERCENT_CHAR
,
409 FORMAT_TYPE_LONG_LONG
,
424 enum format_type type
;
425 int flags
; /* flags to number() */
426 int field_width
; /* width of output field */
428 int precision
; /* # of digits/chars */
432 static char *number(char *buf
, char *end
, unsigned long long num
,
433 struct printf_spec spec
)
435 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
436 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
441 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
446 locase
= (spec
.flags
& SMALL
);
447 if (spec
.flags
& LEFT
)
448 spec
.flags
&= ~ZEROPAD
;
450 if (spec
.flags
& SIGN
) {
451 if ((signed long long) num
< 0) {
453 num
= - (signed long long) num
;
455 } else if (spec
.flags
& PLUS
) {
458 } else if (spec
.flags
& SPACE
) {
469 /* generate full string in tmp[], in reverse order */
473 /* Generic code, for any base:
475 tmp[i++] = (digits[do_div(num,base)] | locase);
478 else if (spec
.base
!= 10) { /* 8 or 16 */
479 int mask
= spec
.base
- 1;
481 if (spec
.base
== 16) shift
= 4;
483 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
486 } else { /* base 10 */
487 i
= put_dec(tmp
, num
) - tmp
;
490 /* printing 100 using %2d gives "100", not "00" */
491 if (i
> spec
.precision
)
493 /* leading space padding */
494 spec
.field_width
-= spec
.precision
;
495 if (!(spec
.flags
& (ZEROPAD
+LEFT
))) {
496 while(--spec
.field_width
>= 0) {
508 /* "0x" / "0" prefix */
513 if (spec
.base
== 16) {
515 *buf
= ('X' | locase
);
519 /* zero or space padding */
520 if (!(spec
.flags
& LEFT
)) {
521 char c
= (spec
.flags
& ZEROPAD
) ? '0' : ' ';
522 while (--spec
.field_width
>= 0) {
528 /* hmm even more zero padding? */
529 while (i
<= --spec
.precision
) {
534 /* actual digits of result */
540 /* trailing space padding */
541 while (--spec
.field_width
>= 0) {
549 static char *string(char *buf
, char *end
, char *s
, struct printf_spec spec
)
553 if ((unsigned long)s
< PAGE_SIZE
)
556 len
= strnlen(s
, spec
.precision
);
558 if (!(spec
.flags
& LEFT
)) {
559 while (len
< spec
.field_width
--) {
565 for (i
= 0; i
< len
; ++i
) {
570 while (len
< spec
.field_width
--) {
578 static char *symbol_string(char *buf
, char *end
, void *ptr
,
579 struct printf_spec spec
, char ext
)
581 unsigned long value
= (unsigned long) ptr
;
582 #ifdef CONFIG_KALLSYMS
583 char sym
[KSYM_SYMBOL_LEN
];
585 sprint_symbol(sym
, value
);
587 kallsyms_lookup(value
, NULL
, NULL
, NULL
, sym
);
588 return string(buf
, end
, sym
, spec
);
590 spec
.field_width
= 2*sizeof(void *);
591 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
593 return number(buf
, end
, value
, spec
);
597 static char *resource_string(char *buf
, char *end
, struct resource
*res
,
598 struct printf_spec spec
)
600 #ifndef IO_RSRC_PRINTK_SIZE
601 #define IO_RSRC_PRINTK_SIZE 4
604 #ifndef MEM_RSRC_PRINTK_SIZE
605 #define MEM_RSRC_PRINTK_SIZE 8
607 struct printf_spec num_spec
= {
610 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
612 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
613 char sym
[4*sizeof(resource_size_t
) + 8];
614 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
617 if (res
->flags
& IORESOURCE_IO
)
618 size
= IO_RSRC_PRINTK_SIZE
;
619 else if (res
->flags
& IORESOURCE_MEM
)
620 size
= MEM_RSRC_PRINTK_SIZE
;
623 num_spec
.field_width
= size
;
624 p
= number(p
, pend
, res
->start
, num_spec
);
626 p
= number(p
, pend
, res
->end
, num_spec
);
630 return string(buf
, end
, sym
, spec
);
633 static char *mac_address_string(char *buf
, char *end
, u8
*addr
,
634 struct printf_spec spec
, const char *fmt
)
636 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
640 for (i
= 0; i
< 6; i
++) {
641 p
= pack_hex_byte(p
, addr
[i
]);
642 if (fmt
[0] == 'M' && i
!= 5)
647 return string(buf
, end
, mac_addr
, spec
);
650 static char *ip4_string(char *p
, const u8
*addr
, bool leading_zeros
)
654 for (i
= 0; i
< 4; i
++) {
655 char temp
[3]; /* hold each IP quad in reverse order */
656 int digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
663 /* reverse the digits in the quad */
674 static char *ip6_compressed_string(char *p
, const char *addr
)
679 unsigned char zerolength
[8];
685 bool needcolon
= false;
689 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
691 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
693 memset(zerolength
, 0, sizeof(zerolength
));
700 /* find position of longest 0 run */
701 for (i
= 0; i
< range
; i
++) {
702 for (j
= i
; j
< range
; j
++) {
703 if (in6
.s6_addr16
[j
] != 0)
708 for (i
= 0; i
< range
; i
++) {
709 if (zerolength
[i
] > longest
) {
710 longest
= zerolength
[i
];
716 for (i
= 0; i
< range
; i
++) {
718 if (needcolon
|| i
== 0)
729 /* hex u16 without leading 0s */
730 word
= ntohs(in6
.s6_addr16
[i
]);
735 p
= pack_hex_byte(p
, hi
);
737 *p
++ = hex_asc_lo(hi
);
740 p
= pack_hex_byte(p
, lo
);
742 *p
++ = hex_asc_lo(lo
);
749 p
= ip4_string(p
, &in6
.s6_addr
[12], false);
756 static char *ip6_string(char *p
, const char *addr
, const char *fmt
)
759 for (i
= 0; i
< 8; i
++) {
760 p
= pack_hex_byte(p
, *addr
++);
761 p
= pack_hex_byte(p
, *addr
++);
762 if (fmt
[0] == 'I' && i
!= 7)
770 static char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
771 struct printf_spec spec
, const char *fmt
)
773 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
775 if (fmt
[0] == 'I' && fmt
[2] == 'c')
776 ip6_compressed_string(ip6_addr
, addr
);
778 ip6_string(ip6_addr
, addr
, fmt
);
780 return string(buf
, end
, ip6_addr
, spec
);
783 static char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
784 struct printf_spec spec
, const char *fmt
)
786 char ip4_addr
[sizeof("255.255.255.255")];
788 ip4_string(ip4_addr
, addr
, fmt
[0] == 'i');
790 return string(buf
, end
, ip4_addr
, spec
);
794 * Show a '%p' thing. A kernel extension is that the '%p' is followed
795 * by an extra set of alphanumeric characters that are extended format
798 * Right now we handle:
800 * - 'F' For symbolic function descriptor pointers with offset
801 * - 'f' For simple symbolic function names without offset
802 * - 'S' For symbolic direct pointers
803 * - 'R' For a struct resource pointer, it prints the range of
804 * addresses (not the name nor the flags)
805 * - 'M' For a 6-byte MAC address, it prints the address in the
806 * usual colon-separated hex notation
807 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
808 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
809 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
810 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
811 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
812 * IPv6 omits the colons (01020304...0f)
813 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
814 * - 'I6c' for IPv6 addresses printed as specified by
815 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
816 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
817 * function pointers are really function descriptors, which contain a
818 * pointer to the real address.
820 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
821 struct printf_spec spec
)
824 return string(buf
, end
, "(null)", spec
);
829 ptr
= dereference_function_descriptor(ptr
);
832 return symbol_string(buf
, end
, ptr
, spec
, *fmt
);
834 return resource_string(buf
, end
, ptr
, spec
);
835 case 'M': /* Colon separated: 00:01:02:03:04:05 */
836 case 'm': /* Contiguous: 000102030405 */
837 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
838 case 'I': /* Formatted IP supported
840 * 6: 0001:0203:...:0708
841 * 6c: 1::708 or 1::1.2.3.4
843 case 'i': /* Contiguous:
849 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
851 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
856 if (spec
.field_width
== -1) {
857 spec
.field_width
= 2*sizeof(void *);
858 spec
.flags
|= ZEROPAD
;
862 return number(buf
, end
, (unsigned long) ptr
, spec
);
866 * Helper function to decode printf style format.
867 * Each call decode a token from the format and return the
868 * number of characters read (or likely the delta where it wants
869 * to go on the next call).
870 * The decoded token is returned through the parameters
872 * 'h', 'l', or 'L' for integer fields
873 * 'z' support added 23/7/1999 S.H.
874 * 'z' changed to 'Z' --davidm 1/25/99
875 * 't' added for ptrdiff_t
877 * @fmt: the format string
878 * @type of the token returned
879 * @flags: various flags such as +, -, # tokens..
880 * @field_width: overwritten width
881 * @base: base of the number (octal, hex, ...)
882 * @precision: precision of a number
883 * @qualifier: qualifier of a number (long, size_t, ...)
885 static int format_decode(const char *fmt
, struct printf_spec
*spec
)
887 const char *start
= fmt
;
889 /* we finished early by reading the field width */
890 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
891 if (spec
->field_width
< 0) {
892 spec
->field_width
= -spec
->field_width
;
895 spec
->type
= FORMAT_TYPE_NONE
;
899 /* we finished early by reading the precision */
900 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
901 if (spec
->precision
< 0)
904 spec
->type
= FORMAT_TYPE_NONE
;
909 spec
->type
= FORMAT_TYPE_NONE
;
911 for (; *fmt
; ++fmt
) {
916 /* Return the current non-format string */
917 if (fmt
!= start
|| !*fmt
)
923 while (1) { /* this also skips first '%' */
929 case '-': spec
->flags
|= LEFT
; break;
930 case '+': spec
->flags
|= PLUS
; break;
931 case ' ': spec
->flags
|= SPACE
; break;
932 case '#': spec
->flags
|= SPECIAL
; break;
933 case '0': spec
->flags
|= ZEROPAD
; break;
934 default: found
= false;
941 /* get field width */
942 spec
->field_width
= -1;
945 spec
->field_width
= skip_atoi(&fmt
);
946 else if (*fmt
== '*') {
947 /* it's the next argument */
948 spec
->type
= FORMAT_TYPE_WIDTH
;
949 return ++fmt
- start
;
953 /* get the precision */
954 spec
->precision
= -1;
958 spec
->precision
= skip_atoi(&fmt
);
959 if (spec
->precision
< 0)
961 } else if (*fmt
== '*') {
962 /* it's the next argument */
963 spec
->type
= FORMAT_TYPE_PRECISION
;
964 return ++fmt
- start
;
969 /* get the conversion qualifier */
970 spec
->qualifier
= -1;
971 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
972 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't') {
973 spec
->qualifier
= *fmt
++;
974 if (unlikely(spec
->qualifier
== *fmt
)) {
975 if (spec
->qualifier
== 'l') {
976 spec
->qualifier
= 'L';
978 } else if (spec
->qualifier
== 'h') {
979 spec
->qualifier
= 'H';
989 spec
->type
= FORMAT_TYPE_CHAR
;
990 return ++fmt
- start
;
993 spec
->type
= FORMAT_TYPE_STR
;
994 return ++fmt
- start
;
997 spec
->type
= FORMAT_TYPE_PTR
;
1002 spec
->type
= FORMAT_TYPE_NRCHARS
;
1003 return ++fmt
- start
;
1006 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
1007 return ++fmt
- start
;
1009 /* integer number formats - set up the flags and "break" */
1015 spec
->flags
|= SMALL
;
1023 spec
->flags
|= SIGN
;
1028 spec
->type
= FORMAT_TYPE_INVALID
;
1032 if (spec
->qualifier
== 'L')
1033 spec
->type
= FORMAT_TYPE_LONG_LONG
;
1034 else if (spec
->qualifier
== 'l') {
1035 if (spec
->flags
& SIGN
)
1036 spec
->type
= FORMAT_TYPE_LONG
;
1038 spec
->type
= FORMAT_TYPE_ULONG
;
1039 } else if (spec
->qualifier
== 'Z' || spec
->qualifier
== 'z') {
1040 spec
->type
= FORMAT_TYPE_SIZE_T
;
1041 } else if (spec
->qualifier
== 't') {
1042 spec
->type
= FORMAT_TYPE_PTRDIFF
;
1043 } else if (spec
->qualifier
== 'H') {
1044 if (spec
->flags
& SIGN
)
1045 spec
->type
= FORMAT_TYPE_BYTE
;
1047 spec
->type
= FORMAT_TYPE_UBYTE
;
1048 } else if (spec
->qualifier
== 'h') {
1049 if (spec
->flags
& SIGN
)
1050 spec
->type
= FORMAT_TYPE_SHORT
;
1052 spec
->type
= FORMAT_TYPE_USHORT
;
1054 if (spec
->flags
& SIGN
)
1055 spec
->type
= FORMAT_TYPE_INT
;
1057 spec
->type
= FORMAT_TYPE_UINT
;
1060 return ++fmt
- start
;
1064 * vsnprintf - Format a string and place it in a buffer
1065 * @buf: The buffer to place the result into
1066 * @size: The size of the buffer, including the trailing null space
1067 * @fmt: The format string to use
1068 * @args: Arguments for the format string
1070 * This function follows C99 vsnprintf, but has some extensions:
1071 * %pS output the name of a text symbol
1072 * %pF output the name of a function pointer with its offset
1073 * %pf output the name of a function pointer without its offset
1074 * %pR output the address range in a struct resource
1076 * The return value is the number of characters which would
1077 * be generated for the given input, excluding the trailing
1078 * '\0', as per ISO C99. If you want to have the exact
1079 * number of characters written into @buf as return value
1080 * (not including the trailing '\0'), use vscnprintf(). If the
1081 * return is greater than or equal to @size, the resulting
1082 * string is truncated.
1084 * Call this function if you are already dealing with a va_list.
1085 * You probably want snprintf() instead.
1087 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1089 unsigned long long num
;
1092 struct printf_spec spec
= {0};
1094 /* Reject out-of-range values early. Large positive sizes are
1095 used for unknown buffer sizes. */
1096 if (unlikely((int) size
< 0)) {
1097 /* There can be only one.. */
1098 static char warn
= 1;
1107 /* Make sure end is always >= buf */
1114 const char *old_fmt
= fmt
;
1116 read
= format_decode(fmt
, &spec
);
1120 switch (spec
.type
) {
1121 case FORMAT_TYPE_NONE
: {
1124 if (copy
> end
- str
)
1126 memcpy(str
, old_fmt
, copy
);
1132 case FORMAT_TYPE_WIDTH
:
1133 spec
.field_width
= va_arg(args
, int);
1136 case FORMAT_TYPE_PRECISION
:
1137 spec
.precision
= va_arg(args
, int);
1140 case FORMAT_TYPE_CHAR
:
1141 if (!(spec
.flags
& LEFT
)) {
1142 while (--spec
.field_width
> 0) {
1149 c
= (unsigned char) va_arg(args
, int);
1153 while (--spec
.field_width
> 0) {
1160 case FORMAT_TYPE_STR
:
1161 str
= string(str
, end
, va_arg(args
, char *), spec
);
1164 case FORMAT_TYPE_PTR
:
1165 str
= pointer(fmt
+1, str
, end
, va_arg(args
, void *),
1167 while (isalnum(*fmt
))
1171 case FORMAT_TYPE_PERCENT_CHAR
:
1177 case FORMAT_TYPE_INVALID
:
1183 case FORMAT_TYPE_NRCHARS
: {
1184 int qualifier
= spec
.qualifier
;
1186 if (qualifier
== 'l') {
1187 long *ip
= va_arg(args
, long *);
1189 } else if (qualifier
== 'Z' ||
1191 size_t *ip
= va_arg(args
, size_t *);
1194 int *ip
= va_arg(args
, int *);
1201 switch (spec
.type
) {
1202 case FORMAT_TYPE_LONG_LONG
:
1203 num
= va_arg(args
, long long);
1205 case FORMAT_TYPE_ULONG
:
1206 num
= va_arg(args
, unsigned long);
1208 case FORMAT_TYPE_LONG
:
1209 num
= va_arg(args
, long);
1211 case FORMAT_TYPE_SIZE_T
:
1212 num
= va_arg(args
, size_t);
1214 case FORMAT_TYPE_PTRDIFF
:
1215 num
= va_arg(args
, ptrdiff_t);
1217 case FORMAT_TYPE_UBYTE
:
1218 num
= (unsigned char) va_arg(args
, int);
1220 case FORMAT_TYPE_BYTE
:
1221 num
= (signed char) va_arg(args
, int);
1223 case FORMAT_TYPE_USHORT
:
1224 num
= (unsigned short) va_arg(args
, int);
1226 case FORMAT_TYPE_SHORT
:
1227 num
= (short) va_arg(args
, int);
1229 case FORMAT_TYPE_INT
:
1230 num
= (int) va_arg(args
, int);
1233 num
= va_arg(args
, unsigned int);
1236 str
= number(str
, end
, num
, spec
);
1247 /* the trailing null byte doesn't count towards the total */
1251 EXPORT_SYMBOL(vsnprintf
);
1254 * vscnprintf - Format a string and place it in a buffer
1255 * @buf: The buffer to place the result into
1256 * @size: The size of the buffer, including the trailing null space
1257 * @fmt: The format string to use
1258 * @args: Arguments for the format string
1260 * The return value is the number of characters which have been written into
1261 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1264 * Call this function if you are already dealing with a va_list.
1265 * You probably want scnprintf() instead.
1267 * See the vsnprintf() documentation for format string extensions over C99.
1269 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1273 i
=vsnprintf(buf
,size
,fmt
,args
);
1274 return (i
>= size
) ? (size
- 1) : i
;
1276 EXPORT_SYMBOL(vscnprintf
);
1279 * snprintf - Format a string and place it in a buffer
1280 * @buf: The buffer to place the result into
1281 * @size: The size of the buffer, including the trailing null space
1282 * @fmt: The format string to use
1283 * @...: Arguments for the format string
1285 * The return value is the number of characters which would be
1286 * generated for the given input, excluding the trailing null,
1287 * as per ISO C99. If the return is greater than or equal to
1288 * @size, the resulting string is truncated.
1290 * See the vsnprintf() documentation for format string extensions over C99.
1292 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
1297 va_start(args
, fmt
);
1298 i
=vsnprintf(buf
,size
,fmt
,args
);
1302 EXPORT_SYMBOL(snprintf
);
1305 * scnprintf - Format a string and place it in a buffer
1306 * @buf: The buffer to place the result into
1307 * @size: The size of the buffer, including the trailing null space
1308 * @fmt: The format string to use
1309 * @...: Arguments for the format string
1311 * The return value is the number of characters written into @buf not including
1312 * the trailing '\0'. If @size is <= 0 the function returns 0.
1315 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
1320 va_start(args
, fmt
);
1321 i
= vsnprintf(buf
, size
, fmt
, args
);
1323 return (i
>= size
) ? (size
- 1) : i
;
1325 EXPORT_SYMBOL(scnprintf
);
1328 * vsprintf - Format a string and place it in a buffer
1329 * @buf: The buffer to place the result into
1330 * @fmt: The format string to use
1331 * @args: Arguments for the format string
1333 * The function returns the number of characters written
1334 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1337 * Call this function if you are already dealing with a va_list.
1338 * You probably want sprintf() instead.
1340 * See the vsnprintf() documentation for format string extensions over C99.
1342 int vsprintf(char *buf
, const char *fmt
, va_list args
)
1344 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
1346 EXPORT_SYMBOL(vsprintf
);
1349 * sprintf - Format a string and place it in a buffer
1350 * @buf: The buffer to place the result into
1351 * @fmt: The format string to use
1352 * @...: Arguments for the format string
1354 * The function returns the number of characters written
1355 * into @buf. Use snprintf() or scnprintf() in order to avoid
1358 * See the vsnprintf() documentation for format string extensions over C99.
1360 int sprintf(char * buf
, const char *fmt
, ...)
1365 va_start(args
, fmt
);
1366 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
1370 EXPORT_SYMBOL(sprintf
);
1372 #ifdef CONFIG_BINARY_PRINTF
1375 * vbin_printf() - VA arguments to binary data
1376 * bstr_printf() - Binary data to text string
1380 * vbin_printf - Parse a format string and place args' binary value in a buffer
1381 * @bin_buf: The buffer to place args' binary value
1382 * @size: The size of the buffer(by words(32bits), not characters)
1383 * @fmt: The format string to use
1384 * @args: Arguments for the format string
1386 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1389 * The return value is the number of words(32bits) which would be generated for
1393 * If the return value is greater than @size, the resulting bin_buf is NOT
1394 * valid for bstr_printf().
1396 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
1398 struct printf_spec spec
= {0};
1402 str
= (char *)bin_buf
;
1403 end
= (char *)(bin_buf
+ size
);
1405 #define save_arg(type) \
1407 if (sizeof(type) == 8) { \
1408 unsigned long long value; \
1409 str = PTR_ALIGN(str, sizeof(u32)); \
1410 value = va_arg(args, unsigned long long); \
1411 if (str + sizeof(type) <= end) { \
1412 *(u32 *)str = *(u32 *)&value; \
1413 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1416 unsigned long value; \
1417 str = PTR_ALIGN(str, sizeof(type)); \
1418 value = va_arg(args, int); \
1419 if (str + sizeof(type) <= end) \
1420 *(typeof(type) *)str = (type)value; \
1422 str += sizeof(type); \
1427 read
= format_decode(fmt
, &spec
);
1431 switch (spec
.type
) {
1432 case FORMAT_TYPE_NONE
:
1435 case FORMAT_TYPE_WIDTH
:
1436 case FORMAT_TYPE_PRECISION
:
1440 case FORMAT_TYPE_CHAR
:
1444 case FORMAT_TYPE_STR
: {
1445 const char *save_str
= va_arg(args
, char *);
1447 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
1448 || (unsigned long)save_str
< PAGE_SIZE
)
1449 save_str
= "<NULL>";
1450 len
= strlen(save_str
);
1451 if (str
+ len
+ 1 < end
)
1452 memcpy(str
, save_str
, len
+ 1);
1457 case FORMAT_TYPE_PTR
:
1459 /* skip all alphanumeric pointer suffixes */
1460 while (isalnum(*fmt
))
1464 case FORMAT_TYPE_PERCENT_CHAR
:
1467 case FORMAT_TYPE_INVALID
:
1470 case FORMAT_TYPE_NRCHARS
: {
1471 /* skip %n 's argument */
1472 int qualifier
= spec
.qualifier
;
1474 if (qualifier
== 'l')
1475 skip_arg
= va_arg(args
, long *);
1476 else if (qualifier
== 'Z' || qualifier
== 'z')
1477 skip_arg
= va_arg(args
, size_t *);
1479 skip_arg
= va_arg(args
, int *);
1484 switch (spec
.type
) {
1486 case FORMAT_TYPE_LONG_LONG
:
1487 save_arg(long long);
1489 case FORMAT_TYPE_ULONG
:
1490 case FORMAT_TYPE_LONG
:
1491 save_arg(unsigned long);
1493 case FORMAT_TYPE_SIZE_T
:
1496 case FORMAT_TYPE_PTRDIFF
:
1497 save_arg(ptrdiff_t);
1499 case FORMAT_TYPE_UBYTE
:
1500 case FORMAT_TYPE_BYTE
:
1503 case FORMAT_TYPE_USHORT
:
1504 case FORMAT_TYPE_SHORT
:
1512 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
1516 EXPORT_SYMBOL_GPL(vbin_printf
);
1519 * bstr_printf - Format a string from binary arguments and place it in a buffer
1520 * @buf: The buffer to place the result into
1521 * @size: The size of the buffer, including the trailing null space
1522 * @fmt: The format string to use
1523 * @bin_buf: Binary arguments for the format string
1525 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1526 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1527 * a binary buffer that generated by vbin_printf.
1529 * The format follows C99 vsnprintf, but has some extensions:
1530 * %pS output the name of a text symbol
1531 * %pF output the name of a function pointer with its offset
1532 * %pf output the name of a function pointer without its offset
1533 * %pR output the address range in a struct resource
1536 * The return value is the number of characters which would
1537 * be generated for the given input, excluding the trailing
1538 * '\0', as per ISO C99. If you want to have the exact
1539 * number of characters written into @buf as return value
1540 * (not including the trailing '\0'), use vscnprintf(). If the
1541 * return is greater than or equal to @size, the resulting
1542 * string is truncated.
1544 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
1546 unsigned long long num
;
1548 const char *args
= (const char *)bin_buf
;
1550 struct printf_spec spec
= {0};
1552 if (unlikely((int) size
< 0)) {
1553 /* There can be only one.. */
1554 static char warn
= 1;
1563 #define get_arg(type) \
1565 typeof(type) value; \
1566 if (sizeof(type) == 8) { \
1567 args = PTR_ALIGN(args, sizeof(u32)); \
1568 *(u32 *)&value = *(u32 *)args; \
1569 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1571 args = PTR_ALIGN(args, sizeof(type)); \
1572 value = *(typeof(type) *)args; \
1574 args += sizeof(type); \
1578 /* Make sure end is always >= buf */
1586 const char *old_fmt
= fmt
;
1588 read
= format_decode(fmt
, &spec
);
1592 switch (spec
.type
) {
1593 case FORMAT_TYPE_NONE
: {
1596 if (copy
> end
- str
)
1598 memcpy(str
, old_fmt
, copy
);
1604 case FORMAT_TYPE_WIDTH
:
1605 spec
.field_width
= get_arg(int);
1608 case FORMAT_TYPE_PRECISION
:
1609 spec
.precision
= get_arg(int);
1612 case FORMAT_TYPE_CHAR
:
1613 if (!(spec
.flags
& LEFT
)) {
1614 while (--spec
.field_width
> 0) {
1620 c
= (unsigned char) get_arg(char);
1624 while (--spec
.field_width
> 0) {
1631 case FORMAT_TYPE_STR
: {
1632 const char *str_arg
= args
;
1633 size_t len
= strlen(str_arg
);
1635 str
= string(str
, end
, (char *)str_arg
, spec
);
1639 case FORMAT_TYPE_PTR
:
1640 str
= pointer(fmt
+1, str
, end
, get_arg(void *), spec
);
1641 while (isalnum(*fmt
))
1645 case FORMAT_TYPE_PERCENT_CHAR
:
1651 case FORMAT_TYPE_INVALID
:
1657 case FORMAT_TYPE_NRCHARS
:
1662 switch (spec
.type
) {
1664 case FORMAT_TYPE_LONG_LONG
:
1665 num
= get_arg(long long);
1667 case FORMAT_TYPE_ULONG
:
1668 num
= get_arg(unsigned long);
1670 case FORMAT_TYPE_LONG
:
1671 num
= get_arg(unsigned long);
1673 case FORMAT_TYPE_SIZE_T
:
1674 num
= get_arg(size_t);
1676 case FORMAT_TYPE_PTRDIFF
:
1677 num
= get_arg(ptrdiff_t);
1679 case FORMAT_TYPE_UBYTE
:
1680 num
= get_arg(unsigned char);
1682 case FORMAT_TYPE_BYTE
:
1683 num
= get_arg(signed char);
1685 case FORMAT_TYPE_USHORT
:
1686 num
= get_arg(unsigned short);
1688 case FORMAT_TYPE_SHORT
:
1689 num
= get_arg(short);
1691 case FORMAT_TYPE_UINT
:
1692 num
= get_arg(unsigned int);
1698 str
= number(str
, end
, num
, spec
);
1711 /* the trailing null byte doesn't count towards the total */
1714 EXPORT_SYMBOL_GPL(bstr_printf
);
1717 * bprintf - Parse a format string and place args' binary value in a buffer
1718 * @bin_buf: The buffer to place args' binary value
1719 * @size: The size of the buffer(by words(32bits), not characters)
1720 * @fmt: The format string to use
1721 * @...: Arguments for the format string
1723 * The function returns the number of words(u32) written
1726 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
1731 va_start(args
, fmt
);
1732 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
1736 EXPORT_SYMBOL_GPL(bprintf
);
1738 #endif /* CONFIG_BINARY_PRINTF */
1741 * vsscanf - Unformat a buffer into a list of arguments
1742 * @buf: input buffer
1743 * @fmt: format of buffer
1746 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
1748 const char *str
= buf
;
1757 while(*fmt
&& *str
) {
1758 /* skip any white space in format */
1759 /* white space in format matchs any amount of
1760 * white space, including none, in the input.
1762 if (isspace(*fmt
)) {
1763 while (isspace(*fmt
))
1765 while (isspace(*str
))
1769 /* anything that is not a conversion must match exactly */
1770 if (*fmt
!= '%' && *fmt
) {
1771 if (*fmt
++ != *str
++)
1780 /* skip this conversion.
1781 * advance both strings to next white space
1784 while (!isspace(*fmt
) && *fmt
)
1786 while (!isspace(*str
) && *str
)
1791 /* get field width */
1794 field_width
= skip_atoi(&fmt
);
1796 /* get conversion qualifier */
1798 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
1799 *fmt
== 'Z' || *fmt
== 'z') {
1801 if (unlikely(qualifier
== *fmt
)) {
1802 if (qualifier
== 'h') {
1805 } else if (qualifier
== 'l') {
1820 char *s
= (char *) va_arg(args
,char*);
1821 if (field_width
== -1)
1825 } while (--field_width
> 0 && *str
);
1831 char *s
= (char *) va_arg(args
, char *);
1832 if(field_width
== -1)
1833 field_width
= INT_MAX
;
1834 /* first, skip leading white space in buffer */
1835 while (isspace(*str
))
1838 /* now copy until next white space */
1839 while (*str
&& !isspace(*str
) && field_width
--) {
1847 /* return number of characters read so far */
1849 int *i
= (int *)va_arg(args
,int*);
1867 /* looking for '%' in str */
1872 /* invalid format; stop here */
1876 /* have some sort of integer conversion.
1877 * first, skip white space in buffer.
1879 while (isspace(*str
))
1883 if (is_sign
&& digit
== '-')
1887 || (base
== 16 && !isxdigit(digit
))
1888 || (base
== 10 && !isdigit(digit
))
1889 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
1890 || (base
== 0 && !isdigit(digit
)))
1894 case 'H': /* that's 'hh' in format */
1896 signed char *s
= (signed char *) va_arg(args
,signed char *);
1897 *s
= (signed char) simple_strtol(str
,&next
,base
);
1899 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
1900 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
1905 short *s
= (short *) va_arg(args
,short *);
1906 *s
= (short) simple_strtol(str
,&next
,base
);
1908 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
1909 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
1914 long *l
= (long *) va_arg(args
,long *);
1915 *l
= simple_strtol(str
,&next
,base
);
1917 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
1918 *l
= simple_strtoul(str
,&next
,base
);
1923 long long *l
= (long long*) va_arg(args
,long long *);
1924 *l
= simple_strtoll(str
,&next
,base
);
1926 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
1927 *l
= simple_strtoull(str
,&next
,base
);
1933 size_t *s
= (size_t*) va_arg(args
,size_t*);
1934 *s
= (size_t) simple_strtoul(str
,&next
,base
);
1939 int *i
= (int *) va_arg(args
, int*);
1940 *i
= (int) simple_strtol(str
,&next
,base
);
1942 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
1943 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
1955 * Now we've come all the way through so either the input string or the
1956 * format ended. In the former case, there can be a %n at the current
1957 * position in the format that needs to be filled.
1959 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
1960 int *p
= (int *)va_arg(args
, int *);
1966 EXPORT_SYMBOL(vsscanf
);
1969 * sscanf - Unformat a buffer into a list of arguments
1970 * @buf: input buffer
1971 * @fmt: formatting of buffer
1972 * @...: resulting arguments
1974 int sscanf(const char * buf
, const char * fmt
, ...)
1980 i
= vsscanf(buf
,fmt
,args
);
1984 EXPORT_SYMBOL(sscanf
);