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>
28 #include <asm/page.h> /* for PAGE_SIZE */
29 #include <asm/div64.h>
31 /* Works only for digits and letters, but small and fast */
32 #define TOLOWER(x) ((x) | 0x20)
35 * simple_strtoul - convert a string to an unsigned long
36 * @cp: The start of the string
37 * @endp: A pointer to the end of the parsed string will be placed here
38 * @base: The number base to use
40 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
42 unsigned long result
= 0,value
;
49 if ((TOLOWER(*cp
) == 'x') && isxdigit(cp
[1])) {
54 } else if (base
== 16) {
55 if (cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
58 while (isxdigit(*cp
) &&
59 (value
= isdigit(*cp
) ? *cp
-'0' : TOLOWER(*cp
)-'a'+10) < base
) {
60 result
= result
*base
+ value
;
68 EXPORT_SYMBOL(simple_strtoul
);
71 * simple_strtol - convert a string to a signed long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
76 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
79 return -simple_strtoul(cp
+1,endp
,base
);
80 return simple_strtoul(cp
,endp
,base
);
83 EXPORT_SYMBOL(simple_strtol
);
86 * simple_strtoull - convert a string to an unsigned long long
87 * @cp: The start of the string
88 * @endp: A pointer to the end of the parsed string will be placed here
89 * @base: The number base to use
91 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
93 unsigned long long result
= 0,value
;
100 if ((TOLOWER(*cp
) == 'x') && isxdigit(cp
[1])) {
105 } else if (base
== 16) {
106 if (cp
[0] == '0' && TOLOWER(cp
[1]) == 'x')
110 && (value
= isdigit(*cp
) ? *cp
-'0' : TOLOWER(*cp
)-'a'+10) < base
) {
111 result
= result
*base
+ value
;
119 EXPORT_SYMBOL(simple_strtoull
);
122 * simple_strtoll - convert a string to a signed long long
123 * @cp: The start of the string
124 * @endp: A pointer to the end of the parsed string will be placed here
125 * @base: The number base to use
127 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
130 return -simple_strtoull(cp
+1,endp
,base
);
131 return simple_strtoull(cp
,endp
,base
);
134 static int skip_atoi(const char **s
)
139 i
= i
*10 + *((*s
)++) - '0';
143 /* Decimal conversion is by far the most typical, and is used
144 * for /proc and /sys data. This directly impacts e.g. top performance
145 * with many processes running. We optimize it for speed
147 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
148 * (with permission from the author, Douglas W. Jones). */
150 /* Formats correctly any integer in [0,99999].
151 * Outputs from one to five digits depending on input.
152 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
153 static char* put_dec_trunc(char *buf
, unsigned q
)
155 unsigned d3
, d2
, d1
, d0
;
160 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
161 q
= (d0
* 0xcd) >> 11;
163 *buf
++ = d0
+ '0'; /* least significant digit */
164 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
166 q
= (d1
* 0xcd) >> 11;
168 *buf
++ = d1
+ '0'; /* next digit */
171 if ((d2
!= 0) || (d3
!= 0)) {
174 *buf
++ = d2
+ '0'; /* next digit */
178 q
= (d3
* 0xcd) >> 11;
180 *buf
++ = d3
+ '0'; /* next digit */
182 *buf
++ = q
+ '0'; /* most sign. digit */
188 /* Same with if's removed. Always emits five digits */
189 static char* put_dec_full(char *buf
, unsigned q
)
191 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
192 /* but anyway, gcc produces better code with full-sized ints */
193 unsigned d3
, d2
, d1
, d0
;
198 /* Possible ways to approx. divide by 10 */
199 /* gcc -O2 replaces multiply with shifts and adds */
200 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
201 // (x * 0x67) >> 10: 1100111
202 // (x * 0x34) >> 9: 110100 - same
203 // (x * 0x1a) >> 8: 11010 - same
204 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
206 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
207 q
= (d0
* 0xcd) >> 11;
210 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
211 q
= (d1
* 0xcd) >> 11;
221 q
= (d3
* 0xcd) >> 11; /* - shorter code */
222 /* q = (d3 * 0x67) >> 10; - would also work */
228 /* No inlining helps gcc to use registers better */
229 static noinline
char* put_dec(char *buf
, unsigned long long num
)
234 return put_dec_trunc(buf
, num
);
235 rem
= do_div(num
, 100000);
236 buf
= put_dec_full(buf
, rem
);
240 #define ZEROPAD 1 /* pad with zero */
241 #define SIGN 2 /* unsigned/signed long */
242 #define PLUS 4 /* show plus */
243 #define SPACE 8 /* space if plus */
244 #define LEFT 16 /* left justified */
245 #define SMALL 32 /* Must be 32 == 0x20 */
246 #define SPECIAL 64 /* 0x */
248 static char *number(char *buf
, char *end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
250 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
251 static const char digits
[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
256 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
259 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
260 * produces same digits or (maybe lowercased) letters */
261 locase
= (type
& SMALL
);
266 if ((signed long long) num
< 0) {
268 num
= - (signed long long) num
;
270 } else if (type
& PLUS
) {
273 } else if (type
& SPACE
) {
284 /* generate full string in tmp[], in reverse order */
288 /* Generic code, for any base:
290 tmp[i++] = (digits[do_div(num,base)] | locase);
293 else if (base
!= 10) { /* 8 or 16 */
296 if (base
== 16) shift
= 4;
298 tmp
[i
++] = (digits
[((unsigned char)num
) & mask
] | locase
);
301 } else { /* base 10 */
302 i
= put_dec(tmp
, num
) - tmp
;
305 /* printing 100 using %2d gives "100", not "00" */
308 /* leading space padding */
310 if (!(type
& (ZEROPAD
+LEFT
))) {
323 /* "0x" / "0" prefix */
330 *buf
= ('X' | locase
);
334 /* zero or space padding */
335 if (!(type
& LEFT
)) {
336 char c
= (type
& ZEROPAD
) ? '0' : ' ';
337 while (--size
>= 0) {
343 /* hmm even more zero padding? */
344 while (i
<= --precision
) {
349 /* actual digits of result */
355 /* trailing space padding */
356 while (--size
>= 0) {
364 static char *string(char *buf
, char *end
, char *s
, int field_width
, int precision
, int flags
)
368 if ((unsigned long)s
< PAGE_SIZE
)
371 len
= strnlen(s
, precision
);
373 if (!(flags
& LEFT
)) {
374 while (len
< field_width
--) {
380 for (i
= 0; i
< len
; ++i
) {
385 while (len
< field_width
--) {
393 static char *mac_address_string(char *buf
, char *end
, u8
*addr
, int field_width
,
394 int precision
, int flags
)
396 char mac_addr
[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
400 for (i
= 0; i
< 6; i
++) {
401 p
= pack_hex_byte(p
, addr
[i
]);
402 if (!(flags
& SPECIAL
) && i
!= 5)
407 return string(buf
, end
, mac_addr
, field_width
, precision
, flags
& ~SPECIAL
);
411 * Show a '%p' thing. A kernel extension is that the '%p' is followed
412 * by an extra set of alphanumeric characters that are extended format
415 * - 'M' For a 6-byte MAC address, it prints the address in the
416 * usual colon-separated hex notation
418 static char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
, int field_width
, int precision
, int flags
)
422 return mac_address_string(buf
, end
, ptr
, field_width
, precision
, flags
);
425 if (field_width
== -1) {
426 field_width
= 2*sizeof(void *);
429 return number(buf
, end
, (unsigned long) ptr
, 16, field_width
, precision
, flags
);
433 * vsnprintf - Format a string and place it in a buffer
434 * @buf: The buffer to place the result into
435 * @size: The size of the buffer, including the trailing null space
436 * @fmt: The format string to use
437 * @args: Arguments for the format string
439 * The return value is the number of characters which would
440 * be generated for the given input, excluding the trailing
441 * '\0', as per ISO C99. If you want to have the exact
442 * number of characters written into @buf as return value
443 * (not including the trailing '\0'), use vscnprintf(). If the
444 * return is greater than or equal to @size, the resulting
445 * string is truncated.
447 * Call this function if you are already dealing with a va_list.
448 * You probably want snprintf() instead.
450 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
452 unsigned long long num
;
456 int flags
; /* flags to number() */
458 int field_width
; /* width of output field */
459 int precision
; /* min. # of digits for integers; max
460 number of chars for from string */
461 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
462 /* 'z' support added 23/7/1999 S.H. */
463 /* 'z' changed to 'Z' --davidm 1/25/99 */
464 /* 't' added for ptrdiff_t */
466 /* Reject out-of-range values early. Large positive sizes are
467 used for unknown buffer sizes. */
468 if (unlikely((int) size
< 0)) {
469 /* There can be only one.. */
470 static char warn
= 1;
479 /* Make sure end is always >= buf */
485 for (; *fmt
; ++fmt
) {
496 ++fmt
; /* this also skips first '%' */
498 case '-': flags
|= LEFT
; goto repeat
;
499 case '+': flags
|= PLUS
; goto repeat
;
500 case ' ': flags
|= SPACE
; goto repeat
;
501 case '#': flags
|= SPECIAL
; goto repeat
;
502 case '0': flags
|= ZEROPAD
; goto repeat
;
505 /* get field width */
508 field_width
= skip_atoi(&fmt
);
509 else if (*fmt
== '*') {
511 /* it's the next argument */
512 field_width
= va_arg(args
, int);
513 if (field_width
< 0) {
514 field_width
= -field_width
;
519 /* get the precision */
524 precision
= skip_atoi(&fmt
);
525 else if (*fmt
== '*') {
527 /* it's the next argument */
528 precision
= va_arg(args
, int);
534 /* get the conversion qualifier */
536 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
537 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
540 if (qualifier
== 'l' && *fmt
== 'l') {
551 if (!(flags
& LEFT
)) {
552 while (--field_width
> 0) {
558 c
= (unsigned char) va_arg(args
, int);
562 while (--field_width
> 0) {
570 str
= string(str
, end
, va_arg(args
, char *), field_width
, precision
, flags
);
574 str
= pointer(fmt
+1, str
, end
,
575 va_arg(args
, void *),
576 field_width
, precision
, flags
);
577 /* Skip all alphanumeric pointer suffixes */
578 while (isalnum(fmt
[1]))
584 * What does C99 say about the overflow case here? */
585 if (qualifier
== 'l') {
586 long * ip
= va_arg(args
, long *);
588 } else if (qualifier
== 'Z' || qualifier
== 'z') {
589 size_t * ip
= va_arg(args
, size_t *);
592 int * ip
= va_arg(args
, int *);
603 /* integer number formats - set up the flags and "break" */
633 if (qualifier
== 'L')
634 num
= va_arg(args
, long long);
635 else if (qualifier
== 'l') {
636 num
= va_arg(args
, unsigned long);
638 num
= (signed long) num
;
639 } else if (qualifier
== 'Z' || qualifier
== 'z') {
640 num
= va_arg(args
, size_t);
641 } else if (qualifier
== 't') {
642 num
= va_arg(args
, ptrdiff_t);
643 } else if (qualifier
== 'h') {
644 num
= (unsigned short) va_arg(args
, int);
646 num
= (signed short) num
;
648 num
= va_arg(args
, unsigned int);
650 num
= (signed int) num
;
652 str
= number(str
, end
, num
, base
,
653 field_width
, precision
, flags
);
661 /* the trailing null byte doesn't count towards the total */
665 EXPORT_SYMBOL(vsnprintf
);
668 * vscnprintf - Format a string and place it in a buffer
669 * @buf: The buffer to place the result into
670 * @size: The size of the buffer, including the trailing null space
671 * @fmt: The format string to use
672 * @args: Arguments for the format string
674 * The return value is the number of characters which have been written into
675 * the @buf not including the trailing '\0'. If @size is <= 0 the function
678 * Call this function if you are already dealing with a va_list.
679 * You probably want scnprintf() instead.
681 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
685 i
=vsnprintf(buf
,size
,fmt
,args
);
686 return (i
>= size
) ? (size
- 1) : i
;
689 EXPORT_SYMBOL(vscnprintf
);
692 * snprintf - Format a string and place it in a buffer
693 * @buf: The buffer to place the result into
694 * @size: The size of the buffer, including the trailing null space
695 * @fmt: The format string to use
696 * @...: Arguments for the format string
698 * The return value is the number of characters which would be
699 * generated for the given input, excluding the trailing null,
700 * as per ISO C99. If the return is greater than or equal to
701 * @size, the resulting string is truncated.
703 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
709 i
=vsnprintf(buf
,size
,fmt
,args
);
714 EXPORT_SYMBOL(snprintf
);
717 * scnprintf - Format a string and place it in a buffer
718 * @buf: The buffer to place the result into
719 * @size: The size of the buffer, including the trailing null space
720 * @fmt: The format string to use
721 * @...: Arguments for the format string
723 * The return value is the number of characters written into @buf not including
724 * the trailing '\0'. If @size is <= 0 the function returns 0.
727 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
733 i
= vsnprintf(buf
, size
, fmt
, args
);
735 return (i
>= size
) ? (size
- 1) : i
;
737 EXPORT_SYMBOL(scnprintf
);
740 * vsprintf - Format a string and place it in a buffer
741 * @buf: The buffer to place the result into
742 * @fmt: The format string to use
743 * @args: Arguments for the format string
745 * The function returns the number of characters written
746 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
749 * Call this function if you are already dealing with a va_list.
750 * You probably want sprintf() instead.
752 int vsprintf(char *buf
, const char *fmt
, va_list args
)
754 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
757 EXPORT_SYMBOL(vsprintf
);
760 * sprintf - Format a string and place it in a buffer
761 * @buf: The buffer to place the result into
762 * @fmt: The format string to use
763 * @...: Arguments for the format string
765 * The function returns the number of characters written
766 * into @buf. Use snprintf() or scnprintf() in order to avoid
769 int sprintf(char * buf
, const char *fmt
, ...)
775 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
780 EXPORT_SYMBOL(sprintf
);
783 * vsscanf - Unformat a buffer into a list of arguments
785 * @fmt: format of buffer
788 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
790 const char *str
= buf
;
799 while(*fmt
&& *str
) {
800 /* skip any white space in format */
801 /* white space in format matchs any amount of
802 * white space, including none, in the input.
805 while (isspace(*fmt
))
807 while (isspace(*str
))
811 /* anything that is not a conversion must match exactly */
812 if (*fmt
!= '%' && *fmt
) {
813 if (*fmt
++ != *str
++)
822 /* skip this conversion.
823 * advance both strings to next white space
826 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
)
828 while (!isspace(*str
) && *str
)
833 /* get field width */
836 field_width
= skip_atoi(&fmt
);
838 /* get conversion qualifier */
840 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
841 *fmt
== 'Z' || *fmt
== 'z') {
843 if (unlikely(qualifier
== *fmt
)) {
844 if (qualifier
== 'h') {
847 } else if (qualifier
== 'l') {
862 char *s
= (char *) va_arg(args
,char*);
863 if (field_width
== -1)
867 } while (--field_width
> 0 && *str
);
873 char *s
= (char *) va_arg(args
, char *);
874 if(field_width
== -1)
875 field_width
= INT_MAX
;
876 /* first, skip leading white space in buffer */
877 while (isspace(*str
))
880 /* now copy until next white space */
881 while (*str
&& !isspace(*str
) && field_width
--) {
889 /* return number of characters read so far */
891 int *i
= (int *)va_arg(args
,int*);
909 /* looking for '%' in str */
914 /* invalid format; stop here */
918 /* have some sort of integer conversion.
919 * first, skip white space in buffer.
921 while (isspace(*str
))
925 if (is_sign
&& digit
== '-')
929 || (base
== 16 && !isxdigit(digit
))
930 || (base
== 10 && !isdigit(digit
))
931 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
932 || (base
== 0 && !isdigit(digit
)))
936 case 'H': /* that's 'hh' in format */
938 signed char *s
= (signed char *) va_arg(args
,signed char *);
939 *s
= (signed char) simple_strtol(str
,&next
,base
);
941 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
942 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
947 short *s
= (short *) va_arg(args
,short *);
948 *s
= (short) simple_strtol(str
,&next
,base
);
950 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
951 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
956 long *l
= (long *) va_arg(args
,long *);
957 *l
= simple_strtol(str
,&next
,base
);
959 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
960 *l
= simple_strtoul(str
,&next
,base
);
965 long long *l
= (long long*) va_arg(args
,long long *);
966 *l
= simple_strtoll(str
,&next
,base
);
968 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
969 *l
= simple_strtoull(str
,&next
,base
);
975 size_t *s
= (size_t*) va_arg(args
,size_t*);
976 *s
= (size_t) simple_strtoul(str
,&next
,base
);
981 int *i
= (int *) va_arg(args
, int*);
982 *i
= (int) simple_strtol(str
,&next
,base
);
984 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
985 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
997 * Now we've come all the way through so either the input string or the
998 * format ended. In the former case, there can be a %n at the current
999 * position in the format that needs to be filled.
1001 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
1002 int *p
= (int *)va_arg(args
, int *);
1009 EXPORT_SYMBOL(vsscanf
);
1012 * sscanf - Unformat a buffer into a list of arguments
1013 * @buf: input buffer
1014 * @fmt: formatting of buffer
1015 * @...: resulting arguments
1017 int sscanf(const char * buf
, const char * fmt
, ...)
1023 i
= vsscanf(buf
,fmt
,args
);
1028 EXPORT_SYMBOL(sscanf
);
1031 /* Simplified asprintf. */
1032 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
1039 len
= vsnprintf(NULL
, 0, fmt
, aq
);
1042 p
= kmalloc(len
+1, gfp
);
1046 vsnprintf(p
, len
+1, fmt
, ap
);
1050 EXPORT_SYMBOL(kvasprintf
);
1052 char *kasprintf(gfp_t gfp
, const char *fmt
, ...)
1058 p
= kvasprintf(gfp
, fmt
, ap
);
1063 EXPORT_SYMBOL(kasprintf
);