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_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 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
57 unsigned long 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_strtoull
);
82 * simple_strtoul - convert a string to an unsigned 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 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
89 return simple_strtoull(cp
, endp
, base
);
91 EXPORT_SYMBOL(simple_strtoul
);
94 * simple_strtol - convert a string to a signed long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
99 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
102 return -simple_strtoul(cp
+ 1, endp
, base
);
104 return simple_strtoul(cp
, endp
, base
);
106 EXPORT_SYMBOL(simple_strtol
);
109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
114 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
117 return -simple_strtoull(cp
+ 1, endp
, base
);
119 return simple_strtoull(cp
, endp
, base
);
121 EXPORT_SYMBOL(simple_strtoll
);
124 * strict_strtoul - convert a string to an unsigned long strictly
125 * @cp: The string to be converted
126 * @base: The number base to use
127 * @res: The converted result value
129 * strict_strtoul converts a string to an unsigned long only if the
130 * string is really an unsigned long string, any string containing
131 * any invalid char at the tail will be rejected and -EINVAL is returned,
132 * only a newline char at the tail is acceptible because people generally
133 * change a module parameter in the following way:
135 * echo 1024 > /sys/module/e1000/parameters/copybreak
137 * echo will append a newline to the tail.
139 * It returns 0 if conversion is successful and *res is set to the converted
140 * value, otherwise it returns -EINVAL and *res is set to 0.
142 * simple_strtoul just ignores the successive invalid characters and
143 * return the converted value of prefix part of the string.
145 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
156 val
= simple_strtoul(cp
, &tail
, base
);
160 if ((*tail
== '\0') ||
161 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
168 EXPORT_SYMBOL(strict_strtoul
);
171 * strict_strtol - convert a string to a long strictly
172 * @cp: The string to be converted
173 * @base: The number base to use
174 * @res: The converted result value
176 * strict_strtol is similiar to strict_strtoul, but it allows the first
177 * character of a string is '-'.
179 * It returns 0 if conversion is successful and *res is set to the converted
180 * value, otherwise it returns -EINVAL and *res is set to 0.
182 int strict_strtol(const char *cp
, unsigned int base
, long *res
)
186 ret
= strict_strtoul(cp
+ 1, base
, (unsigned long *)res
);
190 ret
= strict_strtoul(cp
, base
, (unsigned long *)res
);
195 EXPORT_SYMBOL(strict_strtol
);
198 * strict_strtoull - convert a string to an unsigned long long strictly
199 * @cp: The string to be converted
200 * @base: The number base to use
201 * @res: The converted result value
203 * strict_strtoull converts a string to an unsigned long long only if the
204 * string is really an unsigned long long string, any string containing
205 * any invalid char at the tail will be rejected and -EINVAL is returned,
206 * only a newline char at the tail is acceptible because people generally
207 * change a module parameter in the following way:
209 * echo 1024 > /sys/module/e1000/parameters/copybreak
211 * echo will append a newline to the tail of the string.
213 * It returns 0 if conversion is successful and *res is set to the converted
214 * value, otherwise it returns -EINVAL and *res is set to 0.
216 * simple_strtoull just ignores the successive invalid characters and
217 * return the converted value of prefix part of the string.
219 int strict_strtoull(const char *cp
, unsigned int base
, unsigned long long *res
)
222 unsigned long long val
;
230 val
= simple_strtoull(cp
, &tail
, base
);
233 if ((*tail
== '\0') ||
234 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
241 EXPORT_SYMBOL(strict_strtoull
);
244 * strict_strtoll - convert a string to a long long strictly
245 * @cp: The string to be converted
246 * @base: The number base to use
247 * @res: The converted result value
249 * strict_strtoll is similiar to strict_strtoull, but it allows the first
250 * character of a string is '-'.
252 * It returns 0 if conversion is successful and *res is set to the converted
253 * value, otherwise it returns -EINVAL and *res is set to 0.
255 int strict_strtoll(const char *cp
, unsigned int base
, long long *res
)
259 ret
= strict_strtoull(cp
+ 1, base
, (unsigned long long *)res
);
263 ret
= strict_strtoull(cp
, base
, (unsigned long long *)res
);
268 EXPORT_SYMBOL(strict_strtoll
);
270 static noinline_for_stack
271 int skip_atoi(const char **s
)
276 i
= i
*10 + *((*s
)++) - '0';
281 /* Decimal conversion is by far the most typical, and is used
282 * for /proc and /sys data. This directly impacts e.g. top performance
283 * with many processes running. We optimize it for speed
285 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
286 * (with permission from the author, Douglas W. Jones). */
288 /* Formats correctly any integer in [0,99999].
289 * Outputs from one to five digits depending on input.
290 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
291 static noinline_for_stack
292 char *put_dec_trunc(char *buf
, unsigned q
)
294 unsigned d3
, d2
, d1
, d0
;
299 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
300 q
= (d0
* 0xcd) >> 11;
302 *buf
++ = d0
+ '0'; /* least significant digit */
303 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
305 q
= (d1
* 0xcd) >> 11;
307 *buf
++ = d1
+ '0'; /* next digit */
310 if ((d2
!= 0) || (d3
!= 0)) {
313 *buf
++ = d2
+ '0'; /* next digit */
317 q
= (d3
* 0xcd) >> 11;
319 *buf
++ = d3
+ '0'; /* next digit */
321 *buf
++ = q
+ '0'; /* most sign. digit */
328 /* Same with if's removed. Always emits five digits */
329 static noinline_for_stack
330 char *put_dec_full(char *buf
, unsigned q
)
332 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
333 /* but anyway, gcc produces better code with full-sized ints */
334 unsigned d3
, d2
, d1
, d0
;
340 * Possible ways to approx. divide by 10
341 * gcc -O2 replaces multiply with shifts and adds
342 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
343 * (x * 0x67) >> 10: 1100111
344 * (x * 0x34) >> 9: 110100 - same
345 * (x * 0x1a) >> 8: 11010 - same
346 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
348 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
349 q
= (d0
* 0xcd) >> 11;
352 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
353 q
= (d1
* 0xcd) >> 11;
363 q
= (d3
* 0xcd) >> 11; /* - shorter code */
364 /* q = (d3 * 0x67) >> 10; - would also work */
371 /* No inlining helps gcc to use registers better */
372 static noinline_for_stack
373 char *put_dec(char *buf
, unsigned long long num
)
378 return put_dec_trunc(buf
, num
);
379 rem
= do_div(num
, 100000);
380 buf
= put_dec_full(buf
, rem
);
384 #define ZEROPAD 1 /* pad with zero */
385 #define SIGN 2 /* unsigned/signed long */
386 #define PLUS 4 /* show plus */
387 #define SPACE 8 /* space if plus */
388 #define LEFT 16 /* left justified */
389 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
390 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
393 FORMAT_TYPE_NONE
, /* Just a string part */
395 FORMAT_TYPE_PRECISION
,
399 FORMAT_TYPE_PERCENT_CHAR
,
401 FORMAT_TYPE_LONG_LONG
,
416 u8 type
; /* format_type enum */
417 u8 flags
; /* flags to number() */
418 u8 base
; /* number base, 8, 10 or 16 only */
419 u8 qualifier
; /* number qualifier, one of 'hHlLtzZ' */
420 s16 field_width
; /* width of output field */
421 s16 precision
; /* # of digits/chars */
424 static noinline_for_stack
425 char *number(char *buf
, char *end
, unsigned long long num
,
426 struct printf_spec spec
)
428 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
429 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
434 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
437 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
438 * produces same digits or (maybe lowercased) letters */
439 locase
= (spec
.flags
& SMALL
);
440 if (spec
.flags
& LEFT
)
441 spec
.flags
&= ~ZEROPAD
;
443 if (spec
.flags
& SIGN
) {
444 if ((signed long long)num
< 0) {
446 num
= -(signed long long)num
;
448 } else if (spec
.flags
& PLUS
) {
451 } else if (spec
.flags
& SPACE
) {
462 /* generate full string in tmp[], in reverse order */
466 /* Generic code, for any base:
468 tmp[i++] = (digits[do_div(num,base)] | locase);
471 else if (spec
.base
!= 10) { /* 8 or 16 */
472 int mask
= spec
.base
- 1;
478 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
481 } else { /* base 10 */
482 i
= put_dec(tmp
, num
) - tmp
;
485 /* printing 100 using %2d gives "100", not "00" */
486 if (i
> spec
.precision
)
488 /* leading space padding */
489 spec
.field_width
-= spec
.precision
;
490 if (!(spec
.flags
& (ZEROPAD
+LEFT
))) {
491 while (--spec
.field_width
>= 0) {
503 /* "0x" / "0" prefix */
508 if (spec
.base
== 16) {
510 *buf
= ('X' | locase
);
514 /* zero or space padding */
515 if (!(spec
.flags
& LEFT
)) {
516 char c
= (spec
.flags
& ZEROPAD
) ? '0' : ' ';
517 while (--spec
.field_width
>= 0) {
523 /* hmm even more zero padding? */
524 while (i
<= --spec
.precision
) {
529 /* actual digits of result */
535 /* trailing space padding */
536 while (--spec
.field_width
>= 0) {
545 static noinline_for_stack
546 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
550 if ((unsigned long)s
< PAGE_SIZE
)
553 len
= strnlen(s
, spec
.precision
);
555 if (!(spec
.flags
& LEFT
)) {
556 while (len
< spec
.field_width
--) {
562 for (i
= 0; i
< len
; ++i
) {
567 while (len
< spec
.field_width
--) {
576 static noinline_for_stack
577 char *symbol_string(char *buf
, char *end
, void *ptr
,
578 struct printf_spec spec
, char ext
)
580 unsigned long value
= (unsigned long) ptr
;
581 #ifdef CONFIG_KALLSYMS
582 char sym
[KSYM_SYMBOL_LEN
];
583 if (ext
!= 'f' && ext
!= 's')
584 sprint_symbol(sym
, value
);
586 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
;
594 return number(buf
, end
, value
, spec
);
598 static noinline_for_stack
599 char *resource_string(char *buf
, char *end
, struct resource
*res
,
600 struct printf_spec spec
, const char *fmt
)
602 #ifndef IO_RSRC_PRINTK_SIZE
603 #define IO_RSRC_PRINTK_SIZE 6
606 #ifndef MEM_RSRC_PRINTK_SIZE
607 #define MEM_RSRC_PRINTK_SIZE 10
609 static const struct printf_spec io_spec
= {
611 .field_width
= IO_RSRC_PRINTK_SIZE
,
613 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
615 static const struct printf_spec mem_spec
= {
617 .field_width
= MEM_RSRC_PRINTK_SIZE
,
619 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
621 static const struct printf_spec bus_spec
= {
625 .flags
= SMALL
| ZEROPAD
,
627 static const struct printf_spec dec_spec
= {
632 static const struct printf_spec str_spec
= {
637 static const struct printf_spec flag_spec
= {
640 .flags
= SPECIAL
| SMALL
,
643 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
644 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
645 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
646 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
647 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
648 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
649 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
650 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
652 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
653 int decode
= (fmt
[0] == 'R') ? 1 : 0;
654 const struct printf_spec
*specp
;
657 if (res
->flags
& IORESOURCE_IO
) {
658 p
= string(p
, pend
, "io ", str_spec
);
660 } else if (res
->flags
& IORESOURCE_MEM
) {
661 p
= string(p
, pend
, "mem ", str_spec
);
663 } else if (res
->flags
& IORESOURCE_IRQ
) {
664 p
= string(p
, pend
, "irq ", str_spec
);
666 } else if (res
->flags
& IORESOURCE_DMA
) {
667 p
= string(p
, pend
, "dma ", str_spec
);
669 } else if (res
->flags
& IORESOURCE_BUS
) {
670 p
= string(p
, pend
, "bus ", str_spec
);
673 p
= string(p
, pend
, "??? ", str_spec
);
677 p
= number(p
, pend
, res
->start
, *specp
);
678 if (res
->start
!= res
->end
) {
680 p
= number(p
, pend
, res
->end
, *specp
);
683 if (res
->flags
& IORESOURCE_MEM_64
)
684 p
= string(p
, pend
, " 64bit", str_spec
);
685 if (res
->flags
& IORESOURCE_PREFETCH
)
686 p
= string(p
, pend
, " pref", str_spec
);
687 if (res
->flags
& IORESOURCE_WINDOW
)
688 p
= string(p
, pend
, " window", str_spec
);
689 if (res
->flags
& IORESOURCE_DISABLED
)
690 p
= string(p
, pend
, " disabled", str_spec
);
692 p
= string(p
, pend
, " flags ", str_spec
);
693 p
= number(p
, pend
, res
->flags
, flag_spec
);
698 return string(buf
, end
, sym
, spec
);
701 static noinline_for_stack
702 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
703 struct printf_spec spec
, const char *fmt
)
705 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
710 if (fmt
[1] == 'F') { /* FDDI canonical format */
716 for (i
= 0; i
< 6; i
++) {
717 p
= pack_hex_byte(p
, addr
[i
]);
718 if (fmt
[0] == 'M' && i
!= 5)
723 return string(buf
, end
, mac_addr
, spec
);
726 static noinline_for_stack
727 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
730 bool leading_zeros
= (fmt
[0] == 'i');
755 for (i
= 0; i
< 4; i
++) {
756 char temp
[3]; /* hold each IP quad in reverse order */
757 int digits
= put_dec_trunc(temp
, addr
[index
]) - temp
;
764 /* reverse the digits in the quad */
776 static noinline_for_stack
777 char *ip6_compressed_string(char *p
, const char *addr
)
780 unsigned char zerolength
[8];
785 bool needcolon
= false;
789 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
791 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
793 memset(zerolength
, 0, sizeof(zerolength
));
800 /* find position of longest 0 run */
801 for (i
= 0; i
< range
; i
++) {
802 for (j
= i
; j
< range
; j
++) {
803 if (in6
.s6_addr16
[j
] != 0)
808 for (i
= 0; i
< range
; i
++) {
809 if (zerolength
[i
] > longest
) {
810 longest
= zerolength
[i
];
816 for (i
= 0; i
< range
; i
++) {
818 if (needcolon
|| i
== 0)
829 /* hex u16 without leading 0s */
830 word
= ntohs(in6
.s6_addr16
[i
]);
835 p
= pack_hex_byte(p
, hi
);
837 *p
++ = hex_asc_lo(hi
);
838 p
= pack_hex_byte(p
, lo
);
841 p
= pack_hex_byte(p
, lo
);
843 *p
++ = hex_asc_lo(lo
);
850 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
857 static noinline_for_stack
858 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
862 for (i
= 0; i
< 8; i
++) {
863 p
= pack_hex_byte(p
, *addr
++);
864 p
= pack_hex_byte(p
, *addr
++);
865 if (fmt
[0] == 'I' && i
!= 7)
873 static noinline_for_stack
874 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
875 struct printf_spec spec
, const char *fmt
)
877 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
879 if (fmt
[0] == 'I' && fmt
[2] == 'c')
880 ip6_compressed_string(ip6_addr
, addr
);
882 ip6_string(ip6_addr
, addr
, fmt
);
884 return string(buf
, end
, ip6_addr
, spec
);
887 static noinline_for_stack
888 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
889 struct printf_spec spec
, const char *fmt
)
891 char ip4_addr
[sizeof("255.255.255.255")];
893 ip4_string(ip4_addr
, addr
, fmt
);
895 return string(buf
, end
, ip4_addr
, spec
);
898 static noinline_for_stack
899 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
900 struct printf_spec spec
, const char *fmt
)
902 char uuid
[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
905 static const u8 be
[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
906 static const u8 le
[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
907 const u8
*index
= be
;
912 uc
= true; /* fall-through */
921 for (i
= 0; i
< 16; i
++) {
922 p
= pack_hex_byte(p
, addr
[index
[i
]]);
942 return string(buf
, end
, uuid
, spec
);
946 * Show a '%p' thing. A kernel extension is that the '%p' is followed
947 * by an extra set of alphanumeric characters that are extended format
950 * Right now we handle:
952 * - 'F' For symbolic function descriptor pointers with offset
953 * - 'f' For simple symbolic function names without offset
954 * - 'S' For symbolic direct pointers with offset
955 * - 's' For symbolic direct pointers without offset
956 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
957 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
958 * - 'M' For a 6-byte MAC address, it prints the address in the
959 * usual colon-separated hex notation
960 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
961 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
962 * with a dash-separated hex notation
963 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
964 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
965 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
966 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
967 * IPv6 omits the colons (01020304...0f)
968 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
969 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
970 * - 'I6c' for IPv6 addresses printed as specified by
971 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
972 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
973 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
974 * Options for %pU are:
975 * b big endian lower case hex (default)
976 * B big endian UPPER case hex
977 * l little endian lower case hex
978 * L little endian UPPER case hex
979 * big endian output byte order is:
980 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
981 * little endian output byte order is:
982 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
984 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
985 * function pointers are really function descriptors, which contain a
986 * pointer to the real address.
988 static noinline_for_stack
989 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
990 struct printf_spec spec
)
993 return string(buf
, end
, "(null)", spec
);
998 ptr
= dereference_function_descriptor(ptr
);
1002 return symbol_string(buf
, end
, ptr
, spec
, *fmt
);
1005 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1006 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1007 case 'm': /* Contiguous: 000102030405 */
1008 /* [mM]F (FDDI, bit reversed) */
1009 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1010 case 'I': /* Formatted IP supported
1012 * 6: 0001:0203:...:0708
1013 * 6c: 1::708 or 1::1.2.3.4
1015 case 'i': /* Contiguous:
1016 * 4: 001.002.003.004
1021 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1023 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1027 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1029 spec
.flags
|= SMALL
;
1030 if (spec
.field_width
== -1) {
1031 spec
.field_width
= 2*sizeof(void *);
1032 spec
.flags
|= ZEROPAD
;
1036 return number(buf
, end
, (unsigned long) ptr
, spec
);
1040 * Helper function to decode printf style format.
1041 * Each call decode a token from the format and return the
1042 * number of characters read (or likely the delta where it wants
1043 * to go on the next call).
1044 * The decoded token is returned through the parameters
1046 * 'h', 'l', or 'L' for integer fields
1047 * 'z' support added 23/7/1999 S.H.
1048 * 'z' changed to 'Z' --davidm 1/25/99
1049 * 't' added for ptrdiff_t
1051 * @fmt: the format string
1052 * @type of the token returned
1053 * @flags: various flags such as +, -, # tokens..
1054 * @field_width: overwritten width
1055 * @base: base of the number (octal, hex, ...)
1056 * @precision: precision of a number
1057 * @qualifier: qualifier of a number (long, size_t, ...)
1059 static noinline_for_stack
1060 int format_decode(const char *fmt
, struct printf_spec
*spec
)
1062 const char *start
= fmt
;
1064 /* we finished early by reading the field width */
1065 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
1066 if (spec
->field_width
< 0) {
1067 spec
->field_width
= -spec
->field_width
;
1068 spec
->flags
|= LEFT
;
1070 spec
->type
= FORMAT_TYPE_NONE
;
1074 /* we finished early by reading the precision */
1075 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
1076 if (spec
->precision
< 0)
1077 spec
->precision
= 0;
1079 spec
->type
= FORMAT_TYPE_NONE
;
1084 spec
->type
= FORMAT_TYPE_NONE
;
1086 for (; *fmt
; ++fmt
) {
1091 /* Return the current non-format string */
1092 if (fmt
!= start
|| !*fmt
)
1098 while (1) { /* this also skips first '%' */
1104 case '-': spec
->flags
|= LEFT
; break;
1105 case '+': spec
->flags
|= PLUS
; break;
1106 case ' ': spec
->flags
|= SPACE
; break;
1107 case '#': spec
->flags
|= SPECIAL
; break;
1108 case '0': spec
->flags
|= ZEROPAD
; break;
1109 default: found
= false;
1116 /* get field width */
1117 spec
->field_width
= -1;
1120 spec
->field_width
= skip_atoi(&fmt
);
1121 else if (*fmt
== '*') {
1122 /* it's the next argument */
1123 spec
->type
= FORMAT_TYPE_WIDTH
;
1124 return ++fmt
- start
;
1128 /* get the precision */
1129 spec
->precision
= -1;
1132 if (isdigit(*fmt
)) {
1133 spec
->precision
= skip_atoi(&fmt
);
1134 if (spec
->precision
< 0)
1135 spec
->precision
= 0;
1136 } else if (*fmt
== '*') {
1137 /* it's the next argument */
1138 spec
->type
= FORMAT_TYPE_PRECISION
;
1139 return ++fmt
- start
;
1144 /* get the conversion qualifier */
1145 spec
->qualifier
= -1;
1146 if (*fmt
== 'h' || TOLOWER(*fmt
) == 'l' ||
1147 TOLOWER(*fmt
) == 'z' || *fmt
== 't') {
1148 spec
->qualifier
= *fmt
++;
1149 if (unlikely(spec
->qualifier
== *fmt
)) {
1150 if (spec
->qualifier
== 'l') {
1151 spec
->qualifier
= 'L';
1153 } else if (spec
->qualifier
== 'h') {
1154 spec
->qualifier
= 'H';
1164 spec
->type
= FORMAT_TYPE_CHAR
;
1165 return ++fmt
- start
;
1168 spec
->type
= FORMAT_TYPE_STR
;
1169 return ++fmt
- start
;
1172 spec
->type
= FORMAT_TYPE_PTR
;
1177 spec
->type
= FORMAT_TYPE_NRCHARS
;
1178 return ++fmt
- start
;
1181 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
1182 return ++fmt
- start
;
1184 /* integer number formats - set up the flags and "break" */
1190 spec
->flags
|= SMALL
;
1198 spec
->flags
|= SIGN
;
1203 spec
->type
= FORMAT_TYPE_INVALID
;
1207 if (spec
->qualifier
== 'L')
1208 spec
->type
= FORMAT_TYPE_LONG_LONG
;
1209 else if (spec
->qualifier
== 'l') {
1210 if (spec
->flags
& SIGN
)
1211 spec
->type
= FORMAT_TYPE_LONG
;
1213 spec
->type
= FORMAT_TYPE_ULONG
;
1214 } else if (TOLOWER(spec
->qualifier
) == 'z') {
1215 spec
->type
= FORMAT_TYPE_SIZE_T
;
1216 } else if (spec
->qualifier
== 't') {
1217 spec
->type
= FORMAT_TYPE_PTRDIFF
;
1218 } else if (spec
->qualifier
== 'H') {
1219 if (spec
->flags
& SIGN
)
1220 spec
->type
= FORMAT_TYPE_BYTE
;
1222 spec
->type
= FORMAT_TYPE_UBYTE
;
1223 } else if (spec
->qualifier
== 'h') {
1224 if (spec
->flags
& SIGN
)
1225 spec
->type
= FORMAT_TYPE_SHORT
;
1227 spec
->type
= FORMAT_TYPE_USHORT
;
1229 if (spec
->flags
& SIGN
)
1230 spec
->type
= FORMAT_TYPE_INT
;
1232 spec
->type
= FORMAT_TYPE_UINT
;
1235 return ++fmt
- start
;
1239 * vsnprintf - Format a string and place it in a buffer
1240 * @buf: The buffer to place the result into
1241 * @size: The size of the buffer, including the trailing null space
1242 * @fmt: The format string to use
1243 * @args: Arguments for the format string
1245 * This function follows C99 vsnprintf, but has some extensions:
1246 * %pS output the name of a text symbol with offset
1247 * %ps output the name of a text symbol without offset
1248 * %pF output the name of a function pointer with its offset
1249 * %pf output the name of a function pointer without its offset
1250 * %pR output the address range in a struct resource with decoded flags
1251 * %pr output the address range in a struct resource with raw flags
1252 * %pM output a 6-byte MAC address with colons
1253 * %pm output a 6-byte MAC address without colons
1254 * %pI4 print an IPv4 address without leading zeros
1255 * %pi4 print an IPv4 address with leading zeros
1256 * %pI6 print an IPv6 address with colons
1257 * %pi6 print an IPv6 address without colons
1258 * %pI6c print an IPv6 address as specified by
1259 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
1260 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1264 * The return value is the number of characters which would
1265 * be generated for the given input, excluding the trailing
1266 * '\0', as per ISO C99. If you want to have the exact
1267 * number of characters written into @buf as return value
1268 * (not including the trailing '\0'), use vscnprintf(). If the
1269 * return is greater than or equal to @size, the resulting
1270 * string is truncated.
1272 * Call this function if you are already dealing with a va_list.
1273 * You probably want snprintf() instead.
1275 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1277 unsigned long long num
;
1279 struct printf_spec spec
= {0};
1281 /* Reject out-of-range values early. Large positive sizes are
1282 used for unknown buffer sizes. */
1283 if (WARN_ON_ONCE((int) size
< 0))
1289 /* Make sure end is always >= buf */
1296 const char *old_fmt
= fmt
;
1297 int read
= format_decode(fmt
, &spec
);
1301 switch (spec
.type
) {
1302 case FORMAT_TYPE_NONE
: {
1305 if (copy
> end
- str
)
1307 memcpy(str
, old_fmt
, copy
);
1313 case FORMAT_TYPE_WIDTH
:
1314 spec
.field_width
= va_arg(args
, int);
1317 case FORMAT_TYPE_PRECISION
:
1318 spec
.precision
= va_arg(args
, int);
1321 case FORMAT_TYPE_CHAR
: {
1324 if (!(spec
.flags
& LEFT
)) {
1325 while (--spec
.field_width
> 0) {
1332 c
= (unsigned char) va_arg(args
, int);
1336 while (--spec
.field_width
> 0) {
1344 case FORMAT_TYPE_STR
:
1345 str
= string(str
, end
, va_arg(args
, char *), spec
);
1348 case FORMAT_TYPE_PTR
:
1349 str
= pointer(fmt
+1, str
, end
, va_arg(args
, void *),
1351 while (isalnum(*fmt
))
1355 case FORMAT_TYPE_PERCENT_CHAR
:
1361 case FORMAT_TYPE_INVALID
:
1367 case FORMAT_TYPE_NRCHARS
: {
1368 u8 qualifier
= spec
.qualifier
;
1370 if (qualifier
== 'l') {
1371 long *ip
= va_arg(args
, long *);
1373 } else if (TOLOWER(qualifier
) == 'z') {
1374 size_t *ip
= va_arg(args
, size_t *);
1377 int *ip
= va_arg(args
, int *);
1384 switch (spec
.type
) {
1385 case FORMAT_TYPE_LONG_LONG
:
1386 num
= va_arg(args
, long long);
1388 case FORMAT_TYPE_ULONG
:
1389 num
= va_arg(args
, unsigned long);
1391 case FORMAT_TYPE_LONG
:
1392 num
= va_arg(args
, long);
1394 case FORMAT_TYPE_SIZE_T
:
1395 num
= va_arg(args
, size_t);
1397 case FORMAT_TYPE_PTRDIFF
:
1398 num
= va_arg(args
, ptrdiff_t);
1400 case FORMAT_TYPE_UBYTE
:
1401 num
= (unsigned char) va_arg(args
, int);
1403 case FORMAT_TYPE_BYTE
:
1404 num
= (signed char) va_arg(args
, int);
1406 case FORMAT_TYPE_USHORT
:
1407 num
= (unsigned short) va_arg(args
, int);
1409 case FORMAT_TYPE_SHORT
:
1410 num
= (short) va_arg(args
, int);
1412 case FORMAT_TYPE_INT
:
1413 num
= (int) va_arg(args
, int);
1416 num
= va_arg(args
, unsigned int);
1419 str
= number(str
, end
, num
, spec
);
1430 /* the trailing null byte doesn't count towards the total */
1434 EXPORT_SYMBOL(vsnprintf
);
1437 * vscnprintf - Format a string and place it in a buffer
1438 * @buf: The buffer to place the result into
1439 * @size: The size of the buffer, including the trailing null space
1440 * @fmt: The format string to use
1441 * @args: Arguments for the format string
1443 * The return value is the number of characters which have been written into
1444 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1447 * Call this function if you are already dealing with a va_list.
1448 * You probably want scnprintf() instead.
1450 * See the vsnprintf() documentation for format string extensions over C99.
1452 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1456 i
= vsnprintf(buf
, size
, fmt
, args
);
1458 return (i
>= size
) ? (size
- 1) : i
;
1460 EXPORT_SYMBOL(vscnprintf
);
1463 * snprintf - Format a string and place it in a buffer
1464 * @buf: The buffer to place the result into
1465 * @size: The size of the buffer, including the trailing null space
1466 * @fmt: The format string to use
1467 * @...: Arguments for the format string
1469 * The return value is the number of characters which would be
1470 * generated for the given input, excluding the trailing null,
1471 * as per ISO C99. If the return is greater than or equal to
1472 * @size, the resulting string is truncated.
1474 * See the vsnprintf() documentation for format string extensions over C99.
1476 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
1481 va_start(args
, fmt
);
1482 i
= vsnprintf(buf
, size
, fmt
, args
);
1487 EXPORT_SYMBOL(snprintf
);
1490 * scnprintf - Format a string and place it in a buffer
1491 * @buf: The buffer to place the result into
1492 * @size: The size of the buffer, including the trailing null space
1493 * @fmt: The format string to use
1494 * @...: Arguments for the format string
1496 * The return value is the number of characters written into @buf not including
1497 * the trailing '\0'. If @size is <= 0 the function returns 0.
1500 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
1505 va_start(args
, fmt
);
1506 i
= vsnprintf(buf
, size
, fmt
, args
);
1509 return (i
>= size
) ? (size
- 1) : i
;
1511 EXPORT_SYMBOL(scnprintf
);
1514 * vsprintf - Format a string and place it in a buffer
1515 * @buf: The buffer to place the result into
1516 * @fmt: The format string to use
1517 * @args: Arguments for the format string
1519 * The function returns the number of characters written
1520 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1523 * Call this function if you are already dealing with a va_list.
1524 * You probably want sprintf() instead.
1526 * See the vsnprintf() documentation for format string extensions over C99.
1528 int vsprintf(char *buf
, const char *fmt
, va_list args
)
1530 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
1532 EXPORT_SYMBOL(vsprintf
);
1535 * sprintf - Format a string and place it in a buffer
1536 * @buf: The buffer to place the result into
1537 * @fmt: The format string to use
1538 * @...: Arguments for the format string
1540 * The function returns the number of characters written
1541 * into @buf. Use snprintf() or scnprintf() in order to avoid
1544 * See the vsnprintf() documentation for format string extensions over C99.
1546 int sprintf(char *buf
, const char *fmt
, ...)
1551 va_start(args
, fmt
);
1552 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
1557 EXPORT_SYMBOL(sprintf
);
1559 #ifdef CONFIG_BINARY_PRINTF
1562 * vbin_printf() - VA arguments to binary data
1563 * bstr_printf() - Binary data to text string
1567 * vbin_printf - Parse a format string and place args' binary value in a buffer
1568 * @bin_buf: The buffer to place args' binary value
1569 * @size: The size of the buffer(by words(32bits), not characters)
1570 * @fmt: The format string to use
1571 * @args: Arguments for the format string
1573 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1576 * The return value is the number of words(32bits) which would be generated for
1580 * If the return value is greater than @size, the resulting bin_buf is NOT
1581 * valid for bstr_printf().
1583 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
1585 struct printf_spec spec
= {0};
1588 str
= (char *)bin_buf
;
1589 end
= (char *)(bin_buf
+ size
);
1591 #define save_arg(type) \
1593 if (sizeof(type) == 8) { \
1594 unsigned long long value; \
1595 str = PTR_ALIGN(str, sizeof(u32)); \
1596 value = va_arg(args, unsigned long long); \
1597 if (str + sizeof(type) <= end) { \
1598 *(u32 *)str = *(u32 *)&value; \
1599 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1602 unsigned long value; \
1603 str = PTR_ALIGN(str, sizeof(type)); \
1604 value = va_arg(args, int); \
1605 if (str + sizeof(type) <= end) \
1606 *(typeof(type) *)str = (type)value; \
1608 str += sizeof(type); \
1612 int read
= format_decode(fmt
, &spec
);
1616 switch (spec
.type
) {
1617 case FORMAT_TYPE_NONE
:
1618 case FORMAT_TYPE_INVALID
:
1619 case FORMAT_TYPE_PERCENT_CHAR
:
1622 case FORMAT_TYPE_WIDTH
:
1623 case FORMAT_TYPE_PRECISION
:
1627 case FORMAT_TYPE_CHAR
:
1631 case FORMAT_TYPE_STR
: {
1632 const char *save_str
= va_arg(args
, char *);
1635 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
1636 || (unsigned long)save_str
< PAGE_SIZE
)
1637 save_str
= "(null)";
1638 len
= strlen(save_str
) + 1;
1639 if (str
+ len
< end
)
1640 memcpy(str
, save_str
, len
);
1645 case FORMAT_TYPE_PTR
:
1647 /* skip all alphanumeric pointer suffixes */
1648 while (isalnum(*fmt
))
1652 case FORMAT_TYPE_NRCHARS
: {
1653 /* skip %n 's argument */
1654 u8 qualifier
= spec
.qualifier
;
1656 if (qualifier
== 'l')
1657 skip_arg
= va_arg(args
, long *);
1658 else if (TOLOWER(qualifier
) == 'z')
1659 skip_arg
= va_arg(args
, size_t *);
1661 skip_arg
= va_arg(args
, int *);
1666 switch (spec
.type
) {
1668 case FORMAT_TYPE_LONG_LONG
:
1669 save_arg(long long);
1671 case FORMAT_TYPE_ULONG
:
1672 case FORMAT_TYPE_LONG
:
1673 save_arg(unsigned long);
1675 case FORMAT_TYPE_SIZE_T
:
1678 case FORMAT_TYPE_PTRDIFF
:
1679 save_arg(ptrdiff_t);
1681 case FORMAT_TYPE_UBYTE
:
1682 case FORMAT_TYPE_BYTE
:
1685 case FORMAT_TYPE_USHORT
:
1686 case FORMAT_TYPE_SHORT
:
1695 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
1698 EXPORT_SYMBOL_GPL(vbin_printf
);
1701 * bstr_printf - Format a string from binary arguments and place it in a buffer
1702 * @buf: The buffer to place the result into
1703 * @size: The size of the buffer, including the trailing null space
1704 * @fmt: The format string to use
1705 * @bin_buf: Binary arguments for the format string
1707 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1708 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1709 * a binary buffer that generated by vbin_printf.
1711 * The format follows C99 vsnprintf, but has some extensions:
1712 * see vsnprintf comment for details.
1714 * The return value is the number of characters which would
1715 * be generated for the given input, excluding the trailing
1716 * '\0', as per ISO C99. If you want to have the exact
1717 * number of characters written into @buf as return value
1718 * (not including the trailing '\0'), use vscnprintf(). If the
1719 * return is greater than or equal to @size, the resulting
1720 * string is truncated.
1722 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
1724 struct printf_spec spec
= {0};
1726 const char *args
= (const char *)bin_buf
;
1728 if (WARN_ON_ONCE((int) size
< 0))
1734 #define get_arg(type) \
1736 typeof(type) value; \
1737 if (sizeof(type) == 8) { \
1738 args = PTR_ALIGN(args, sizeof(u32)); \
1739 *(u32 *)&value = *(u32 *)args; \
1740 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1742 args = PTR_ALIGN(args, sizeof(type)); \
1743 value = *(typeof(type) *)args; \
1745 args += sizeof(type); \
1749 /* Make sure end is always >= buf */
1756 const char *old_fmt
= fmt
;
1757 int read
= format_decode(fmt
, &spec
);
1761 switch (spec
.type
) {
1762 case FORMAT_TYPE_NONE
: {
1765 if (copy
> end
- str
)
1767 memcpy(str
, old_fmt
, copy
);
1773 case FORMAT_TYPE_WIDTH
:
1774 spec
.field_width
= get_arg(int);
1777 case FORMAT_TYPE_PRECISION
:
1778 spec
.precision
= get_arg(int);
1781 case FORMAT_TYPE_CHAR
: {
1784 if (!(spec
.flags
& LEFT
)) {
1785 while (--spec
.field_width
> 0) {
1791 c
= (unsigned char) get_arg(char);
1795 while (--spec
.field_width
> 0) {
1803 case FORMAT_TYPE_STR
: {
1804 const char *str_arg
= args
;
1805 args
+= strlen(str_arg
) + 1;
1806 str
= string(str
, end
, (char *)str_arg
, spec
);
1810 case FORMAT_TYPE_PTR
:
1811 str
= pointer(fmt
+1, str
, end
, get_arg(void *), spec
);
1812 while (isalnum(*fmt
))
1816 case FORMAT_TYPE_PERCENT_CHAR
:
1817 case FORMAT_TYPE_INVALID
:
1823 case FORMAT_TYPE_NRCHARS
:
1828 unsigned long long num
;
1830 switch (spec
.type
) {
1832 case FORMAT_TYPE_LONG_LONG
:
1833 num
= get_arg(long long);
1835 case FORMAT_TYPE_ULONG
:
1836 case FORMAT_TYPE_LONG
:
1837 num
= get_arg(unsigned long);
1839 case FORMAT_TYPE_SIZE_T
:
1840 num
= get_arg(size_t);
1842 case FORMAT_TYPE_PTRDIFF
:
1843 num
= get_arg(ptrdiff_t);
1845 case FORMAT_TYPE_UBYTE
:
1846 num
= get_arg(unsigned char);
1848 case FORMAT_TYPE_BYTE
:
1849 num
= get_arg(signed char);
1851 case FORMAT_TYPE_USHORT
:
1852 num
= get_arg(unsigned short);
1854 case FORMAT_TYPE_SHORT
:
1855 num
= get_arg(short);
1857 case FORMAT_TYPE_UINT
:
1858 num
= get_arg(unsigned int);
1864 str
= number(str
, end
, num
, spec
);
1866 } /* switch(spec.type) */
1878 /* the trailing null byte doesn't count towards the total */
1881 EXPORT_SYMBOL_GPL(bstr_printf
);
1884 * bprintf - Parse a format string and place args' binary value in a buffer
1885 * @bin_buf: The buffer to place args' binary value
1886 * @size: The size of the buffer(by words(32bits), not characters)
1887 * @fmt: The format string to use
1888 * @...: Arguments for the format string
1890 * The function returns the number of words(u32) written
1893 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
1898 va_start(args
, fmt
);
1899 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
1904 EXPORT_SYMBOL_GPL(bprintf
);
1906 #endif /* CONFIG_BINARY_PRINTF */
1909 * vsscanf - Unformat a buffer into a list of arguments
1910 * @buf: input buffer
1911 * @fmt: format of buffer
1914 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
1916 const char *str
= buf
;
1925 while (*fmt
&& *str
) {
1926 /* skip any white space in format */
1927 /* white space in format matchs any amount of
1928 * white space, including none, in the input.
1930 if (isspace(*fmt
)) {
1931 fmt
= skip_spaces(++fmt
);
1932 str
= skip_spaces(str
);
1935 /* anything that is not a conversion must match exactly */
1936 if (*fmt
!= '%' && *fmt
) {
1937 if (*fmt
++ != *str
++)
1946 /* skip this conversion.
1947 * advance both strings to next white space
1950 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
)
1952 while (!isspace(*str
) && *str
)
1957 /* get field width */
1960 field_width
= skip_atoi(&fmt
);
1962 /* get conversion qualifier */
1964 if (*fmt
== 'h' || TOLOWER(*fmt
) == 'l' ||
1965 TOLOWER(*fmt
) == 'z') {
1967 if (unlikely(qualifier
== *fmt
)) {
1968 if (qualifier
== 'h') {
1971 } else if (qualifier
== 'l') {
1987 char *s
= (char *)va_arg(args
, char*);
1988 if (field_width
== -1)
1992 } while (--field_width
> 0 && *str
);
1998 char *s
= (char *)va_arg(args
, char *);
1999 if (field_width
== -1)
2000 field_width
= SHRT_MAX
;
2001 /* first, skip leading white space in buffer */
2002 str
= skip_spaces(str
);
2004 /* now copy until next white space */
2005 while (*str
&& !isspace(*str
) && field_width
--)
2012 /* return number of characters read so far */
2014 int *i
= (int *)va_arg(args
, int*);
2032 /* looking for '%' in str */
2037 /* invalid format; stop here */
2041 /* have some sort of integer conversion.
2042 * first, skip white space in buffer.
2044 str
= skip_spaces(str
);
2047 if (is_sign
&& digit
== '-')
2051 || (base
== 16 && !isxdigit(digit
))
2052 || (base
== 10 && !isdigit(digit
))
2053 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
2054 || (base
== 0 && !isdigit(digit
)))
2057 switch (qualifier
) {
2058 case 'H': /* that's 'hh' in format */
2060 signed char *s
= (signed char *)va_arg(args
, signed char *);
2061 *s
= (signed char)simple_strtol(str
, &next
, base
);
2063 unsigned char *s
= (unsigned char *)va_arg(args
, unsigned char *);
2064 *s
= (unsigned char)simple_strtoul(str
, &next
, base
);
2069 short *s
= (short *)va_arg(args
, short *);
2070 *s
= (short)simple_strtol(str
, &next
, base
);
2072 unsigned short *s
= (unsigned short *)va_arg(args
, unsigned short *);
2073 *s
= (unsigned short)simple_strtoul(str
, &next
, base
);
2078 long *l
= (long *)va_arg(args
, long *);
2079 *l
= simple_strtol(str
, &next
, base
);
2081 unsigned long *l
= (unsigned long *)va_arg(args
, unsigned long *);
2082 *l
= simple_strtoul(str
, &next
, base
);
2087 long long *l
= (long long *)va_arg(args
, long long *);
2088 *l
= simple_strtoll(str
, &next
, base
);
2090 unsigned long long *l
= (unsigned long long *)va_arg(args
, unsigned long long *);
2091 *l
= simple_strtoull(str
, &next
, base
);
2097 size_t *s
= (size_t *)va_arg(args
, size_t *);
2098 *s
= (size_t)simple_strtoul(str
, &next
, base
);
2103 int *i
= (int *)va_arg(args
, int *);
2104 *i
= (int)simple_strtol(str
, &next
, base
);
2106 unsigned int *i
= (unsigned int *)va_arg(args
, unsigned int*);
2107 *i
= (unsigned int)simple_strtoul(str
, &next
, base
);
2119 * Now we've come all the way through so either the input string or the
2120 * format ended. In the former case, there can be a %n at the current
2121 * position in the format that needs to be filled.
2123 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
2124 int *p
= (int *)va_arg(args
, int *);
2130 EXPORT_SYMBOL(vsscanf
);
2133 * sscanf - Unformat a buffer into a list of arguments
2134 * @buf: input buffer
2135 * @fmt: formatting of buffer
2136 * @...: resulting arguments
2138 int sscanf(const char *buf
, const char *fmt
, ...)
2143 va_start(args
, fmt
);
2144 i
= vsscanf(buf
, fmt
, args
);
2149 EXPORT_SYMBOL(sscanf
);