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>
29 #include <asm/page.h> /* for PAGE_SIZE */
30 #include <asm/div64.h>
31 #include <asm/sections.h> /* for dereference_function_descriptor() */
33 /* Works only for digits and letters, but small and fast */
34 #define TOLOWER(x) ((x) | 0x20)
36 static unsigned int simple_guess_base(const char *cp
)
39 if (TOLOWER(cp
[1]) == 'x' && isxdigit(cp
[2]))
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
54 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
56 unsigned long result
= 0;
59 base
= simple_guess_base(cp
);
61 if (base
== 16 && cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
64 while (isxdigit(*cp
)) {
67 value
= isdigit(*cp
) ? *cp
- '0' : TOLOWER(*cp
) - 'a' + 10;
70 result
= result
* base
+ value
;
78 EXPORT_SYMBOL(simple_strtoul
);
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
86 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
89 return -simple_strtoul(cp
+ 1, endp
, base
);
90 return simple_strtoul(cp
, endp
, base
);
92 EXPORT_SYMBOL(simple_strtol
);
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
100 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
102 unsigned long long result
= 0;
105 base
= simple_guess_base(cp
);
107 if (base
== 16 && cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
110 while (isxdigit(*cp
)) {
113 value
= isdigit(*cp
) ? *cp
- '0' : TOLOWER(*cp
) - 'a' + 10;
116 result
= result
* base
+ value
;
124 EXPORT_SYMBOL(simple_strtoull
);
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
132 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
135 return -simple_strtoull(cp
+ 1, endp
, base
);
136 return simple_strtoull(cp
, endp
, base
);
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 * echo will append a newline to the tail.
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
161 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
)
172 val
= simple_strtoul(cp
, &tail
, base
);
175 if ((*tail
== '\0') ||
176 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
183 EXPORT_SYMBOL(strict_strtoul
);
186 * strict_strtol - convert a string to a long strictly
187 * @cp: The string to be converted
188 * @base: The number base to use
189 * @res: The converted result value
191 * strict_strtol is similiar to strict_strtoul, but it allows the first
192 * character of a string is '-'.
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
197 int strict_strtol(const char *cp
, unsigned int base
, long *res
)
201 ret
= strict_strtoul(cp
+ 1, base
, (unsigned long *)res
);
205 ret
= strict_strtoul(cp
, base
, (unsigned long *)res
);
210 EXPORT_SYMBOL(strict_strtol
);
213 * strict_strtoull - convert a string to an unsigned long long strictly
214 * @cp: The string to be converted
215 * @base: The number base to use
216 * @res: The converted result value
218 * strict_strtoull converts a string to an unsigned long long only if the
219 * string is really an unsigned long long string, any string containing
220 * any invalid char at the tail will be rejected and -EINVAL is returned,
221 * only a newline char at the tail is acceptible because people generally
222 * change a module parameter in the following way:
224 * echo 1024 > /sys/module/e1000/parameters/copybreak
226 * echo will append a newline to the tail of the string.
228 * It returns 0 if conversion is successful and *res is set to the converted
229 * value, otherwise it returns -EINVAL and *res is set to 0.
231 * simple_strtoull just ignores the successive invalid characters and
232 * return the converted value of prefix part of the string.
234 int strict_strtoull(const char *cp
, unsigned int base
, unsigned long long *res
)
237 unsigned long long val
;
245 val
= simple_strtoull(cp
, &tail
, base
);
248 if ((*tail
== '\0') ||
249 ((len
== (size_t)(tail
- cp
) + 1) && (*tail
== '\n'))) {
256 EXPORT_SYMBOL(strict_strtoull
);
259 * strict_strtoll - convert a string to a long long strictly
260 * @cp: The string to be converted
261 * @base: The number base to use
262 * @res: The converted result value
264 * strict_strtoll is similiar to strict_strtoull, but it allows the first
265 * character of a string is '-'.
267 * It returns 0 if conversion is successful and *res is set to the converted
268 * value, otherwise it returns -EINVAL and *res is set to 0.
270 int strict_strtoll(const char *cp
, unsigned int base
, long long *res
)
274 ret
= strict_strtoull(cp
+ 1, base
, (unsigned long long *)res
);
278 ret
= strict_strtoull(cp
, base
, (unsigned long long *)res
);
283 EXPORT_SYMBOL(strict_strtoll
);
285 static int skip_atoi(const char **s
)
290 i
= i
*10 + *((*s
)++) - '0';
294 /* Decimal conversion is by far the most typical, and is used
295 * for /proc and /sys data. This directly impacts e.g. top performance
296 * with many processes running. We optimize it for speed
298 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
299 * (with permission from the author, Douglas W. Jones). */
301 /* Formats correctly any integer in [0,99999].
302 * Outputs from one to five digits depending on input.
303 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
304 static char* put_dec_trunc(char *buf
, unsigned q
)
306 unsigned d3
, d2
, d1
, d0
;
311 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
312 q
= (d0
* 0xcd) >> 11;
314 *buf
++ = d0
+ '0'; /* least significant digit */
315 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
317 q
= (d1
* 0xcd) >> 11;
319 *buf
++ = d1
+ '0'; /* next digit */
322 if ((d2
!= 0) || (d3
!= 0)) {
325 *buf
++ = d2
+ '0'; /* next digit */
329 q
= (d3
* 0xcd) >> 11;
331 *buf
++ = d3
+ '0'; /* next digit */
333 *buf
++ = q
+ '0'; /* most sign. digit */
339 /* Same with if's removed. Always emits five digits */
340 static char* put_dec_full(char *buf
, unsigned q
)
342 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
343 /* but anyway, gcc produces better code with full-sized ints */
344 unsigned d3
, d2
, d1
, d0
;
349 /* Possible ways to approx. divide by 10 */
350 /* gcc -O2 replaces multiply with shifts and adds */
351 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
352 // (x * 0x67) >> 10: 1100111
353 // (x * 0x34) >> 9: 110100 - same
354 // (x * 0x1a) >> 8: 11010 - same
355 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
357 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
358 q
= (d0
* 0xcd) >> 11;
361 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
362 q
= (d1
* 0xcd) >> 11;
372 q
= (d3
* 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
379 /* No inlining helps gcc to use registers better */
380 static noinline
char* put_dec(char *buf
, unsigned long long num
)
385 return put_dec_trunc(buf
, num
);
386 rem
= do_div(num
, 100000);
387 buf
= put_dec_full(buf
, rem
);
391 #define ZEROPAD 1 /* pad with zero */
392 #define SIGN 2 /* unsigned/signed long */
393 #define PLUS 4 /* show plus */
394 #define SPACE 8 /* space if plus */
395 #define LEFT 16 /* left justified */
396 #define SMALL 32 /* Must be 32 == 0x20 */
397 #define SPECIAL 64 /* 0x */
399 static char *number(char *buf
, char *end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
401 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
402 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
407 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
410 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
411 * produces same digits or (maybe lowercased) letters */
412 locase
= (type
& SMALL
);
417 if ((signed long long) num
< 0) {
419 num
= - (signed long long) num
;
421 } else if (type
& PLUS
) {
424 } else if (type
& SPACE
) {
435 /* generate full string in tmp[], in reverse order */
439 /* Generic code, for any base:
441 tmp[i++] = (digits[do_div(num,base)] | locase);
444 else if (base
!= 10) { /* 8 or 16 */
447 if (base
== 16) shift
= 4;
449 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
452 } else { /* base 10 */
453 i
= put_dec(tmp
, num
) - tmp
;
456 /* printing 100 using %2d gives "100", not "00" */
459 /* leading space padding */
461 if (!(type
& (ZEROPAD
+LEFT
))) {
474 /* "0x" / "0" prefix */
481 *buf
= ('X' | locase
);
485 /* zero or space padding */
486 if (!(type
& LEFT
)) {
487 char c
= (type
& ZEROPAD
) ? '0' : ' ';
488 while (--size
>= 0) {
494 /* hmm even more zero padding? */
495 while (i
<= --precision
) {
500 /* actual digits of result */
506 /* trailing space padding */
507 while (--size
>= 0) {
515 static char *string(char *buf
, char *end
, char *s
, int field_width
, int precision
, int flags
)
519 if ((unsigned long)s
< PAGE_SIZE
)
522 len
= strnlen(s
, precision
);
524 if (!(flags
& LEFT
)) {
525 while (len
< field_width
--) {
531 for (i
= 0; i
< len
; ++i
) {
536 while (len
< field_width
--) {
544 static char *symbol_string(char *buf
, char *end
, void *ptr
, int field_width
, int precision
, int flags
)
546 unsigned long value
= (unsigned long) ptr
;
547 #ifdef CONFIG_KALLSYMS
548 char sym
[KSYM_SYMBOL_LEN
];
549 sprint_symbol(sym
, value
);
550 return string(buf
, end
, sym
, field_width
, precision
, flags
);
552 field_width
= 2*sizeof(void *);
553 flags
|= SPECIAL
| SMALL
| ZEROPAD
;
554 return number(buf
, end
, value
, 16, field_width
, precision
, flags
);
558 static char *resource_string(char *buf
, char *end
, struct resource
*res
, int field_width
, int precision
, int flags
)
560 #ifndef IO_RSRC_PRINTK_SIZE
561 #define IO_RSRC_PRINTK_SIZE 4
564 #ifndef MEM_RSRC_PRINTK_SIZE
565 #define MEM_RSRC_PRINTK_SIZE 8
568 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
569 char sym
[4*sizeof(resource_size_t
) + 8];
570 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
573 if (res
->flags
& IORESOURCE_IO
)
574 size
= IO_RSRC_PRINTK_SIZE
;
575 else if (res
->flags
& IORESOURCE_MEM
)
576 size
= MEM_RSRC_PRINTK_SIZE
;
579 p
= number(p
, pend
, res
->start
, 16, size
, -1, SPECIAL
| SMALL
| ZEROPAD
);
581 p
= number(p
, pend
, res
->end
, 16, size
, -1, SPECIAL
| SMALL
| ZEROPAD
);
585 return string(buf
, end
, sym
, field_width
, precision
, flags
);
588 static char *mac_address_string(char *buf
, char *end
, u8
*addr
, int field_width
,
589 int precision
, int flags
)
591 char mac_addr
[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
595 for (i
= 0; i
< 6; i
++) {
596 p
= pack_hex_byte(p
, addr
[i
]);
597 if (!(flags
& SPECIAL
) && i
!= 5)
602 return string(buf
, end
, mac_addr
, field_width
, precision
, flags
& ~SPECIAL
);
605 static char *ip6_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
606 int precision
, int flags
)
608 char ip6_addr
[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
612 for (i
= 0; i
< 8; i
++) {
613 p
= pack_hex_byte(p
, addr
[2 * i
]);
614 p
= pack_hex_byte(p
, addr
[2 * i
+ 1]);
615 if (!(flags
& SPECIAL
) && i
!= 7)
620 return string(buf
, end
, ip6_addr
, field_width
, precision
, flags
& ~SPECIAL
);
623 static char *ip4_addr_string(char *buf
, char *end
, u8
*addr
, int field_width
,
624 int precision
, int flags
)
626 char ip4_addr
[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
627 char temp
[3]; /* hold each IP quad in reverse order */
631 for (i
= 0; i
< 4; i
++) {
632 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
633 /* reverse the digits in the quad */
641 return string(buf
, end
, ip4_addr
, field_width
, precision
, flags
& ~SPECIAL
);
645 * Show a '%p' thing. A kernel extension is that the '%p' is followed
646 * by an extra set of alphanumeric characters that are extended format
649 * Right now we handle:
651 * - 'F' For symbolic function descriptor pointers
652 * - 'S' For symbolic direct pointers
653 * - 'R' For a struct resource pointer, it prints the range of
654 * addresses (not the name nor the flags)
655 * - 'M' For a 6-byte MAC address, it prints the address in the
656 * usual colon-separated hex notation
657 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
658 * decimal for v4 and colon separated network-order 16 bit hex for v6)
659 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
662 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
663 * function pointers are really function descriptors, which contain a
664 * pointer to the real address.
666 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
, int field_width
, int precision
, int flags
)
669 return string(buf
, end
, "(null)", field_width
, precision
, flags
);
673 ptr
= dereference_function_descriptor(ptr
);
676 return symbol_string(buf
, end
, ptr
, field_width
, precision
, flags
);
678 return resource_string(buf
, end
, ptr
, field_width
, precision
, flags
);
683 return mac_address_string(buf
, end
, ptr
, field_width
, precision
, flags
);
689 return ip6_addr_string(buf
, end
, ptr
, field_width
, precision
, flags
);
691 return ip4_addr_string(buf
, end
, ptr
, field_width
, precision
, flags
);
696 if (field_width
== -1) {
697 field_width
= 2*sizeof(void *);
700 return number(buf
, end
, (unsigned long) ptr
, 16, field_width
, precision
, flags
);
704 * vsnprintf - Format a string and place it in a buffer
705 * @buf: The buffer to place the result into
706 * @size: The size of the buffer, including the trailing null space
707 * @fmt: The format string to use
708 * @args: Arguments for the format string
710 * This function follows C99 vsnprintf, but has some extensions:
711 * %pS output the name of a text symbol
712 * %pF output the name of a function pointer
713 * %pR output the address range in a struct resource
715 * The return value is the number of characters which would
716 * be generated for the given input, excluding the trailing
717 * '\0', as per ISO C99. If you want to have the exact
718 * number of characters written into @buf as return value
719 * (not including the trailing '\0'), use vscnprintf(). If the
720 * return is greater than or equal to @size, the resulting
721 * string is truncated.
723 * Call this function if you are already dealing with a va_list.
724 * You probably want snprintf() instead.
726 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
728 unsigned long long num
;
732 int flags
; /* flags to number() */
734 int field_width
; /* width of output field */
735 int precision
; /* min. # of digits for integers; max
736 number of chars for from string */
737 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
738 /* 'z' support added 23/7/1999 S.H. */
739 /* 'z' changed to 'Z' --davidm 1/25/99 */
740 /* 't' added for ptrdiff_t */
742 /* Reject out-of-range values early. Large positive sizes are
743 used for unknown buffer sizes. */
744 if (unlikely((int) size
< 0)) {
745 /* There can be only one.. */
746 static char warn
= 1;
755 /* Make sure end is always >= buf */
761 for (; *fmt
; ++fmt
) {
772 ++fmt
; /* this also skips first '%' */
774 case '-': flags
|= LEFT
; goto repeat
;
775 case '+': flags
|= PLUS
; goto repeat
;
776 case ' ': flags
|= SPACE
; goto repeat
;
777 case '#': flags
|= SPECIAL
; goto repeat
;
778 case '0': flags
|= ZEROPAD
; goto repeat
;
781 /* get field width */
784 field_width
= skip_atoi(&fmt
);
785 else if (*fmt
== '*') {
787 /* it's the next argument */
788 field_width
= va_arg(args
, int);
789 if (field_width
< 0) {
790 field_width
= -field_width
;
795 /* get the precision */
800 precision
= skip_atoi(&fmt
);
801 else if (*fmt
== '*') {
803 /* it's the next argument */
804 precision
= va_arg(args
, int);
810 /* get the conversion qualifier */
812 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
813 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
816 if (qualifier
== 'l' && *fmt
== 'l') {
827 if (!(flags
& LEFT
)) {
828 while (--field_width
> 0) {
834 c
= (unsigned char) va_arg(args
, int);
838 while (--field_width
> 0) {
846 str
= string(str
, end
, va_arg(args
, char *), field_width
, precision
, flags
);
850 str
= pointer(fmt
+1, str
, end
,
851 va_arg(args
, void *),
852 field_width
, precision
, flags
);
853 /* Skip all alphanumeric pointer suffixes */
854 while (isalnum(fmt
[1]))
860 * What does C99 say about the overflow case here? */
861 if (qualifier
== 'l') {
862 long * ip
= va_arg(args
, long *);
864 } else if (qualifier
== 'Z' || qualifier
== 'z') {
865 size_t * ip
= va_arg(args
, size_t *);
868 int * ip
= va_arg(args
, int *);
879 /* integer number formats - set up the flags and "break" */
909 if (qualifier
== 'L')
910 num
= va_arg(args
, long long);
911 else if (qualifier
== 'l') {
912 num
= va_arg(args
, unsigned long);
914 num
= (signed long) num
;
915 } else if (qualifier
== 'Z' || qualifier
== 'z') {
916 num
= va_arg(args
, size_t);
917 } else if (qualifier
== 't') {
918 num
= va_arg(args
, ptrdiff_t);
919 } else if (qualifier
== 'h') {
920 num
= (unsigned short) va_arg(args
, int);
922 num
= (signed short) num
;
924 num
= va_arg(args
, unsigned int);
926 num
= (signed int) num
;
928 str
= number(str
, end
, num
, base
,
929 field_width
, precision
, flags
);
937 /* the trailing null byte doesn't count towards the total */
940 EXPORT_SYMBOL(vsnprintf
);
943 * vscnprintf - Format a string and place it in a buffer
944 * @buf: The buffer to place the result into
945 * @size: The size of the buffer, including the trailing null space
946 * @fmt: The format string to use
947 * @args: Arguments for the format string
949 * The return value is the number of characters which have been written into
950 * the @buf not including the trailing '\0'. If @size is <= 0 the function
953 * Call this function if you are already dealing with a va_list.
954 * You probably want scnprintf() instead.
956 * See the vsnprintf() documentation for format string extensions over C99.
958 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
962 i
=vsnprintf(buf
,size
,fmt
,args
);
963 return (i
>= size
) ? (size
- 1) : i
;
965 EXPORT_SYMBOL(vscnprintf
);
968 * snprintf - Format a string and place it in a buffer
969 * @buf: The buffer to place the result into
970 * @size: The size of the buffer, including the trailing null space
971 * @fmt: The format string to use
972 * @...: Arguments for the format string
974 * The return value is the number of characters which would be
975 * generated for the given input, excluding the trailing null,
976 * as per ISO C99. If the return is greater than or equal to
977 * @size, the resulting string is truncated.
979 * See the vsnprintf() documentation for format string extensions over C99.
981 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
987 i
=vsnprintf(buf
,size
,fmt
,args
);
991 EXPORT_SYMBOL(snprintf
);
994 * scnprintf - Format a string and place it in a buffer
995 * @buf: The buffer to place the result into
996 * @size: The size of the buffer, including the trailing null space
997 * @fmt: The format string to use
998 * @...: Arguments for the format string
1000 * The return value is the number of characters written into @buf not including
1001 * the trailing '\0'. If @size is <= 0 the function returns 0.
1004 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
1009 va_start(args
, fmt
);
1010 i
= vsnprintf(buf
, size
, fmt
, args
);
1012 return (i
>= size
) ? (size
- 1) : i
;
1014 EXPORT_SYMBOL(scnprintf
);
1017 * vsprintf - Format a string and place it in a buffer
1018 * @buf: The buffer to place the result into
1019 * @fmt: The format string to use
1020 * @args: Arguments for the format string
1022 * The function returns the number of characters written
1023 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1026 * Call this function if you are already dealing with a va_list.
1027 * You probably want sprintf() instead.
1029 * See the vsnprintf() documentation for format string extensions over C99.
1031 int vsprintf(char *buf
, const char *fmt
, va_list args
)
1033 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
1035 EXPORT_SYMBOL(vsprintf
);
1038 * sprintf - Format a string and place it in a buffer
1039 * @buf: The buffer to place the result into
1040 * @fmt: The format string to use
1041 * @...: Arguments for the format string
1043 * The function returns the number of characters written
1044 * into @buf. Use snprintf() or scnprintf() in order to avoid
1047 * See the vsnprintf() documentation for format string extensions over C99.
1049 int sprintf(char * buf
, const char *fmt
, ...)
1054 va_start(args
, fmt
);
1055 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
1059 EXPORT_SYMBOL(sprintf
);
1062 * vsscanf - Unformat a buffer into a list of arguments
1063 * @buf: input buffer
1064 * @fmt: format of buffer
1067 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
1069 const char *str
= buf
;
1078 while(*fmt
&& *str
) {
1079 /* skip any white space in format */
1080 /* white space in format matchs any amount of
1081 * white space, including none, in the input.
1083 if (isspace(*fmt
)) {
1084 while (isspace(*fmt
))
1086 while (isspace(*str
))
1090 /* anything that is not a conversion must match exactly */
1091 if (*fmt
!= '%' && *fmt
) {
1092 if (*fmt
++ != *str
++)
1101 /* skip this conversion.
1102 * advance both strings to next white space
1105 while (!isspace(*fmt
) && *fmt
)
1107 while (!isspace(*str
) && *str
)
1112 /* get field width */
1115 field_width
= skip_atoi(&fmt
);
1117 /* get conversion qualifier */
1119 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
1120 *fmt
== 'Z' || *fmt
== 'z') {
1122 if (unlikely(qualifier
== *fmt
)) {
1123 if (qualifier
== 'h') {
1126 } else if (qualifier
== 'l') {
1141 char *s
= (char *) va_arg(args
,char*);
1142 if (field_width
== -1)
1146 } while (--field_width
> 0 && *str
);
1152 char *s
= (char *) va_arg(args
, char *);
1153 if(field_width
== -1)
1154 field_width
= INT_MAX
;
1155 /* first, skip leading white space in buffer */
1156 while (isspace(*str
))
1159 /* now copy until next white space */
1160 while (*str
&& !isspace(*str
) && field_width
--) {
1168 /* return number of characters read so far */
1170 int *i
= (int *)va_arg(args
,int*);
1188 /* looking for '%' in str */
1193 /* invalid format; stop here */
1197 /* have some sort of integer conversion.
1198 * first, skip white space in buffer.
1200 while (isspace(*str
))
1204 if (is_sign
&& digit
== '-')
1208 || (base
== 16 && !isxdigit(digit
))
1209 || (base
== 10 && !isdigit(digit
))
1210 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
1211 || (base
== 0 && !isdigit(digit
)))
1215 case 'H': /* that's 'hh' in format */
1217 signed char *s
= (signed char *) va_arg(args
,signed char *);
1218 *s
= (signed char) simple_strtol(str
,&next
,base
);
1220 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
1221 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
1226 short *s
= (short *) va_arg(args
,short *);
1227 *s
= (short) simple_strtol(str
,&next
,base
);
1229 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
1230 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
1235 long *l
= (long *) va_arg(args
,long *);
1236 *l
= simple_strtol(str
,&next
,base
);
1238 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
1239 *l
= simple_strtoul(str
,&next
,base
);
1244 long long *l
= (long long*) va_arg(args
,long long *);
1245 *l
= simple_strtoll(str
,&next
,base
);
1247 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
1248 *l
= simple_strtoull(str
,&next
,base
);
1254 size_t *s
= (size_t*) va_arg(args
,size_t*);
1255 *s
= (size_t) simple_strtoul(str
,&next
,base
);
1260 int *i
= (int *) va_arg(args
, int*);
1261 *i
= (int) simple_strtol(str
,&next
,base
);
1263 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
1264 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
1276 * Now we've come all the way through so either the input string or the
1277 * format ended. In the former case, there can be a %n at the current
1278 * position in the format that needs to be filled.
1280 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
1281 int *p
= (int *)va_arg(args
, int *);
1287 EXPORT_SYMBOL(vsscanf
);
1290 * sscanf - Unformat a buffer into a list of arguments
1291 * @buf: input buffer
1292 * @fmt: formatting of buffer
1293 * @...: resulting arguments
1295 int sscanf(const char * buf
, const char * fmt
, ...)
1301 i
= vsscanf(buf
,fmt
,args
);
1305 EXPORT_SYMBOL(sscanf
);