]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - lib/vsprintf.c
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
);
129 static int skip_atoi(const char **s
)
134 i
= i
*10 + *((*s
)++) - '0';
138 /* Decimal conversion is by far the most typical, and is used
139 * for /proc and /sys data. This directly impacts e.g. top performance
140 * with many processes running. We optimize it for speed
142 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
143 * (with permission from the author, Douglas W. Jones). */
145 /* Formats correctly any integer in [0,99999].
146 * Outputs from one to five digits depending on input.
147 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
148 static char* put_dec_trunc(char *buf
, unsigned q
)
150 unsigned d3
, d2
, d1
, d0
;
155 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
156 q
= (d0
* 0xcd) >> 11;
158 *buf
++ = d0
+ '0'; /* least significant digit */
159 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
161 q
= (d1
* 0xcd) >> 11;
163 *buf
++ = d1
+ '0'; /* next digit */
166 if ((d2
!= 0) || (d3
!= 0)) {
169 *buf
++ = d2
+ '0'; /* next digit */
173 q
= (d3
* 0xcd) >> 11;
175 *buf
++ = d3
+ '0'; /* next digit */
177 *buf
++ = q
+ '0'; /* most sign. digit */
183 /* Same with if's removed. Always emits five digits */
184 static char* put_dec_full(char *buf
, unsigned q
)
186 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
187 /* but anyway, gcc produces better code with full-sized ints */
188 unsigned d3
, d2
, d1
, d0
;
193 /* Possible ways to approx. divide by 10 */
194 /* gcc -O2 replaces multiply with shifts and adds */
195 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
196 // (x * 0x67) >> 10: 1100111
197 // (x * 0x34) >> 9: 110100 - same
198 // (x * 0x1a) >> 8: 11010 - same
199 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
201 d0
= 6*(d3
+ d2
+ d1
) + (q
& 0xf);
202 q
= (d0
* 0xcd) >> 11;
205 d1
= q
+ 9*d3
+ 5*d2
+ d1
;
206 q
= (d1
* 0xcd) >> 11;
216 q
= (d3
* 0xcd) >> 11; /* - shorter code */
217 /* q = (d3 * 0x67) >> 10; - would also work */
223 /* No inlining helps gcc to use registers better */
224 static noinline
char* put_dec(char *buf
, unsigned long long num
)
229 return put_dec_trunc(buf
, num
);
230 rem
= do_div(num
, 100000);
231 buf
= put_dec_full(buf
, rem
);
235 #define ZEROPAD 1 /* pad with zero */
236 #define SIGN 2 /* unsigned/signed long */
237 #define PLUS 4 /* show plus */
238 #define SPACE 8 /* space if plus */
239 #define LEFT 16 /* left justified */
240 #define SPECIAL 32 /* 0x */
241 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
243 static char *number(char *buf
, char *end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
247 /* we are called with base 8, 10 or 16, only, thus don't need "g..." */
248 static const char small_digits
[] = "0123456789abcdefx"; /* "ghijklmnopqrstuvwxyz"; */
249 static const char large_digits
[] = "0123456789ABCDEFX"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
250 int need_pfx
= ((type
& SPECIAL
) && base
!= 10);
253 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
256 if (base
< 2 || base
> 36)
260 if ((signed long long) num
< 0) {
262 num
= - (signed long long) num
;
264 } else if (type
& PLUS
) {
267 } else if (type
& SPACE
) {
278 /* generate full string in tmp[], in reverse order */
282 /* Generic code, for any base:
284 tmp[i++] = digits[do_div(num,base)];
287 else if (base
!= 10) { /* 8 or 16 */
290 if (base
== 16) shift
= 4;
292 tmp
[i
++] = digits
[((unsigned char)num
) & mask
];
295 } else { /* base 10 */
296 i
= put_dec(tmp
, num
) - tmp
;
299 /* printing 100 using %2d gives "100", not "00" */
302 /* leading space padding */
304 if (!(type
& (ZEROPAD
+LEFT
))) {
317 /* "0x" / "0" prefix */
324 *buf
= digits
[16]; /* for arbitrary base: digits[33]; */
328 /* zero or space padding */
329 if (!(type
& LEFT
)) {
330 char c
= (type
& ZEROPAD
) ? '0' : ' ';
331 while (--size
>= 0) {
337 /* hmm even more zero padding? */
338 while (i
<= --precision
) {
343 /* actual digits of result */
349 /* trailing space padding */
350 while (--size
>= 0) {
359 * vsnprintf - Format a string and place it in a buffer
360 * @buf: The buffer to place the result into
361 * @size: The size of the buffer, including the trailing null space
362 * @fmt: The format string to use
363 * @args: Arguments for the format string
365 * The return value is the number of characters which would
366 * be generated for the given input, excluding the trailing
367 * '\0', as per ISO C99. If you want to have the exact
368 * number of characters written into @buf as return value
369 * (not including the trailing '\0'), use vscnprintf(). If the
370 * return is greater than or equal to @size, the resulting
371 * string is truncated.
373 * Call this function if you are already dealing with a va_list.
374 * You probably want snprintf() instead.
376 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
379 unsigned long long num
;
384 int flags
; /* flags to number() */
386 int field_width
; /* width of output field */
387 int precision
; /* min. # of digits for integers; max
388 number of chars for from string */
389 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
390 /* 'z' support added 23/7/1999 S.H. */
391 /* 'z' changed to 'Z' --davidm 1/25/99 */
392 /* 't' added for ptrdiff_t */
394 /* Reject out-of-range values early. Large positive sizes are
395 used for unknown buffer sizes. */
396 if (unlikely((int) size
< 0)) {
397 /* There can be only one.. */
398 static char warn
= 1;
407 /* Make sure end is always >= buf */
413 for (; *fmt
; ++fmt
) {
424 ++fmt
; /* this also skips first '%' */
426 case '-': flags
|= LEFT
; goto repeat
;
427 case '+': flags
|= PLUS
; goto repeat
;
428 case ' ': flags
|= SPACE
; goto repeat
;
429 case '#': flags
|= SPECIAL
; goto repeat
;
430 case '0': flags
|= ZEROPAD
; goto repeat
;
433 /* get field width */
436 field_width
= skip_atoi(&fmt
);
437 else if (*fmt
== '*') {
439 /* it's the next argument */
440 field_width
= va_arg(args
, int);
441 if (field_width
< 0) {
442 field_width
= -field_width
;
447 /* get the precision */
452 precision
= skip_atoi(&fmt
);
453 else if (*fmt
== '*') {
455 /* it's the next argument */
456 precision
= va_arg(args
, int);
462 /* get the conversion qualifier */
464 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
465 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
468 if (qualifier
== 'l' && *fmt
== 'l') {
479 if (!(flags
& LEFT
)) {
480 while (--field_width
> 0) {
486 c
= (unsigned char) va_arg(args
, int);
490 while (--field_width
> 0) {
498 s
= va_arg(args
, char *);
499 if ((unsigned long)s
< PAGE_SIZE
)
502 len
= strnlen(s
, precision
);
504 if (!(flags
& LEFT
)) {
505 while (len
< field_width
--) {
511 for (i
= 0; i
< len
; ++i
) {
516 while (len
< field_width
--) {
524 if (field_width
== -1) {
525 field_width
= 2*sizeof(void *);
528 str
= number(str
, end
,
529 (unsigned long) va_arg(args
, void *),
530 16, field_width
, precision
, flags
);
536 * What does C99 say about the overflow case here? */
537 if (qualifier
== 'l') {
538 long * ip
= va_arg(args
, long *);
540 } else if (qualifier
== 'Z' || qualifier
== 'z') {
541 size_t * ip
= va_arg(args
, size_t *);
544 int * ip
= va_arg(args
, int *);
555 /* integer number formats - set up the flags and "break" */
585 if (qualifier
== 'L')
586 num
= va_arg(args
, long long);
587 else if (qualifier
== 'l') {
588 num
= va_arg(args
, unsigned long);
590 num
= (signed long) num
;
591 } else if (qualifier
== 'Z' || qualifier
== 'z') {
592 num
= va_arg(args
, size_t);
593 } else if (qualifier
== 't') {
594 num
= va_arg(args
, ptrdiff_t);
595 } else if (qualifier
== 'h') {
596 num
= (unsigned short) va_arg(args
, int);
598 num
= (signed short) num
;
600 num
= va_arg(args
, unsigned int);
602 num
= (signed int) num
;
604 str
= number(str
, end
, num
, base
,
605 field_width
, precision
, flags
);
613 /* the trailing null byte doesn't count towards the total */
617 EXPORT_SYMBOL(vsnprintf
);
620 * vscnprintf - Format a string and place it in a buffer
621 * @buf: The buffer to place the result into
622 * @size: The size of the buffer, including the trailing null space
623 * @fmt: The format string to use
624 * @args: Arguments for the format string
626 * The return value is the number of characters which have been written into
627 * the @buf not including the trailing '\0'. If @size is <= 0 the function
630 * Call this function if you are already dealing with a va_list.
631 * You probably want scnprintf() instead.
633 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
637 i
=vsnprintf(buf
,size
,fmt
,args
);
638 return (i
>= size
) ? (size
- 1) : i
;
641 EXPORT_SYMBOL(vscnprintf
);
644 * snprintf - Format a string and place it in a buffer
645 * @buf: The buffer to place the result into
646 * @size: The size of the buffer, including the trailing null space
647 * @fmt: The format string to use
648 * @...: Arguments for the format string
650 * The return value is the number of characters which would be
651 * generated for the given input, excluding the trailing null,
652 * as per ISO C99. If the return is greater than or equal to
653 * @size, the resulting string is truncated.
655 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
661 i
=vsnprintf(buf
,size
,fmt
,args
);
666 EXPORT_SYMBOL(snprintf
);
669 * scnprintf - Format a string and place it in a buffer
670 * @buf: The buffer to place the result into
671 * @size: The size of the buffer, including the trailing null space
672 * @fmt: The format string to use
673 * @...: Arguments for the format string
675 * The return value is the number of characters written into @buf not including
676 * the trailing '\0'. If @size is <= 0 the function returns 0.
679 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
685 i
= vsnprintf(buf
, size
, fmt
, args
);
687 return (i
>= size
) ? (size
- 1) : i
;
689 EXPORT_SYMBOL(scnprintf
);
692 * vsprintf - Format a string and place it in a buffer
693 * @buf: The buffer to place the result into
694 * @fmt: The format string to use
695 * @args: Arguments for the format string
697 * The function returns the number of characters written
698 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
701 * Call this function if you are already dealing with a va_list.
702 * You probably want sprintf() instead.
704 int vsprintf(char *buf
, const char *fmt
, va_list args
)
706 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
709 EXPORT_SYMBOL(vsprintf
);
712 * sprintf - Format a string and place it in a buffer
713 * @buf: The buffer to place the result into
714 * @fmt: The format string to use
715 * @...: Arguments for the format string
717 * The function returns the number of characters written
718 * into @buf. Use snprintf() or scnprintf() in order to avoid
721 int sprintf(char * buf
, const char *fmt
, ...)
727 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
732 EXPORT_SYMBOL(sprintf
);
735 * vsscanf - Unformat a buffer into a list of arguments
737 * @fmt: format of buffer
740 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
742 const char *str
= buf
;
751 while(*fmt
&& *str
) {
752 /* skip any white space in format */
753 /* white space in format matchs any amount of
754 * white space, including none, in the input.
757 while (isspace(*fmt
))
759 while (isspace(*str
))
763 /* anything that is not a conversion must match exactly */
764 if (*fmt
!= '%' && *fmt
) {
765 if (*fmt
++ != *str
++)
774 /* skip this conversion.
775 * advance both strings to next white space
778 while (!isspace(*fmt
) && *fmt
)
780 while (!isspace(*str
) && *str
)
785 /* get field width */
788 field_width
= skip_atoi(&fmt
);
790 /* get conversion qualifier */
792 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
793 *fmt
== 'Z' || *fmt
== 'z') {
795 if (unlikely(qualifier
== *fmt
)) {
796 if (qualifier
== 'h') {
799 } else if (qualifier
== 'l') {
814 char *s
= (char *) va_arg(args
,char*);
815 if (field_width
== -1)
819 } while (--field_width
> 0 && *str
);
825 char *s
= (char *) va_arg(args
, char *);
826 if(field_width
== -1)
827 field_width
= INT_MAX
;
828 /* first, skip leading white space in buffer */
829 while (isspace(*str
))
832 /* now copy until next white space */
833 while (*str
&& !isspace(*str
) && field_width
--) {
841 /* return number of characters read so far */
843 int *i
= (int *)va_arg(args
,int*);
861 /* looking for '%' in str */
866 /* invalid format; stop here */
870 /* have some sort of integer conversion.
871 * first, skip white space in buffer.
873 while (isspace(*str
))
877 if (is_sign
&& digit
== '-')
881 || (base
== 16 && !isxdigit(digit
))
882 || (base
== 10 && !isdigit(digit
))
883 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
884 || (base
== 0 && !isdigit(digit
)))
888 case 'H': /* that's 'hh' in format */
890 signed char *s
= (signed char *) va_arg(args
,signed char *);
891 *s
= (signed char) simple_strtol(str
,&next
,base
);
893 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
894 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
899 short *s
= (short *) va_arg(args
,short *);
900 *s
= (short) simple_strtol(str
,&next
,base
);
902 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
903 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
908 long *l
= (long *) va_arg(args
,long *);
909 *l
= simple_strtol(str
,&next
,base
);
911 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
912 *l
= simple_strtoul(str
,&next
,base
);
917 long long *l
= (long long*) va_arg(args
,long long *);
918 *l
= simple_strtoll(str
,&next
,base
);
920 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
921 *l
= simple_strtoull(str
,&next
,base
);
927 size_t *s
= (size_t*) va_arg(args
,size_t*);
928 *s
= (size_t) simple_strtoul(str
,&next
,base
);
933 int *i
= (int *) va_arg(args
, int*);
934 *i
= (int) simple_strtol(str
,&next
,base
);
936 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
937 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
949 * Now we've come all the way through so either the input string or the
950 * format ended. In the former case, there can be a %n at the current
951 * position in the format that needs to be filled.
953 if (*fmt
== '%' && *(fmt
+ 1) == 'n') {
954 int *p
= (int *)va_arg(args
, int *);
961 EXPORT_SYMBOL(vsscanf
);
964 * sscanf - Unformat a buffer into a list of arguments
966 * @fmt: formatting of buffer
967 * @...: resulting arguments
969 int sscanf(const char * buf
, const char * fmt
, ...)
975 i
= vsscanf(buf
,fmt
,args
);
980 EXPORT_SYMBOL(sscanf
);
983 /* Simplified asprintf. */
984 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
991 len
= vsnprintf(NULL
, 0, fmt
, aq
);
994 p
= kmalloc(len
+1, gfp
);
998 vsnprintf(p
, len
+1, fmt
, ap
);
1002 EXPORT_SYMBOL(kvasprintf
);
1004 char *kasprintf(gfp_t gfp
, const char *fmt
, ...)
1010 p
= kvasprintf(gfp
, fmt
, ap
);
1015 EXPORT_SYMBOL(kasprintf
);