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>
26 #include <asm/page.h> /* for PAGE_SIZE */
27 #include <asm/div64.h>
30 * simple_strtoul - convert a string to an unsigned long
31 * @cp: The start of the string
32 * @endp: A pointer to the end of the parsed string will be placed here
33 * @base: The number base to use
35 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
37 unsigned long result
= 0,value
;
44 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
49 } else if (base
== 16) {
50 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
53 while (isxdigit(*cp
) &&
54 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
55 result
= result
*base
+ value
;
63 EXPORT_SYMBOL(simple_strtoul
);
66 * simple_strtol - convert a string to a signed long
67 * @cp: The start of the string
68 * @endp: A pointer to the end of the parsed string will be placed here
69 * @base: The number base to use
71 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
74 return -simple_strtoul(cp
+1,endp
,base
);
75 return simple_strtoul(cp
,endp
,base
);
78 EXPORT_SYMBOL(simple_strtol
);
81 * simple_strtoull - convert a string to an unsigned long 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 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
88 unsigned long long result
= 0,value
;
95 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
100 } else if (base
== 16) {
101 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
104 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
105 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
106 result
= result
*base
+ value
;
114 EXPORT_SYMBOL(simple_strtoull
);
117 * simple_strtoll - convert a string to a signed long long
118 * @cp: The start of the string
119 * @endp: A pointer to the end of the parsed string will be placed here
120 * @base: The number base to use
122 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
125 return -simple_strtoull(cp
+1,endp
,base
);
126 return simple_strtoull(cp
,endp
,base
);
131 * strict_strtoul - convert a string to an unsigned long strictly
132 * @cp: The string to be converted
133 * @base: The number base to use
134 * @res: The converted result value
136 * strict_strtoul converts a string to an unsigned long only if the
137 * string is really an unsigned long string, any string containing
138 * any invalid char at the tail will be rejected and -EINVAL is returned,
139 * only a newline char at the tail is acceptible because people generally
140 * change a module parameter in the following way:
142 * echo 1024 > /sys/module/e1000/parameters/copybreak
144 * echo will append a newline to the tail.
146 * It returns 0 if conversion is successful and *res is set to the converted
147 * value, otherwise it returns -EINVAL and *res is set to 0.
149 * simple_strtoul just ignores the successive invalid characters and
150 * return the converted value of prefix part of the string.
152 int strict_strtoul(const char *cp
, unsigned int base
, unsigned long *res
);
155 * strict_strtol - convert a string to a long strictly
156 * @cp: The string to be converted
157 * @base: The number base to use
158 * @res: The converted result value
160 * strict_strtol is similiar to strict_strtoul, but it allows the first
161 * character of a string is '-'.
163 * It returns 0 if conversion is successful and *res is set to the converted
164 * value, otherwise it returns -EINVAL and *res is set to 0.
166 int strict_strtol(const char *cp
, unsigned int base
, long *res
);
169 * strict_strtoull - convert a string to an unsigned long long strictly
170 * @cp: The string to be converted
171 * @base: The number base to use
172 * @res: The converted result value
174 * strict_strtoull converts a string to an unsigned long long only if the
175 * string is really an unsigned long long string, any string containing
176 * any invalid char at the tail will be rejected and -EINVAL is returned,
177 * only a newline char at the tail is acceptible because people generally
178 * change a module parameter in the following way:
180 * echo 1024 > /sys/module/e1000/parameters/copybreak
182 * echo will append a newline to the tail of the string.
184 * It returns 0 if conversion is successful and *res is set to the converted
185 * value, otherwise it returns -EINVAL and *res is set to 0.
187 * simple_strtoull just ignores the successive invalid characters and
188 * return the converted value of prefix part of the string.
190 int strict_strtoull(const char *cp
, unsigned int base
, unsigned long long *res
);
193 * strict_strtoll - convert a string to a long long strictly
194 * @cp: The string to be converted
195 * @base: The number base to use
196 * @res: The converted result value
198 * strict_strtoll is similiar to strict_strtoull, but it allows the first
199 * character of a string is '-'.
201 * It returns 0 if conversion is successful and *res is set to the converted
202 * value, otherwise it returns -EINVAL and *res is set to 0.
204 int strict_strtoll(const char *cp
, unsigned int base
, long long *res
);
206 #define define_strict_strtoux(type, valtype) \
207 int strict_strtou##type(const char *cp, unsigned int base, valtype *res)\
218 val = simple_strtoul(cp, &tail, base); \
219 if ((*tail == '\0') || \
220 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {\
228 #define define_strict_strtox(type, valtype) \
229 int strict_strto##type(const char *cp, unsigned int base, valtype *res) \
233 ret = strict_strtou##type(cp+1, base, res); \
237 ret = strict_strtou##type(cp, base, res); \
242 define_strict_strtoux(l, unsigned long)
243 define_strict_strtox(l
, long)
244 define_strict_strtoux(ll
, unsigned long long)
245 define_strict_strtox(ll
, long long)
247 EXPORT_SYMBOL(strict_strtoul
);
248 EXPORT_SYMBOL(strict_strtol
);
249 EXPORT_SYMBOL(strict_strtoll
);
250 EXPORT_SYMBOL(strict_strtoull
);
252 static int skip_atoi(const char **s
)
257 i
= i
*10 + *((*s
)++) - '0';
261 /* Decimal conversion is by far the most typical, and is used
262 * for /proc and /sys data. This directly impacts e.g. top performance
263 * with many processes running. We optimize it for speed
265 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
266 * (with permission from the author, Douglas W. Jones). */
268 /* Formats correctly any integer in [0,99999].
269 * Outputs from one to five digits depending on input.
270 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
271 static char* put_dec_trunc(char *buf
, unsigned q
)
273 unsigned d3
, d2
, d1
, d0
;
278 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
279 q
= (d0
* 0xcd) >> 11;
281 *buf
++ = d0
+ '0'; /* least significant digit */
282 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
284 q
= (d1
* 0xcd) >> 11;
286 *buf
++ = d1
+ '0'; /* next digit */
289 if ((d2
!= 0) || (d3
!= 0)) {
292 *buf
++ = d2
+ '0'; /* next digit */
296 q
= (d3
* 0xcd) >> 11;
298 *buf
++ = d3
+ '0'; /* next digit */
300 *buf
++ = q
+ '0'; /* most sign. digit */
306 /* Same with if's removed. Always emits five digits */
307 static char* put_dec_full(char *buf
, unsigned q
)
309 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
310 /* but anyway, gcc produces better code with full-sized ints */
311 unsigned d3
, d2
, d1
, d0
;
316 /* Possible ways to approx. divide by 10 */
317 /* gcc -O2 replaces multiply with shifts and adds */
318 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
319 // (x * 0x67) >> 10: 1100111
320 // (x * 0x34) >> 9: 110100 - same
321 // (x * 0x1a) >> 8: 11010 - same
322 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
324 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
325 q
= (d0
* 0xcd) >> 11;
328 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
329 q
= (d1
* 0xcd) >> 11;
339 q
= (d3
* 0xcd) >> 11; /* - shorter code */
340 /* q = (d3 * 0x67) >> 10; - would also work */
346 /* No inlining helps gcc to use registers better */
347 static noinline
char* put_dec(char *buf
, unsigned long long num
)
352 return put_dec_trunc(buf
, num
);
353 rem
= do_div(num
, 100000);
354 buf
= put_dec_full(buf
, rem
);
358 #define ZEROPAD 1 /* pad with zero */
359 #define SIGN 2 /* unsigned/signed long */
360 #define PLUS 4 /* show plus */
361 #define SPACE 8 /* space if plus */
362 #define LEFT 16 /* left justified */
363 #define SPECIAL 32 /* 0x */
364 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
366 static char *number(char *buf
, char *end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
370 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
371 static const char small_digits
[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
372 static const char large_digits
[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
373 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
376 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
379 if (base
< 2 || base
> 36)
383 if ((signed long long) num
< 0) {
385 num
= - (signed long long) num
;
387 } else if (type
& PLUS
) {
390 } else if (type
& SPACE
) {
401 /* generate full string in tmp[], in reverse order */
405 /* Generic code, for any base:
407 tmp[i++] = digits[do_div(num,base)];
410 else if (base
!= 10) { /* 8 or 16 */
413 if (base
== 16) shift
= 4;
415 tmp
[i
++] = digits
[((unsigned char)num
) & mask
];
418 } else { /* base 10 */
419 i
= put_dec(tmp
, num
) - tmp
;
422 /* printing 100 using %2d gives "100", not "00" */
425 /* leading space padding */
427 if (!(type
& (ZEROPAD
+LEFT
))) {
440 /* "0x" / "0" prefix */
447 *buf
= digits
[16]; /* for arbitrary base: digits[33]; */
451 /* zero or space padding */
452 if (!(type
& LEFT
)) {
453 char c
= (type
& ZEROPAD
) ? '0' : ' ';
454 while (--size
>= 0) {
460 /* hmm even more zero padding? */
461 while (i
<= --precision
) {
466 /* actual digits of result */
472 /* trailing space padding */
473 while (--size
>= 0) {
482 * vsnprintf - Format a string and place it in a buffer
483 * @buf: The buffer to place the result into
484 * @size: The size of the buffer, including the trailing null space
485 * @fmt: The format string to use
486 * @args: Arguments for the format string
488 * The return value is the number of characters which would
489 * be generated for the given input, excluding the trailing
490 * '\0', as per ISO C99. If you want to have the exact
491 * number of characters written into @buf as return value
492 * (not including the trailing '\0'), use vscnprintf(). If the
493 * return is greater than or equal to @size, the resulting
494 * string is truncated.
496 * Call this function if you are already dealing with a va_list.
497 * You probably want snprintf() instead.
499 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
502 unsigned long long num
;
507 int flags
; /* flags to number() */
509 int field_width
; /* width of output field */
510 int precision
; /* min. # of digits for integers; max
511 number of chars for from string */
512 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
513 /* 'z' support added 23/7/1999 S.H. */
514 /* 'z' changed to 'Z' --davidm 1/25/99 */
515 /* 't' added for ptrdiff_t */
517 /* Reject out-of-range values early. Large positive sizes are
518 used for unknown buffer sizes. */
519 if (unlikely((int) size
< 0)) {
520 /* There can be only one.. */
521 static char warn
= 1;
530 /* Make sure end is always >= buf */
536 for (; *fmt
; ++fmt
) {
547 ++fmt
; /* this also skips first '%' */
549 case '-': flags
|= LEFT
; goto repeat
;
550 case '+': flags
|= PLUS
; goto repeat
;
551 case ' ': flags
|= SPACE
; goto repeat
;
552 case '#': flags
|= SPECIAL
; goto repeat
;
553 case '0': flags
|= ZEROPAD
; goto repeat
;
556 /* get field width */
559 field_width
= skip_atoi(&fmt
);
560 else if (*fmt
== '*') {
562 /* it's the next argument */
563 field_width
= va_arg(args
, int);
564 if (field_width
< 0) {
565 field_width
= -field_width
;
570 /* get the precision */
575 precision
= skip_atoi(&fmt
);
576 else if (*fmt
== '*') {
578 /* it's the next argument */
579 precision
= va_arg(args
, int);
585 /* get the conversion qualifier */
587 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
588 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
591 if (qualifier
== 'l' && *fmt
== 'l') {
602 if (!(flags
& LEFT
)) {
603 while (--field_width
> 0) {
609 c
= (unsigned char) va_arg(args
, int);
613 while (--field_width
> 0) {
621 s
= va_arg(args
, char *);
622 if ((unsigned long)s
< PAGE_SIZE
)
625 len
= strnlen(s
, precision
);
627 if (!(flags
& LEFT
)) {
628 while (len
< field_width
--) {
634 for (i
= 0; i
< len
; ++i
) {
639 while (len
< field_width
--) {
647 if (field_width
== -1) {
648 field_width
= 2*sizeof(void *);
651 str
= number(str
, end
,
652 (unsigned long) va_arg(args
, void *),
653 16, field_width
, precision
, flags
);
659 * What does C99 say about the overflow case here? */
660 if (qualifier
== 'l') {
661 long * ip
= va_arg(args
, long *);
663 } else if (qualifier
== 'Z' || qualifier
== 'z') {
664 size_t * ip
= va_arg(args
, size_t *);
667 int * ip
= va_arg(args
, int *);
678 /* integer number formats - set up the flags and "break" */
708 if (qualifier
== 'L')
709 num
= va_arg(args
, long long);
710 else if (qualifier
== 'l') {
711 num
= va_arg(args
, unsigned long);
713 num
= (signed long) num
;
714 } else if (qualifier
== 'Z' || qualifier
== 'z') {
715 num
= va_arg(args
, size_t);
716 } else if (qualifier
== 't') {
717 num
= va_arg(args
, ptrdiff_t);
718 } else if (qualifier
== 'h') {
719 num
= (unsigned short) va_arg(args
, int);
721 num
= (signed short) num
;
723 num
= va_arg(args
, unsigned int);
725 num
= (signed int) num
;
727 str
= number(str
, end
, num
, base
,
728 field_width
, precision
, flags
);
736 /* the trailing null byte doesn't count towards the total */
740 EXPORT_SYMBOL(vsnprintf
);
743 * vscnprintf - Format a string and place it in a buffer
744 * @buf: The buffer to place the result into
745 * @size: The size of the buffer, including the trailing null space
746 * @fmt: The format string to use
747 * @args: Arguments for the format string
749 * The return value is the number of characters which have been written into
750 * the @buf not including the trailing '\0'. If @size is <= 0 the function
753 * Call this function if you are already dealing with a va_list.
754 * You probably want scnprintf() instead.
756 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
760 i
=vsnprintf(buf
,size
,fmt
,args
);
761 return (i
>= size
) ? (size
- 1) : i
;
764 EXPORT_SYMBOL(vscnprintf
);
767 * snprintf - Format a string and place it in a buffer
768 * @buf: The buffer to place the result into
769 * @size: The size of the buffer, including the trailing null space
770 * @fmt: The format string to use
771 * @...: Arguments for the format string
773 * The return value is the number of characters which would be
774 * generated for the given input, excluding the trailing null,
775 * as per ISO C99. If the return is greater than or equal to
776 * @size, the resulting string is truncated.
778 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
784 i
=vsnprintf(buf
,size
,fmt
,args
);
789 EXPORT_SYMBOL(snprintf
);
792 * scnprintf - Format a string and place it in a buffer
793 * @buf: The buffer to place the result into
794 * @size: The size of the buffer, including the trailing null space
795 * @fmt: The format string to use
796 * @...: Arguments for the format string
798 * The return value is the number of characters written into @buf not including
799 * the trailing '\0'. If @size is <= 0 the function returns 0.
802 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
808 i
= vsnprintf(buf
, size
, fmt
, args
);
810 return (i
>= size
) ? (size
- 1) : i
;
812 EXPORT_SYMBOL(scnprintf
);
815 * vsprintf - Format a string and place it in a buffer
816 * @buf: The buffer to place the result into
817 * @fmt: The format string to use
818 * @args: Arguments for the format string
820 * The function returns the number of characters written
821 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
824 * Call this function if you are already dealing with a va_list.
825 * You probably want sprintf() instead.
827 int vsprintf(char *buf
, const char *fmt
, va_list args
)
829 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
832 EXPORT_SYMBOL(vsprintf
);
835 * sprintf - Format a string and place it in a buffer
836 * @buf: The buffer to place the result into
837 * @fmt: The format string to use
838 * @...: Arguments for the format string
840 * The function returns the number of characters written
841 * into @buf. Use snprintf() or scnprintf() in order to avoid
844 int sprintf(char * buf
, const char *fmt
, ...)
850 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
855 EXPORT_SYMBOL(sprintf
);
858 * vsscanf - Unformat a buffer into a list of arguments
860 * @fmt: format of buffer
863 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
865 const char *str
= buf
;
874 while(*fmt
&& *str
) {
875 /* skip any white space in format */
876 /* white space in format matchs any amount of
877 * white space, including none, in the input.
880 while (isspace(*fmt
))
882 while (isspace(*str
))
886 /* anything that is not a conversion must match exactly */
887 if (*fmt
!= '%' && *fmt
) {
888 if (*fmt
++ != *str
++)
897 /* skip this conversion.
898 * advance both strings to next white space
901 while (!isspace(*fmt
) && *fmt
)
903 while (!isspace(*str
) && *str
)
908 /* get field width */
911 field_width
= skip_atoi(&fmt
);
913 /* get conversion qualifier */
915 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
916 *fmt
== 'Z' || *fmt
== 'z') {
918 if (unlikely(qualifier
== *fmt
)) {
919 if (qualifier
== 'h') {
922 } else if (qualifier
== 'l') {
937 char *s
= (char *) va_arg(args
,char*);
938 if (field_width
== -1)
942 } while (--field_width
> 0 && *str
);
948 char *s
= (char *) va_arg(args
, char *);
949 if(field_width
== -1)
950 field_width
= INT_MAX
;
951 /* first, skip leading white space in buffer */
952 while (isspace(*str
))
955 /* now copy until next white space */
956 while (*str
&& !isspace(*str
) && field_width
--) {
964 /* return number of characters read so far */
966 int *i
= (int *)va_arg(args
,int*);
984 /* looking for '%' in str */
989 /* invalid format; stop here */
993 /* have some sort of integer conversion.
994 * first, skip white space in buffer.
996 while (isspace(*str
))
1000 if (is_sign
&& digit
== '-')
1004 || (base
== 16 && !isxdigit(digit
))
1005 || (base
== 10 && !isdigit(digit
))
1006 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
1007 || (base
== 0 && !isdigit(digit
)))
1011 case 'H': /* that's 'hh' in format */
1013 signed char *s
= (signed char *) va_arg(args
,signed char *);
1014 *s
= (signed char) simple_strtol(str
,&next
,base
);
1016 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
1017 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
1022 short *s
= (short *) va_arg(args
,short *);
1023 *s
= (short) simple_strtol(str
,&next
,base
);
1025 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
1026 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
1031 long *l
= (long *) va_arg(args
,long *);
1032 *l
= simple_strtol(str
,&next
,base
);
1034 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
1035 *l
= simple_strtoul(str
,&next
,base
);
1040 long long *l
= (long long*) va_arg(args
,long long *);
1041 *l
= simple_strtoll(str
,&next
,base
);
1043 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
1044 *l
= simple_strtoull(str
,&next
,base
);
1050 size_t *s
= (size_t*) va_arg(args
,size_t*);
1051 *s
= (size_t) simple_strtoul(str
,&next
,base
);
1056 int *i
= (int *) va_arg(args
, int*);
1057 *i
= (int) simple_strtol(str
,&next
,base
);
1059 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
1060 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
1072 * Now we've come all the way through so either the input string or the
1073 * format ended. In the former case, there can be a %n at the current
1074 * position in the format that needs to be filled.
1076 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
1077 int *p
= (int *)va_arg(args
, int *);
1084 EXPORT_SYMBOL(vsscanf
);
1087 * sscanf - Unformat a buffer into a list of arguments
1088 * @buf: input buffer
1089 * @fmt: formatting of buffer
1090 * @...: resulting arguments
1092 int sscanf(const char * buf
, const char * fmt
, ...)
1098 i
= vsscanf(buf
,fmt
,args
);
1103 EXPORT_SYMBOL(sscanf
);