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/div64.h>
29 * simple_strtoul - convert a string to an unsigned long
30 * @cp: The start of the string
31 * @endp: A pointer to the end of the parsed string will be placed here
32 * @base: The number base to use
34 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
36 unsigned long result
= 0,value
;
43 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
48 } else if (base
== 16) {
49 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
52 while (isxdigit(*cp
) &&
53 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
54 result
= result
*base
+ value
;
62 EXPORT_SYMBOL(simple_strtoul
);
65 * simple_strtol - convert a string to a signed long
66 * @cp: The start of the string
67 * @endp: A pointer to the end of the parsed string will be placed here
68 * @base: The number base to use
70 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
73 return -simple_strtoul(cp
+1,endp
,base
);
74 return simple_strtoul(cp
,endp
,base
);
77 EXPORT_SYMBOL(simple_strtol
);
80 * simple_strtoull - convert a string to an unsigned long long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
87 unsigned long long result
= 0,value
;
94 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
99 } else if (base
== 16) {
100 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
103 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
104 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
105 result
= result
*base
+ value
;
113 EXPORT_SYMBOL(simple_strtoull
);
116 * simple_strtoll - convert a string to a signed long long
117 * @cp: The start of the string
118 * @endp: A pointer to the end of the parsed string will be placed here
119 * @base: The number base to use
121 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
124 return -simple_strtoull(cp
+1,endp
,base
);
125 return simple_strtoull(cp
,endp
,base
);
128 static int skip_atoi(const char **s
)
133 i
= i
*10 + *((*s
)++) - '0';
137 #define ZEROPAD 1 /* pad with zero */
138 #define SIGN 2 /* unsigned/signed long */
139 #define PLUS 4 /* show plus */
140 #define SPACE 8 /* space if plus */
141 #define LEFT 16 /* left justified */
142 #define SPECIAL 32 /* 0x */
143 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
145 static char * number(char * buf
, char * end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
149 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
150 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
153 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
156 if (base
< 2 || base
> 36)
158 c
= (type
& ZEROPAD
) ? '0' : ' ';
161 if ((signed long long) num
< 0) {
163 num
= - (signed long long) num
;
165 } else if (type
& PLUS
) {
168 } else if (type
& SPACE
) {
173 if (type
& SPECIAL
) {
182 else while (num
!= 0)
183 tmp
[i
++] = digits
[do_div(num
,base
)];
187 if (!(type
&(ZEROPAD
+LEFT
))) {
199 if (type
& SPECIAL
) {
204 } else if (base
==16) {
213 if (!(type
& LEFT
)) {
220 while (i
< precision
--) {
239 * vsnprintf - Format a string and place it in a buffer
240 * @buf: The buffer to place the result into
241 * @size: The size of the buffer, including the trailing null space
242 * @fmt: The format string to use
243 * @args: Arguments for the format string
245 * The return value is the number of characters which would
246 * be generated for the given input, excluding the trailing
247 * '\0', as per ISO C99. If you want to have the exact
248 * number of characters written into @buf as return value
249 * (not including the trailing '\0'), use vscnprintf. If the
250 * return is greater than or equal to @size, the resulting
251 * string is truncated.
253 * Call this function if you are already dealing with a va_list.
254 * You probably want snprintf instead.
256 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
259 unsigned long long num
;
264 int flags
; /* flags to number() */
266 int field_width
; /* width of output field */
267 int precision
; /* min. # of digits for integers; max
268 number of chars for from string */
269 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
270 /* 'z' support added 23/7/1999 S.H. */
271 /* 'z' changed to 'Z' --davidm 1/25/99 */
272 /* 't' added for ptrdiff_t */
274 /* Reject out-of-range values early */
275 if (unlikely((int) size
< 0)) {
276 /* There can be only one.. */
284 end
= buf
+ size
- 1;
288 size
= end
- buf
+ 1;
291 for (; *fmt
; ++fmt
) {
302 ++fmt
; /* this also skips first '%' */
304 case '-': flags
|= LEFT
; goto repeat
;
305 case '+': flags
|= PLUS
; goto repeat
;
306 case ' ': flags
|= SPACE
; goto repeat
;
307 case '#': flags
|= SPECIAL
; goto repeat
;
308 case '0': flags
|= ZEROPAD
; goto repeat
;
311 /* get field width */
314 field_width
= skip_atoi(&fmt
);
315 else if (*fmt
== '*') {
317 /* it's the next argument */
318 field_width
= va_arg(args
, int);
319 if (field_width
< 0) {
320 field_width
= -field_width
;
325 /* get the precision */
330 precision
= skip_atoi(&fmt
);
331 else if (*fmt
== '*') {
333 /* it's the next argument */
334 precision
= va_arg(args
, int);
340 /* get the conversion qualifier */
342 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
343 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
346 if (qualifier
== 'l' && *fmt
== 'l') {
357 if (!(flags
& LEFT
)) {
358 while (--field_width
> 0) {
364 c
= (unsigned char) va_arg(args
, int);
368 while (--field_width
> 0) {
376 s
= va_arg(args
, char *);
377 if ((unsigned long)s
< PAGE_SIZE
)
380 len
= strnlen(s
, precision
);
382 if (!(flags
& LEFT
)) {
383 while (len
< field_width
--) {
389 for (i
= 0; i
< len
; ++i
) {
394 while (len
< field_width
--) {
402 if (field_width
== -1) {
403 field_width
= 2*sizeof(void *);
406 str
= number(str
, end
,
407 (unsigned long) va_arg(args
, void *),
408 16, field_width
, precision
, flags
);
414 * What does C99 say about the overflow case here? */
415 if (qualifier
== 'l') {
416 long * ip
= va_arg(args
, long *);
418 } else if (qualifier
== 'Z' || qualifier
== 'z') {
419 size_t * ip
= va_arg(args
, size_t *);
422 int * ip
= va_arg(args
, int *);
433 /* integer number formats - set up the flags and "break" */
463 if (qualifier
== 'L')
464 num
= va_arg(args
, long long);
465 else if (qualifier
== 'l') {
466 num
= va_arg(args
, unsigned long);
468 num
= (signed long) num
;
469 } else if (qualifier
== 'Z' || qualifier
== 'z') {
470 num
= va_arg(args
, size_t);
471 } else if (qualifier
== 't') {
472 num
= va_arg(args
, ptrdiff_t);
473 } else if (qualifier
== 'h') {
474 num
= (unsigned short) va_arg(args
, int);
476 num
= (signed short) num
;
478 num
= va_arg(args
, unsigned int);
480 num
= (signed int) num
;
482 str
= number(str
, end
, num
, base
,
483 field_width
, precision
, flags
);
488 /* don't write out a null byte if the buf size is zero */
490 /* the trailing null byte doesn't count towards the total
496 EXPORT_SYMBOL(vsnprintf
);
499 * vscnprintf - Format a string and place it in a buffer
500 * @buf: The buffer to place the result into
501 * @size: The size of the buffer, including the trailing null space
502 * @fmt: The format string to use
503 * @args: Arguments for the format string
505 * The return value is the number of characters which have been written into
506 * the @buf not including the trailing '\0'. If @size is <= 0 the function
509 * Call this function if you are already dealing with a va_list.
510 * You probably want scnprintf instead.
512 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
516 i
=vsnprintf(buf
,size
,fmt
,args
);
517 return (i
>= size
) ? (size
- 1) : i
;
520 EXPORT_SYMBOL(vscnprintf
);
523 * snprintf - Format a string and place it in a buffer
524 * @buf: The buffer to place the result into
525 * @size: The size of the buffer, including the trailing null space
526 * @fmt: The format string to use
527 * @...: Arguments for the format string
529 * The return value is the number of characters which would be
530 * generated for the given input, excluding the trailing null,
531 * as per ISO C99. If the return is greater than or equal to
532 * @size, the resulting string is truncated.
534 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
540 i
=vsnprintf(buf
,size
,fmt
,args
);
545 EXPORT_SYMBOL(snprintf
);
548 * scnprintf - Format a string and place it in a buffer
549 * @buf: The buffer to place the result into
550 * @size: The size of the buffer, including the trailing null space
551 * @fmt: The format string to use
552 * @...: Arguments for the format string
554 * The return value is the number of characters written into @buf not including
555 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
556 * greater than or equal to @size, the resulting string is truncated.
559 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
565 i
= vsnprintf(buf
, size
, fmt
, args
);
567 return (i
>= size
) ? (size
- 1) : i
;
569 EXPORT_SYMBOL(scnprintf
);
572 * vsprintf - Format a string and place it in a buffer
573 * @buf: The buffer to place the result into
574 * @fmt: The format string to use
575 * @args: Arguments for the format string
577 * The function returns the number of characters written
578 * into @buf. Use vsnprintf or vscnprintf in order to avoid
581 * Call this function if you are already dealing with a va_list.
582 * You probably want sprintf instead.
584 int vsprintf(char *buf
, const char *fmt
, va_list args
)
586 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
589 EXPORT_SYMBOL(vsprintf
);
592 * sprintf - Format a string and place it in a buffer
593 * @buf: The buffer to place the result into
594 * @fmt: The format string to use
595 * @...: Arguments for the format string
597 * The function returns the number of characters written
598 * into @buf. Use snprintf or scnprintf in order to avoid
601 int sprintf(char * buf
, const char *fmt
, ...)
607 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
612 EXPORT_SYMBOL(sprintf
);
615 * vsscanf - Unformat a buffer into a list of arguments
617 * @fmt: format of buffer
620 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
622 const char *str
= buf
;
631 while(*fmt
&& *str
) {
632 /* skip any white space in format */
633 /* white space in format matchs any amount of
634 * white space, including none, in the input.
637 while (isspace(*fmt
))
639 while (isspace(*str
))
643 /* anything that is not a conversion must match exactly */
644 if (*fmt
!= '%' && *fmt
) {
645 if (*fmt
++ != *str
++)
654 /* skip this conversion.
655 * advance both strings to next white space
658 while (!isspace(*fmt
) && *fmt
)
660 while (!isspace(*str
) && *str
)
665 /* get field width */
668 field_width
= skip_atoi(&fmt
);
670 /* get conversion qualifier */
672 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
673 *fmt
== 'Z' || *fmt
== 'z') {
675 if (unlikely(qualifier
== *fmt
)) {
676 if (qualifier
== 'h') {
679 } else if (qualifier
== 'l') {
694 char *s
= (char *) va_arg(args
,char*);
695 if (field_width
== -1)
699 } while (--field_width
> 0 && *str
);
705 char *s
= (char *) va_arg(args
, char *);
706 if(field_width
== -1)
707 field_width
= INT_MAX
;
708 /* first, skip leading white space in buffer */
709 while (isspace(*str
))
712 /* now copy until next white space */
713 while (*str
&& !isspace(*str
) && field_width
--) {
721 /* return number of characters read so far */
723 int *i
= (int *)va_arg(args
,int*);
741 /* looking for '%' in str */
746 /* invalid format; stop here */
750 /* have some sort of integer conversion.
751 * first, skip white space in buffer.
753 while (isspace(*str
))
757 if (is_sign
&& digit
== '-')
761 || (base
== 16 && !isxdigit(digit
))
762 || (base
== 10 && !isdigit(digit
))
763 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
764 || (base
== 0 && !isdigit(digit
)))
768 case 'H': /* that's 'hh' in format */
770 signed char *s
= (signed char *) va_arg(args
,signed char *);
771 *s
= (signed char) simple_strtol(str
,&next
,base
);
773 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
774 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
779 short *s
= (short *) va_arg(args
,short *);
780 *s
= (short) simple_strtol(str
,&next
,base
);
782 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
783 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
788 long *l
= (long *) va_arg(args
,long *);
789 *l
= simple_strtol(str
,&next
,base
);
791 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
792 *l
= simple_strtoul(str
,&next
,base
);
797 long long *l
= (long long*) va_arg(args
,long long *);
798 *l
= simple_strtoll(str
,&next
,base
);
800 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
801 *l
= simple_strtoull(str
,&next
,base
);
807 size_t *s
= (size_t*) va_arg(args
,size_t*);
808 *s
= (size_t) simple_strtoul(str
,&next
,base
);
813 int *i
= (int *) va_arg(args
, int*);
814 *i
= (int) simple_strtol(str
,&next
,base
);
816 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
817 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
830 EXPORT_SYMBOL(vsscanf
);
833 * sscanf - Unformat a buffer into a list of arguments
835 * @fmt: formatting of buffer
836 * @...: resulting arguments
838 int sscanf(const char * buf
, const char * fmt
, ...)
844 i
= vsscanf(buf
,fmt
,args
);
849 EXPORT_SYMBOL(sscanf
);