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 #define ZEROPAD 1 /* pad with zero */
139 #define SIGN 2 /* unsigned/signed long */
140 #define PLUS 4 /* show plus */
141 #define SPACE 8 /* space if plus */
142 #define LEFT 16 /* left justified */
143 #define SPECIAL 32 /* 0x */
144 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
146 static char * number(char * buf
, char * end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
150 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
151 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
154 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
157 if (base
< 2 || base
> 36)
159 c
= (type
& ZEROPAD
) ? '0' : ' ';
162 if ((signed long long) num
< 0) {
164 num
= - (signed long long) num
;
166 } else if (type
& PLUS
) {
169 } else if (type
& SPACE
) {
174 if (type
& SPECIAL
) {
183 else while (num
!= 0)
184 tmp
[i
++] = digits
[do_div(num
,base
)];
188 if (!(type
&(ZEROPAD
+LEFT
))) {
200 if (type
& SPECIAL
) {
205 } else if (base
==16) {
214 if (!(type
& LEFT
)) {
221 while (i
< precision
--) {
240 * vsnprintf - Format a string and place it in a buffer
241 * @buf: The buffer to place the result into
242 * @size: The size of the buffer, including the trailing null space
243 * @fmt: The format string to use
244 * @args: Arguments for the format string
246 * The return value is the number of characters which would
247 * be generated for the given input, excluding the trailing
248 * '\0', as per ISO C99. If you want to have the exact
249 * number of characters written into @buf as return value
250 * (not including the trailing '\0'), use vscnprintf(). If the
251 * return is greater than or equal to @size, the resulting
252 * string is truncated.
254 * Call this function if you are already dealing with a va_list.
255 * You probably want snprintf() instead.
257 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
260 unsigned long long num
;
265 int flags
; /* flags to number() */
267 int field_width
; /* width of output field */
268 int precision
; /* min. # of digits for integers; max
269 number of chars for from string */
270 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
271 /* 'z' support added 23/7/1999 S.H. */
272 /* 'z' changed to 'Z' --davidm 1/25/99 */
273 /* 't' added for ptrdiff_t */
275 /* Reject out-of-range values early. Large positive sizes are
276 used for unknown buffer sizes. */
277 if (unlikely((int) size
< 0)) {
278 /* There can be only one.. */
288 /* Make sure end is always >= buf */
294 for (; *fmt
; ++fmt
) {
305 ++fmt
; /* this also skips first '%' */
307 case '-': flags
|= LEFT
; goto repeat
;
308 case '+': flags
|= PLUS
; goto repeat
;
309 case ' ': flags
|= SPACE
; goto repeat
;
310 case '#': flags
|= SPECIAL
; goto repeat
;
311 case '0': flags
|= ZEROPAD
; goto repeat
;
314 /* get field width */
317 field_width
= skip_atoi(&fmt
);
318 else if (*fmt
== '*') {
320 /* it's the next argument */
321 field_width
= va_arg(args
, int);
322 if (field_width
< 0) {
323 field_width
= -field_width
;
328 /* get the precision */
333 precision
= skip_atoi(&fmt
);
334 else if (*fmt
== '*') {
336 /* it's the next argument */
337 precision
= va_arg(args
, int);
343 /* get the conversion qualifier */
345 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
346 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
349 if (qualifier
== 'l' && *fmt
== 'l') {
360 if (!(flags
& LEFT
)) {
361 while (--field_width
> 0) {
367 c
= (unsigned char) va_arg(args
, int);
371 while (--field_width
> 0) {
379 s
= va_arg(args
, char *);
380 if ((unsigned long)s
< PAGE_SIZE
)
383 len
= strnlen(s
, precision
);
385 if (!(flags
& LEFT
)) {
386 while (len
< field_width
--) {
392 for (i
= 0; i
< len
; ++i
) {
397 while (len
< field_width
--) {
405 if (field_width
== -1) {
406 field_width
= 2*sizeof(void *);
409 str
= number(str
, end
,
410 (unsigned long) va_arg(args
, void *),
411 16, field_width
, precision
, flags
);
417 * What does C99 say about the overflow case here? */
418 if (qualifier
== 'l') {
419 long * ip
= va_arg(args
, long *);
421 } else if (qualifier
== 'Z' || qualifier
== 'z') {
422 size_t * ip
= va_arg(args
, size_t *);
425 int * ip
= va_arg(args
, int *);
436 /* integer number formats - set up the flags and "break" */
466 if (qualifier
== 'L')
467 num
= va_arg(args
, long long);
468 else if (qualifier
== 'l') {
469 num
= va_arg(args
, unsigned long);
471 num
= (signed long) num
;
472 } else if (qualifier
== 'Z' || qualifier
== 'z') {
473 num
= va_arg(args
, size_t);
474 } else if (qualifier
== 't') {
475 num
= va_arg(args
, ptrdiff_t);
476 } else if (qualifier
== 'h') {
477 num
= (unsigned short) va_arg(args
, int);
479 num
= (signed short) num
;
481 num
= va_arg(args
, unsigned int);
483 num
= (signed int) num
;
485 str
= number(str
, end
, num
, base
,
486 field_width
, precision
, flags
);
494 /* the trailing null byte doesn't count towards the total */
498 EXPORT_SYMBOL(vsnprintf
);
501 * vscnprintf - Format a string and place it in a buffer
502 * @buf: The buffer to place the result into
503 * @size: The size of the buffer, including the trailing null space
504 * @fmt: The format string to use
505 * @args: Arguments for the format string
507 * The return value is the number of characters which have been written into
508 * the @buf not including the trailing '\0'. If @size is <= 0 the function
511 * Call this function if you are already dealing with a va_list.
512 * You probably want scnprintf() instead.
514 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
518 i
=vsnprintf(buf
,size
,fmt
,args
);
519 return (i
>= size
) ? (size
- 1) : i
;
522 EXPORT_SYMBOL(vscnprintf
);
525 * snprintf - Format a string and place it in a buffer
526 * @buf: The buffer to place the result into
527 * @size: The size of the buffer, including the trailing null space
528 * @fmt: The format string to use
529 * @...: Arguments for the format string
531 * The return value is the number of characters which would be
532 * generated for the given input, excluding the trailing null,
533 * as per ISO C99. If the return is greater than or equal to
534 * @size, the resulting string is truncated.
536 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
542 i
=vsnprintf(buf
,size
,fmt
,args
);
547 EXPORT_SYMBOL(snprintf
);
550 * scnprintf - Format a string and place it in a buffer
551 * @buf: The buffer to place the result into
552 * @size: The size of the buffer, including the trailing null space
553 * @fmt: The format string to use
554 * @...: Arguments for the format string
556 * The return value is the number of characters written into @buf not including
557 * the trailing '\0'. If @size is <= 0 the function returns 0.
560 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
566 i
= vsnprintf(buf
, size
, fmt
, args
);
568 return (i
>= size
) ? (size
- 1) : i
;
570 EXPORT_SYMBOL(scnprintf
);
573 * vsprintf - Format a string and place it in a buffer
574 * @buf: The buffer to place the result into
575 * @fmt: The format string to use
576 * @args: Arguments for the format string
578 * The function returns the number of characters written
579 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
582 * Call this function if you are already dealing with a va_list.
583 * You probably want sprintf() instead.
585 int vsprintf(char *buf
, const char *fmt
, va_list args
)
587 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
590 EXPORT_SYMBOL(vsprintf
);
593 * sprintf - Format a string and place it in a buffer
594 * @buf: The buffer to place the result into
595 * @fmt: The format string to use
596 * @...: Arguments for the format string
598 * The function returns the number of characters written
599 * into @buf. Use snprintf() or scnprintf() in order to avoid
602 int sprintf(char * buf
, const char *fmt
, ...)
608 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
613 EXPORT_SYMBOL(sprintf
);
616 * vsscanf - Unformat a buffer into a list of arguments
618 * @fmt: format of buffer
621 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
623 const char *str
= buf
;
632 while(*fmt
&& *str
) {
633 /* skip any white space in format */
634 /* white space in format matchs any amount of
635 * white space, including none, in the input.
638 while (isspace(*fmt
))
640 while (isspace(*str
))
644 /* anything that is not a conversion must match exactly */
645 if (*fmt
!= '%' && *fmt
) {
646 if (*fmt
++ != *str
++)
655 /* skip this conversion.
656 * advance both strings to next white space
659 while (!isspace(*fmt
) && *fmt
)
661 while (!isspace(*str
) && *str
)
666 /* get field width */
669 field_width
= skip_atoi(&fmt
);
671 /* get conversion qualifier */
673 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
674 *fmt
== 'Z' || *fmt
== 'z') {
676 if (unlikely(qualifier
== *fmt
)) {
677 if (qualifier
== 'h') {
680 } else if (qualifier
== 'l') {
695 char *s
= (char *) va_arg(args
,char*);
696 if (field_width
== -1)
700 } while (--field_width
> 0 && *str
);
706 char *s
= (char *) va_arg(args
, char *);
707 if(field_width
== -1)
708 field_width
= INT_MAX
;
709 /* first, skip leading white space in buffer */
710 while (isspace(*str
))
713 /* now copy until next white space */
714 while (*str
&& !isspace(*str
) && field_width
--) {
722 /* return number of characters read so far */
724 int *i
= (int *)va_arg(args
,int*);
742 /* looking for '%' in str */
747 /* invalid format; stop here */
751 /* have some sort of integer conversion.
752 * first, skip white space in buffer.
754 while (isspace(*str
))
758 if (is_sign
&& digit
== '-')
762 || (base
== 16 && !isxdigit(digit
))
763 || (base
== 10 && !isdigit(digit
))
764 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
765 || (base
== 0 && !isdigit(digit
)))
769 case 'H': /* that's 'hh' in format */
771 signed char *s
= (signed char *) va_arg(args
,signed char *);
772 *s
= (signed char) simple_strtol(str
,&next
,base
);
774 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
775 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
780 short *s
= (short *) va_arg(args
,short *);
781 *s
= (short) simple_strtol(str
,&next
,base
);
783 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
784 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
789 long *l
= (long *) va_arg(args
,long *);
790 *l
= simple_strtol(str
,&next
,base
);
792 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
793 *l
= simple_strtoul(str
,&next
,base
);
798 long long *l
= (long long*) va_arg(args
,long long *);
799 *l
= simple_strtoll(str
,&next
,base
);
801 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
802 *l
= simple_strtoull(str
,&next
,base
);
808 size_t *s
= (size_t*) va_arg(args
,size_t*);
809 *s
= (size_t) simple_strtoul(str
,&next
,base
);
814 int *i
= (int *) va_arg(args
, int*);
815 *i
= (int) simple_strtol(str
,&next
,base
);
817 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
818 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
831 EXPORT_SYMBOL(vsscanf
);
834 * sscanf - Unformat a buffer into a list of arguments
836 * @fmt: formatting of buffer
837 * @...: resulting arguments
839 int sscanf(const char * buf
, const char * fmt
, ...)
845 i
= vsscanf(buf
,fmt
,args
);
850 EXPORT_SYMBOL(sscanf
);
853 /* Simplified asprintf. */
854 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
861 len
= vsnprintf(NULL
, 0, fmt
, aq
);
864 p
= kmalloc(len
+1, gfp
);
868 vsnprintf(p
, len
+1, fmt
, ap
);
872 EXPORT_SYMBOL(kvasprintf
);
874 char *kasprintf(gfp_t gfp
, const char *fmt
, ...)
880 p
= kvasprintf(gfp
, fmt
, ap
);
885 EXPORT_SYMBOL(kasprintf
);