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
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/kernel.h>
23 #include <asm/div64.h>
26 * simple_strtoul - convert a string to an unsigned long
27 * @cp: The start of the string
28 * @endp: A pointer to the end of the parsed string will be placed here
29 * @base: The number base to use
31 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
33 unsigned long result
= 0,value
;
40 if ((*cp
== 'x') && isxdigit(cp
[1])) {
46 while (isxdigit(*cp
) &&
47 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
48 result
= result
*base
+ value
;
57 * simple_strtol - convert a string to a signed long
58 * @cp: The start of the string
59 * @endp: A pointer to the end of the parsed string will be placed here
60 * @base: The number base to use
62 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
65 return -simple_strtoul(cp
+1,endp
,base
);
66 return simple_strtoul(cp
,endp
,base
);
70 * simple_strtoull - convert a string to an unsigned long long
71 * @cp: The start of the string
72 * @endp: A pointer to the end of the parsed string will be placed here
73 * @base: The number base to use
75 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
77 unsigned long long result
= 0,value
;
84 if ((*cp
== 'x') && isxdigit(cp
[1])) {
90 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
91 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
92 result
= result
*base
+ value
;
101 * simple_strtoll - convert a string to a signed long long
102 * @cp: The start of the string
103 * @endp: A pointer to the end of the parsed string will be placed here
104 * @base: The number base to use
106 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
109 return -simple_strtoull(cp
+1,endp
,base
);
110 return simple_strtoull(cp
,endp
,base
);
113 static int skip_atoi(const char **s
)
118 i
= i
*10 + *((*s
)++) - '0';
122 #define ZEROPAD 1 /* pad with zero */
123 #define SIGN 2 /* unsigned/signed long */
124 #define PLUS 4 /* show plus */
125 #define SPACE 8 /* space if plus */
126 #define LEFT 16 /* left justified */
127 #define SPECIAL 32 /* 0x */
128 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
130 static char * number(char * buf
, char * end
, long long num
, int base
, int size
, int precision
, int type
)
134 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
135 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
138 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
141 if (base
< 2 || base
> 36)
143 c
= (type
& ZEROPAD
) ? '0' : ' ';
150 } else if (type
& PLUS
) {
153 } else if (type
& SPACE
) {
158 if (type
& SPECIAL
) {
167 else while (num
!= 0)
168 tmp
[i
++] = digits
[do_div(num
,base
)];
172 if (!(type
&(ZEROPAD
+LEFT
))) {
184 if (type
& SPECIAL
) {
189 } else if (base
==16) {
198 if (!(type
& LEFT
)) {
205 while (i
< precision
--) {
224 * vsnprintf - Format a string and place it in a buffer
225 * @buf: The buffer to place the result into
226 * @size: The size of the buffer, including the trailing null space
227 * @fmt: The format string to use
228 * @args: Arguments for the format string
230 * Call this function if you are already dealing with a va_list.
231 * You probably want snprintf instead.
233 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
236 unsigned long long num
;
241 int flags
; /* flags to number() */
243 int field_width
; /* width of output field */
244 int precision
; /* min. # of digits for integers; max
245 number of chars for from string */
246 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
247 /* 'z' support added 23/7/1999 S.H. */
248 /* 'z' changed to 'Z' --davidm 1/25/99 */
251 end
= buf
+ size
- 1;
255 size
= end
- buf
+ 1;
258 for (; *fmt
; ++fmt
) {
269 ++fmt
; /* this also skips first '%' */
271 case '-': flags
|= LEFT
; goto repeat
;
272 case '+': flags
|= PLUS
; goto repeat
;
273 case ' ': flags
|= SPACE
; goto repeat
;
274 case '#': flags
|= SPECIAL
; goto repeat
;
275 case '0': flags
|= ZEROPAD
; goto repeat
;
278 /* get field width */
281 field_width
= skip_atoi(&fmt
);
282 else if (*fmt
== '*') {
284 /* it's the next argument */
285 field_width
= va_arg(args
, int);
286 if (field_width
< 0) {
287 field_width
= -field_width
;
292 /* get the precision */
297 precision
= skip_atoi(&fmt
);
298 else if (*fmt
== '*') {
300 /* it's the next argument */
301 precision
= va_arg(args
, int);
307 /* get the conversion qualifier */
309 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
310 *fmt
=='Z' || *fmt
== 'z') {
313 if (qualifier
== 'l' && *fmt
== 'l') {
324 if (!(flags
& LEFT
)) {
325 while (--field_width
> 0) {
331 c
= (unsigned char) va_arg(args
, int);
335 while (--field_width
> 0) {
343 s
= va_arg(args
, char *);
347 len
= strnlen(s
, precision
);
349 if (!(flags
& LEFT
)) {
350 while (len
< field_width
--) {
356 for (i
= 0; i
< len
; ++i
) {
361 while (len
< field_width
--) {
369 if (field_width
== -1) {
370 field_width
= 2*sizeof(void *);
373 str
= number(str
, end
,
374 (unsigned long) va_arg(args
, void *),
375 16, field_width
, precision
, flags
);
381 * What does C99 say about the overflow case here? */
382 if (qualifier
== 'l') {
383 long * ip
= va_arg(args
, long *);
385 } else if (qualifier
== 'Z' || qualifier
== 'z') {
386 size_t * ip
= va_arg(args
, size_t *);
389 int * ip
= va_arg(args
, int *);
400 /* integer number formats - set up the flags and "break" */
430 if (qualifier
== 'L')
431 num
= va_arg(args
, long long);
432 else if (qualifier
== 'l') {
433 num
= va_arg(args
, unsigned long);
435 num
= (signed long) num
;
436 } else if (qualifier
== 'Z' || qualifier
== 'z') {
437 num
= va_arg(args
, size_t);
438 } else if (qualifier
== 'h') {
439 num
= (unsigned short) va_arg(args
, int);
441 num
= (signed short) num
;
443 num
= va_arg(args
, unsigned int);
445 num
= (signed int) num
;
447 str
= number(str
, end
, num
, base
,
448 field_width
, precision
, flags
);
453 /* don't write out a null byte if the buf size is zero */
455 /* the trailing null byte doesn't count towards the total
462 * snprintf - Format a string and place it in a buffer
463 * @buf: The buffer to place the result into
464 * @size: The size of the buffer, including the trailing null space
465 * @fmt: The format string to use
466 * @...: Arguments for the format string
468 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
474 i
=vsnprintf(buf
,size
,fmt
,args
);
480 * vsprintf - Format a string and place it in a buffer
481 * @buf: The buffer to place the result into
482 * @fmt: The format string to use
483 * @args: Arguments for the format string
485 * Call this function if you are already dealing with a va_list.
486 * You probably want sprintf instead.
488 int vsprintf(char *buf
, const char *fmt
, va_list args
)
490 return vsnprintf(buf
, 0xFFFFFFFFUL
, fmt
, args
);
495 * sprintf - Format a string and place it in a buffer
496 * @buf: The buffer to place the result into
497 * @fmt: The format string to use
498 * @...: Arguments for the format string
500 int sprintf(char * buf
, const char *fmt
, ...)
506 i
=vsprintf(buf
,fmt
,args
);
512 * vsscanf - Unformat a buffer into a list of arguments
514 * @fmt: format of buffer
517 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
519 const char *str
= buf
;
528 while(*fmt
&& *str
) {
529 /* skip any white space in format */
530 /* white space in format matchs any amount of
531 * white space, including none, in the input.
534 while (isspace(*fmt
))
536 while (isspace(*str
))
540 /* anything that is not a conversion must match exactly */
541 if (*fmt
!= '%' && *fmt
) {
542 if (*fmt
++ != *str
++)
551 /* skip this conversion.
552 * advance both strings to next white space
555 while (!isspace(*fmt
) && *fmt
)
557 while (!isspace(*str
) && *str
)
562 /* get field width */
565 field_width
= skip_atoi(&fmt
);
567 /* get conversion qualifier */
569 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
570 *fmt
== 'Z' || *fmt
== 'z') {
583 char *s
= (char *) va_arg(args
,char*);
584 if (field_width
== -1)
588 } while(field_width
-- > 0 && *str
);
594 char *s
= (char *) va_arg(args
, char *);
595 if(field_width
== -1)
596 field_width
= INT_MAX
;
597 /* first, skip leading white space in buffer */
598 while (isspace(*str
))
601 /* now copy until next white space */
602 while (*str
&& !isspace(*str
) && field_width
--) {
610 /* return number of characters read so far */
612 int *i
= (int *)va_arg(args
,int*);
630 /* looking for '%' in str */
635 /* invalid format; stop here */
639 /* have some sort of integer conversion.
640 * first, skip white space in buffer.
642 while (isspace(*str
))
646 if (is_sign
&& digit
== '-')
650 || (base
== 16 && !isxdigit(digit
))
651 || (base
== 10 && !isdigit(digit
))
652 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
653 || (base
== 0 && !isdigit(digit
)))
659 short *s
= (short *) va_arg(args
,short *);
660 *s
= (short) simple_strtol(str
,&next
,base
);
662 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
663 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
668 long *l
= (long *) va_arg(args
,long *);
669 *l
= simple_strtol(str
,&next
,base
);
671 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
672 *l
= simple_strtoul(str
,&next
,base
);
677 long long *l
= (long long*) va_arg(args
,long long *);
678 *l
= simple_strtoll(str
,&next
,base
);
680 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
681 *l
= simple_strtoull(str
,&next
,base
);
687 size_t *s
= (size_t*) va_arg(args
,size_t*);
688 *s
= (size_t) simple_strtoul(str
,&next
,base
);
693 int *i
= (int *) va_arg(args
, int*);
694 *i
= (int) simple_strtol(str
,&next
,base
);
696 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
697 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
711 * sscanf - Unformat a buffer into a list of arguments
713 * @fmt: formatting of buffer
714 * @...: resulting arguments
716 int sscanf(const char * buf
, const char * fmt
, ...)
722 i
= vsscanf(buf
,fmt
,args
);