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 ((*cp
== 'x') && isxdigit(cp
[1])) {
49 while (isxdigit(*cp
) &&
50 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
51 result
= result
*base
+ value
;
59 EXPORT_SYMBOL(simple_strtoul
);
62 * simple_strtol - convert a string to a signed long
63 * @cp: The start of the string
64 * @endp: A pointer to the end of the parsed string will be placed here
65 * @base: The number base to use
67 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
70 return -simple_strtoul(cp
+1,endp
,base
);
71 return simple_strtoul(cp
,endp
,base
);
74 EXPORT_SYMBOL(simple_strtol
);
77 * simple_strtoull - convert a string to an unsigned long long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
82 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
84 unsigned long long result
= 0,value
;
91 if ((*cp
== 'x') && isxdigit(cp
[1])) {
97 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
98 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
99 result
= result
*base
+ value
;
107 EXPORT_SYMBOL(simple_strtoull
);
110 * simple_strtoll - convert a string to a signed long long
111 * @cp: The start of the string
112 * @endp: A pointer to the end of the parsed string will be placed here
113 * @base: The number base to use
115 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
118 return -simple_strtoull(cp
+1,endp
,base
);
119 return simple_strtoull(cp
,endp
,base
);
122 static int skip_atoi(const char **s
)
127 i
= i
*10 + *((*s
)++) - '0';
131 #define ZEROPAD 1 /* pad with zero */
132 #define SIGN 2 /* unsigned/signed long */
133 #define PLUS 4 /* show plus */
134 #define SPACE 8 /* space if plus */
135 #define LEFT 16 /* left justified */
136 #define SPECIAL 32 /* 0x */
137 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
139 static char * number(char * buf
, char * end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
143 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
144 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
147 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
150 if (base
< 2 || base
> 36)
152 c
= (type
& ZEROPAD
) ? '0' : ' ';
155 if ((signed long long) num
< 0) {
157 num
= - (signed long long) num
;
159 } else if (type
& PLUS
) {
162 } else if (type
& SPACE
) {
167 if (type
& SPECIAL
) {
176 else while (num
!= 0)
177 tmp
[i
++] = digits
[do_div(num
,base
)];
181 if (!(type
&(ZEROPAD
+LEFT
))) {
193 if (type
& SPECIAL
) {
198 } else if (base
==16) {
207 if (!(type
& LEFT
)) {
214 while (i
< precision
--) {
233 * vsnprintf - Format a string and place it in a buffer
234 * @buf: The buffer to place the result into
235 * @size: The size of the buffer, including the trailing null space
236 * @fmt: The format string to use
237 * @args: Arguments for the format string
239 * The return value is the number of characters which would
240 * be generated for the given input, excluding the trailing
241 * '\0', as per ISO C99. If you want to have the exact
242 * number of characters written into @buf as return value
243 * (not including the trailing '\0'), use vscnprintf. If the
244 * return is greater than or equal to @size, the resulting
245 * string is truncated.
247 * Call this function if you are already dealing with a va_list.
248 * You probably want snprintf instead.
250 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
253 unsigned long long num
;
258 int flags
; /* flags to number() */
260 int field_width
; /* width of output field */
261 int precision
; /* min. # of digits for integers; max
262 number of chars for from string */
263 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
264 /* 'z' support added 23/7/1999 S.H. */
265 /* 'z' changed to 'Z' --davidm 1/25/99 */
267 /* Reject out-of-range values early */
268 if (unlikely((int) size
< 0)) {
269 /* There can be only one.. */
277 end
= buf
+ size
- 1;
281 size
= end
- buf
+ 1;
284 for (; *fmt
; ++fmt
) {
295 ++fmt
; /* this also skips first '%' */
297 case '-': flags
|= LEFT
; goto repeat
;
298 case '+': flags
|= PLUS
; goto repeat
;
299 case ' ': flags
|= SPACE
; goto repeat
;
300 case '#': flags
|= SPECIAL
; goto repeat
;
301 case '0': flags
|= ZEROPAD
; goto repeat
;
304 /* get field width */
307 field_width
= skip_atoi(&fmt
);
308 else if (*fmt
== '*') {
310 /* it's the next argument */
311 field_width
= va_arg(args
, int);
312 if (field_width
< 0) {
313 field_width
= -field_width
;
318 /* get the precision */
323 precision
= skip_atoi(&fmt
);
324 else if (*fmt
== '*') {
326 /* it's the next argument */
327 precision
= va_arg(args
, int);
333 /* get the conversion qualifier */
335 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
336 *fmt
=='Z' || *fmt
== 'z') {
339 if (qualifier
== 'l' && *fmt
== 'l') {
350 if (!(flags
& LEFT
)) {
351 while (--field_width
> 0) {
357 c
= (unsigned char) va_arg(args
, int);
361 while (--field_width
> 0) {
369 s
= va_arg(args
, char *);
370 if ((unsigned long)s
< PAGE_SIZE
)
373 len
= strnlen(s
, precision
);
375 if (!(flags
& LEFT
)) {
376 while (len
< field_width
--) {
382 for (i
= 0; i
< len
; ++i
) {
387 while (len
< field_width
--) {
395 if (field_width
== -1) {
396 field_width
= 2*sizeof(void *);
399 str
= number(str
, end
,
400 (unsigned long) va_arg(args
, void *),
401 16, field_width
, precision
, flags
);
407 * What does C99 say about the overflow case here? */
408 if (qualifier
== 'l') {
409 long * ip
= va_arg(args
, long *);
411 } else if (qualifier
== 'Z' || qualifier
== 'z') {
412 size_t * ip
= va_arg(args
, size_t *);
415 int * ip
= va_arg(args
, int *);
426 /* integer number formats - set up the flags and "break" */
456 if (qualifier
== 'L')
457 num
= va_arg(args
, long long);
458 else if (qualifier
== 'l') {
459 num
= va_arg(args
, unsigned long);
461 num
= (signed long) num
;
462 } else if (qualifier
== 'Z' || qualifier
== 'z') {
463 num
= va_arg(args
, size_t);
464 } else if (qualifier
== 'h') {
465 num
= (unsigned short) va_arg(args
, int);
467 num
= (signed short) num
;
469 num
= va_arg(args
, unsigned int);
471 num
= (signed int) num
;
473 str
= number(str
, end
, num
, base
,
474 field_width
, precision
, flags
);
479 /* don't write out a null byte if the buf size is zero */
481 /* the trailing null byte doesn't count towards the total
487 EXPORT_SYMBOL(vsnprintf
);
490 * vscnprintf - Format a string and place it in a buffer
491 * @buf: The buffer to place the result into
492 * @size: The size of the buffer, including the trailing null space
493 * @fmt: The format string to use
494 * @args: Arguments for the format string
496 * The return value is the number of characters which have been written into
497 * the @buf not including the trailing '\0'. If @size is <= 0 the function
500 * Call this function if you are already dealing with a va_list.
501 * You probably want scnprintf instead.
503 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
507 i
=vsnprintf(buf
,size
,fmt
,args
);
508 return (i
>= size
) ? (size
- 1) : i
;
511 EXPORT_SYMBOL(vscnprintf
);
514 * snprintf - Format a string and place it in a buffer
515 * @buf: The buffer to place the result into
516 * @size: The size of the buffer, including the trailing null space
517 * @fmt: The format string to use
518 * @...: Arguments for the format string
520 * The return value is the number of characters which would be
521 * generated for the given input, excluding the trailing null,
522 * as per ISO C99. If the return is greater than or equal to
523 * @size, the resulting string is truncated.
525 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
531 i
=vsnprintf(buf
,size
,fmt
,args
);
536 EXPORT_SYMBOL(snprintf
);
539 * scnprintf - Format a string and place it in a buffer
540 * @buf: The buffer to place the result into
541 * @size: The size of the buffer, including the trailing null space
542 * @fmt: The format string to use
543 * @...: Arguments for the format string
545 * The return value is the number of characters written into @buf not including
546 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
547 * greater than or equal to @size, the resulting string is truncated.
550 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
556 i
= vsnprintf(buf
, size
, fmt
, args
);
558 return (i
>= size
) ? (size
- 1) : i
;
560 EXPORT_SYMBOL(scnprintf
);
563 * vsprintf - Format a string and place it in a buffer
564 * @buf: The buffer to place the result into
565 * @fmt: The format string to use
566 * @args: Arguments for the format string
568 * The function returns the number of characters written
569 * into @buf. Use vsnprintf or vscnprintf in order to avoid
572 * Call this function if you are already dealing with a va_list.
573 * You probably want sprintf instead.
575 int vsprintf(char *buf
, const char *fmt
, va_list args
)
577 return vsnprintf(buf
, (~0U)>>1, fmt
, args
);
580 EXPORT_SYMBOL(vsprintf
);
583 * sprintf - Format a string and place it in a buffer
584 * @buf: The buffer to place the result into
585 * @fmt: The format string to use
586 * @...: Arguments for the format string
588 * The function returns the number of characters written
589 * into @buf. Use snprintf or scnprintf in order to avoid
592 int sprintf(char * buf
, const char *fmt
, ...)
598 i
=vsprintf(buf
,fmt
,args
);
603 EXPORT_SYMBOL(sprintf
);
606 * vsscanf - Unformat a buffer into a list of arguments
608 * @fmt: format of buffer
611 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
613 const char *str
= buf
;
622 while(*fmt
&& *str
) {
623 /* skip any white space in format */
624 /* white space in format matchs any amount of
625 * white space, including none, in the input.
628 while (isspace(*fmt
))
630 while (isspace(*str
))
634 /* anything that is not a conversion must match exactly */
635 if (*fmt
!= '%' && *fmt
) {
636 if (*fmt
++ != *str
++)
645 /* skip this conversion.
646 * advance both strings to next white space
649 while (!isspace(*fmt
) && *fmt
)
651 while (!isspace(*str
) && *str
)
656 /* get field width */
659 field_width
= skip_atoi(&fmt
);
661 /* get conversion qualifier */
663 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
664 *fmt
== 'Z' || *fmt
== 'z') {
677 char *s
= (char *) va_arg(args
,char*);
678 if (field_width
== -1)
682 } while (--field_width
> 0 && *str
);
688 char *s
= (char *) va_arg(args
, char *);
689 if(field_width
== -1)
690 field_width
= INT_MAX
;
691 /* first, skip leading white space in buffer */
692 while (isspace(*str
))
695 /* now copy until next white space */
696 while (*str
&& !isspace(*str
) && field_width
--) {
704 /* return number of characters read so far */
706 int *i
= (int *)va_arg(args
,int*);
724 /* looking for '%' in str */
729 /* invalid format; stop here */
733 /* have some sort of integer conversion.
734 * first, skip white space in buffer.
736 while (isspace(*str
))
740 if (is_sign
&& digit
== '-')
744 || (base
== 16 && !isxdigit(digit
))
745 || (base
== 10 && !isdigit(digit
))
746 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
747 || (base
== 0 && !isdigit(digit
)))
753 short *s
= (short *) va_arg(args
,short *);
754 *s
= (short) simple_strtol(str
,&next
,base
);
756 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
757 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
762 long *l
= (long *) va_arg(args
,long *);
763 *l
= simple_strtol(str
,&next
,base
);
765 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
766 *l
= simple_strtoul(str
,&next
,base
);
771 long long *l
= (long long*) va_arg(args
,long long *);
772 *l
= simple_strtoll(str
,&next
,base
);
774 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
775 *l
= simple_strtoull(str
,&next
,base
);
781 size_t *s
= (size_t*) va_arg(args
,size_t*);
782 *s
= (size_t) simple_strtoul(str
,&next
,base
);
787 int *i
= (int *) va_arg(args
, int*);
788 *i
= (int) simple_strtol(str
,&next
,base
);
790 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
791 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
804 EXPORT_SYMBOL(vsscanf
);
807 * sscanf - Unformat a buffer into a list of arguments
809 * @fmt: formatting of buffer
810 * @...: resulting arguments
812 int sscanf(const char * buf
, const char * fmt
, ...)
818 i
= vsscanf(buf
,fmt
,args
);
823 EXPORT_SYMBOL(sscanf
);