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 <sys/types.h>
22 #include <asm/ctype.h>
24 #include <asm/div64.h>
27 * simple_strtoul - convert a string to an unsigned long
28 * @cp: The start of the string
29 * @endp: A pointer to the end of the parsed string will be placed here
30 * @base: The number base to use
32 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
34 unsigned long result
= 0,value
;
41 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
46 } else if (base
== 16) {
47 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
50 while (isxdigit(*cp
) &&
51 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
52 result
= result
*base
+ value
;
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
);
76 * simple_strtoull - convert a string to an unsigned long long
77 * @cp: The start of the string
78 * @endp: A pointer to the end of the parsed string will be placed here
79 * @base: The number base to use
81 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
83 unsigned long long result
= 0,value
;
90 if ((toupper(*cp
) == 'X') && isxdigit(cp
[1])) {
95 } else if (base
== 16) {
96 if (cp
[0] == '0' && toupper(cp
[1]) == 'X')
99 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
100 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
101 result
= result
*base
+ value
;
111 * simple_strtoll - convert a string to a signed long long
112 * @cp: The start of the string
113 * @endp: A pointer to the end of the parsed string will be placed here
114 * @base: The number base to use
116 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
119 return -simple_strtoull(cp
+1,endp
,base
);
120 return simple_strtoull(cp
,endp
,base
);
123 static int skip_atoi(const char **s
)
128 i
= i
*10 + *((*s
)++) - '0';
132 #define ZEROPAD 1 /* pad with zero */
133 #define SIGN 2 /* unsigned/signed long */
134 #define PLUS 4 /* show plus */
135 #define SPACE 8 /* space if plus */
136 #define LEFT 16 /* left justified */
137 #define SPECIAL 32 /* 0x */
138 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
140 static char * number(char * buf
, char * end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
144 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
145 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
148 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
151 if (base
< 2 || base
> 36)
153 c
= (type
& ZEROPAD
) ? '0' : ' ';
156 if ((signed long long) num
< 0) {
158 num
= - (signed long long) num
;
160 } else if (type
& PLUS
) {
163 } else if (type
& SPACE
) {
168 if (type
& SPECIAL
) {
177 else while (num
!= 0)
178 tmp
[i
++] = digits
[do_div(num
,base
)];
182 if (!(type
&(ZEROPAD
+LEFT
))) {
194 if (type
& SPECIAL
) {
199 } else if (base
==16) {
208 if (!(type
& LEFT
)) {
215 while (i
< precision
--) {
234 * vsnprintf - Format a string and place it in a buffer
235 * @buf: The buffer to place the result into
236 * @size: The size of the buffer, including the trailing null space
237 * @fmt: The format string to use
238 * @args: Arguments for the format string
240 * The return value is the number of characters which would
241 * be generated for the given input, excluding the trailing
242 * '\0', as per ISO C99. If you want to have the exact
243 * number of characters written into @buf as return value
244 * (not including the trailing '\0'), use vscnprintf. If the
245 * return is greater than or equal to @size, the resulting
246 * string is truncated.
248 * Call this function if you are already dealing with a va_list.
249 * You probably want snprintf instead.
251 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
254 unsigned long long num
;
259 int flags
; /* flags to number() */
261 int field_width
; /* width of output field */
262 int precision
; /* min. # of digits for integers; max
263 number of chars for from string */
264 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
265 /* 'z' support added 23/7/1999 S.H. */
266 /* 'z' changed to 'Z' --davidm 1/25/99 */
267 /* 't' added for ptrdiff_t */
269 /* Reject out-of-range values early */
270 if ((int) size
< 0) {
275 end
= buf
+ size
- 1;
279 size
= end
- buf
+ 1;
282 for (; *fmt
; ++fmt
) {
293 ++fmt
; /* this also skips first '%' */
295 case '-': flags
|= LEFT
; goto repeat
;
296 case '+': flags
|= PLUS
; goto repeat
;
297 case ' ': flags
|= SPACE
; goto repeat
;
298 case '#': flags
|= SPECIAL
; goto repeat
;
299 case '0': flags
|= ZEROPAD
; goto repeat
;
302 /* get field width */
305 field_width
= skip_atoi(&fmt
);
306 else if (*fmt
== '*') {
308 /* it's the next argument */
309 field_width
= va_arg(args
, int);
310 if (field_width
< 0) {
311 field_width
= -field_width
;
316 /* get the precision */
321 precision
= skip_atoi(&fmt
);
322 else if (*fmt
== '*') {
324 /* it's the next argument */
325 precision
= va_arg(args
, int);
331 /* get the conversion qualifier */
333 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
334 *fmt
=='Z' || *fmt
== 'z' || *fmt
== 't') {
337 if (qualifier
== 'l' && *fmt
== 'l') {
348 if (!(flags
& LEFT
)) {
349 while (--field_width
> 0) {
355 c
= (unsigned char) va_arg(args
, int);
359 while (--field_width
> 0) {
367 s
= va_arg(args
, char *);
369 len
= strnlen(s
, precision
);
371 if (!(flags
& LEFT
)) {
372 while (len
< field_width
--) {
378 for (i
= 0; i
< len
; ++i
) {
383 while (len
< field_width
--) {
391 if (field_width
== -1) {
392 field_width
= 2*sizeof(void *);
395 str
= number(str
, end
,
396 (unsigned long) va_arg(args
, void *),
397 16, field_width
, precision
, flags
);
403 * What does C99 say about the overflow case here? */
404 if (qualifier
== 'l') {
405 long * ip
= va_arg(args
, long *);
407 } else if (qualifier
== 'Z' || qualifier
== 'z') {
408 size_t * ip
= va_arg(args
, size_t *);
411 int * ip
= va_arg(args
, int *);
422 /* integer number formats - set up the flags and "break" */
452 if (qualifier
== 'L')
453 num
= va_arg(args
, long long);
454 else if (qualifier
== 'l') {
455 num
= va_arg(args
, unsigned long);
457 num
= (signed long) num
;
458 } else if (qualifier
== 'Z' || qualifier
== 'z') {
459 num
= va_arg(args
, size_t);
460 } else if (qualifier
== 't') {
461 num
= va_arg(args
, ptrdiff_t);
462 } else if (qualifier
== 'h') {
463 num
= (unsigned short) va_arg(args
, int);
465 num
= (signed short) num
;
467 num
= va_arg(args
, unsigned int);
469 num
= (signed int) num
;
471 str
= number(str
, end
, num
, base
,
472 field_width
, precision
, flags
);
477 /* don't write out a null byte if the buf size is zero */
479 /* the trailing null byte doesn't count towards the total
487 * vscnprintf - Format a string and place it in a buffer
488 * @buf: The buffer to place the result into
489 * @size: The size of the buffer, including the trailing null space
490 * @fmt: The format string to use
491 * @args: Arguments for the format string
493 * The return value is the number of characters which have been written into
494 * the @buf not including the trailing '\0'. If @size is <= 0 the function
497 * Call this function if you are already dealing with a va_list.
498 * You probably want scnprintf instead.
500 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
504 i
=vsnprintf(buf
,size
,fmt
,args
);
505 return (i
>= size
) ? (size
- 1) : i
;
510 * snprintf - Format a string and place it in a buffer
511 * @buf: The buffer to place the result into
512 * @size: The size of the buffer, including the trailing null space
513 * @fmt: The format string to use
514 * @...: Arguments for the format string
516 * The return value is the number of characters which would be
517 * generated for the given input, excluding the trailing null,
518 * as per ISO C99. If the return is greater than or equal to
519 * @size, the resulting string is truncated.
521 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
527 i
=vsnprintf(buf
,size
,fmt
,args
);
534 * scnprintf - Format a string and place it in a buffer
535 * @buf: The buffer to place the result into
536 * @size: The size of the buffer, including the trailing null space
537 * @fmt: The format string to use
538 * @...: Arguments for the format string
540 * The return value is the number of characters written into @buf not including
541 * the trailing '\0'. If @size is <= 0 the function returns 0. If the return is
542 * greater than or equal to @size, the resulting string is truncated.
545 int scnprintf(char * buf
, size_t size
, const char *fmt
, ...)
551 i
= vsnprintf(buf
, size
, fmt
, args
);
553 return (i
>= size
) ? (size
- 1) : i
;
557 * vsprintf - Format a string and place it in a buffer
558 * @buf: The buffer to place the result into
559 * @fmt: The format string to use
560 * @args: Arguments for the format string
562 * The function returns the number of characters written
563 * into @buf. Use vsnprintf or vscnprintf in order to avoid
566 * Call this function if you are already dealing with a va_list.
567 * You probably want sprintf instead.
569 int vsprintf(char *buf
, const char *fmt
, va_list args
)
571 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
576 * sprintf - Format a string and place it in a buffer
577 * @buf: The buffer to place the result into
578 * @fmt: The format string to use
579 * @...: Arguments for the format string
581 * The function returns the number of characters written
582 * into @buf. Use snprintf or scnprintf in order to avoid
585 int sprintf(char * buf
, const char *fmt
, ...)
591 i
=vsnprintf(buf
, INT_MAX
, fmt
, args
);
598 * vsscanf - Unformat a buffer into a list of arguments
600 * @fmt: format of buffer
603 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
605 const char *str
= buf
;
614 while(*fmt
&& *str
) {
615 /* skip any white space in format */
616 /* white space in format matchs any amount of
617 * white space, including none, in the input.
620 while (isspace(*fmt
))
622 while (isspace(*str
))
626 /* anything that is not a conversion must match exactly */
627 if (*fmt
!= '%' && *fmt
) {
628 if (*fmt
++ != *str
++)
637 /* skip this conversion.
638 * advance both strings to next white space
641 while (!isspace(*fmt
) && *fmt
)
643 while (!isspace(*str
) && *str
)
648 /* get field width */
651 field_width
= skip_atoi(&fmt
);
653 /* get conversion qualifier */
655 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
656 *fmt
== 'Z' || *fmt
== 'z') {
658 if (qualifier
== *fmt
) {
659 if (qualifier
== 'h') {
662 } else if (qualifier
== 'l') {
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
)))
751 case 'H': /* that's 'hh' in format */
753 signed char *s
= (signed char *) va_arg(args
,signed char *);
754 *s
= (signed char) simple_strtol(str
,&next
,base
);
756 unsigned char *s
= (unsigned char *) va_arg(args
, unsigned char *);
757 *s
= (unsigned char) simple_strtoul(str
, &next
, base
);
762 short *s
= (short *) va_arg(args
,short *);
763 *s
= (short) simple_strtol(str
,&next
,base
);
765 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
766 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
771 long *l
= (long *) va_arg(args
,long *);
772 *l
= simple_strtol(str
,&next
,base
);
774 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
775 *l
= simple_strtoul(str
,&next
,base
);
780 long long *l
= (long long*) va_arg(args
,long long *);
781 *l
= simple_strtoll(str
,&next
,base
);
783 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
784 *l
= simple_strtoull(str
,&next
,base
);
790 size_t *s
= (size_t*) va_arg(args
,size_t*);
791 *s
= (size_t) simple_strtoul(str
,&next
,base
);
796 int *i
= (int *) va_arg(args
, int*);
797 *i
= (int) simple_strtol(str
,&next
,base
);
799 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
800 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
815 * sscanf - Unformat a buffer into a list of arguments
817 * @fmt: formatting of buffer
818 * @...: resulting arguments
820 int sscanf(const char * buf
, const char * fmt
, ...)
826 i
= vsscanf(buf
,fmt
,args
);