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/module.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/ctype.h>
22 #include <linux/kernel.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 ((*cp
== 'x') && isxdigit(cp
[1])) {
47 while (isxdigit(*cp
) &&
48 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
49 result
= result
*base
+ value
;
57 EXPORT_SYMBOL(simple_strtoul
);
60 * simple_strtol - convert a string to a signed long
61 * @cp: The start of the string
62 * @endp: A pointer to the end of the parsed string will be placed here
63 * @base: The number base to use
65 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
68 return -simple_strtoul(cp
+1,endp
,base
);
69 return simple_strtoul(cp
,endp
,base
);
72 EXPORT_SYMBOL(simple_strtol
);
75 * simple_strtoull - convert a string to an unsigned long long
76 * @cp: The start of the string
77 * @endp: A pointer to the end of the parsed string will be placed here
78 * @base: The number base to use
80 unsigned long long simple_strtoull(const char *cp
,char **endp
,unsigned int base
)
82 unsigned long long result
= 0,value
;
89 if ((*cp
== 'x') && isxdigit(cp
[1])) {
95 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
96 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
97 result
= result
*base
+ value
;
105 EXPORT_SYMBOL(simple_strtoull
);
108 * simple_strtoll - convert a string to a signed long long
109 * @cp: The start of the string
110 * @endp: A pointer to the end of the parsed string will be placed here
111 * @base: The number base to use
113 long long simple_strtoll(const char *cp
,char **endp
,unsigned int base
)
116 return -simple_strtoull(cp
+1,endp
,base
);
117 return simple_strtoull(cp
,endp
,base
);
120 static int skip_atoi(const char **s
)
125 i
= i
*10 + *((*s
)++) - '0';
129 #define ZEROPAD 1 /* pad with zero */
130 #define SIGN 2 /* unsigned/signed long */
131 #define PLUS 4 /* show plus */
132 #define SPACE 8 /* space if plus */
133 #define LEFT 16 /* left justified */
134 #define SPECIAL 32 /* 0x */
135 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
137 static char * number(char * buf
, char * end
, unsigned long long num
, int base
, int size
, int precision
, int type
)
141 static const char small_digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
142 static const char large_digits
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
145 digits
= (type
& LARGE
) ? large_digits
: small_digits
;
148 if (base
< 2 || base
> 36)
150 c
= (type
& ZEROPAD
) ? '0' : ' ';
153 if ((signed long long) num
< 0) {
155 num
= - (signed long long) num
;
157 } else if (type
& PLUS
) {
160 } else if (type
& SPACE
) {
165 if (type
& SPECIAL
) {
174 else while (num
!= 0)
175 tmp
[i
++] = digits
[do_div(num
,base
)];
179 if (!(type
&(ZEROPAD
+LEFT
))) {
191 if (type
& SPECIAL
) {
196 } else if (base
==16) {
205 if (!(type
& LEFT
)) {
212 while (i
< precision
--) {
231 * vsnprintf - Format a string and place it in a buffer
232 * @buf: The buffer to place the result into
233 * @size: The size of the buffer, including the trailing null space
234 * @fmt: The format string to use
235 * @args: Arguments for the format string
237 * Call this function if you are already dealing with a va_list.
238 * You probably want snprintf instead.
240 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
243 unsigned long long num
;
248 int flags
; /* flags to number() */
250 int field_width
; /* width of output field */
251 int precision
; /* min. # of digits for integers; max
252 number of chars for from string */
253 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
254 /* 'z' support added 23/7/1999 S.H. */
255 /* 'z' changed to 'Z' --davidm 1/25/99 */
258 end
= buf
+ size
- 1;
262 size
= end
- buf
+ 1;
265 for (; *fmt
; ++fmt
) {
276 ++fmt
; /* this also skips first '%' */
278 case '-': flags
|= LEFT
; goto repeat
;
279 case '+': flags
|= PLUS
; goto repeat
;
280 case ' ': flags
|= SPACE
; goto repeat
;
281 case '#': flags
|= SPECIAL
; goto repeat
;
282 case '0': flags
|= ZEROPAD
; goto repeat
;
285 /* get field width */
288 field_width
= skip_atoi(&fmt
);
289 else if (*fmt
== '*') {
291 /* it's the next argument */
292 field_width
= va_arg(args
, int);
293 if (field_width
< 0) {
294 field_width
= -field_width
;
299 /* get the precision */
304 precision
= skip_atoi(&fmt
);
305 else if (*fmt
== '*') {
307 /* it's the next argument */
308 precision
= va_arg(args
, int);
314 /* get the conversion qualifier */
316 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
317 *fmt
=='Z' || *fmt
== 'z') {
320 if (qualifier
== 'l' && *fmt
== 'l') {
331 if (!(flags
& LEFT
)) {
332 while (--field_width
> 0) {
338 c
= (unsigned char) va_arg(args
, int);
342 while (--field_width
> 0) {
350 s
= va_arg(args
, char *);
351 if ((unsigned long)s
< PAGE_SIZE
)
354 len
= strnlen(s
, precision
);
356 if (!(flags
& LEFT
)) {
357 while (len
< field_width
--) {
363 for (i
= 0; i
< len
; ++i
) {
368 while (len
< field_width
--) {
376 if (field_width
== -1) {
377 field_width
= 2*sizeof(void *);
380 str
= number(str
, end
,
381 (unsigned long) va_arg(args
, void *),
382 16, field_width
, precision
, flags
);
388 * What does C99 say about the overflow case here? */
389 if (qualifier
== 'l') {
390 long * ip
= va_arg(args
, long *);
392 } else if (qualifier
== 'Z' || qualifier
== 'z') {
393 size_t * ip
= va_arg(args
, size_t *);
396 int * ip
= va_arg(args
, int *);
407 /* integer number formats - set up the flags and "break" */
437 if (qualifier
== 'L')
438 num
= va_arg(args
, long long);
439 else if (qualifier
== 'l') {
440 num
= va_arg(args
, unsigned long);
442 num
= (signed long) num
;
443 } else if (qualifier
== 'Z' || qualifier
== 'z') {
444 num
= va_arg(args
, size_t);
445 } else if (qualifier
== 'h') {
446 num
= (unsigned short) va_arg(args
, int);
448 num
= (signed short) num
;
450 num
= va_arg(args
, unsigned int);
452 num
= (signed int) num
;
454 str
= number(str
, end
, num
, base
,
455 field_width
, precision
, flags
);
460 /* don't write out a null byte if the buf size is zero */
462 /* the trailing null byte doesn't count towards the total
468 EXPORT_SYMBOL(vsnprintf
);
471 * snprintf - Format a string and place it in a buffer
472 * @buf: The buffer to place the result into
473 * @size: The size of the buffer, including the trailing null space
474 * @fmt: The format string to use
475 * @...: Arguments for the format string
477 int snprintf(char * buf
, size_t size
, const char *fmt
, ...)
483 i
=vsnprintf(buf
,size
,fmt
,args
);
488 EXPORT_SYMBOL(snprintf
);
491 * vsprintf - Format a string and place it in a buffer
492 * @buf: The buffer to place the result into
493 * @fmt: The format string to use
494 * @args: Arguments for the format string
496 * Call this function if you are already dealing with a va_list.
497 * You probably want sprintf instead.
499 int vsprintf(char *buf
, const char *fmt
, va_list args
)
501 return vsnprintf(buf
, 0xFFFFFFFFUL
, fmt
, args
);
504 EXPORT_SYMBOL(vsprintf
);
507 * sprintf - Format a string and place it in a buffer
508 * @buf: The buffer to place the result into
509 * @fmt: The format string to use
510 * @...: Arguments for the format string
512 int sprintf(char * buf
, const char *fmt
, ...)
518 i
=vsprintf(buf
,fmt
,args
);
523 EXPORT_SYMBOL(sprintf
);
526 * vsscanf - Unformat a buffer into a list of arguments
528 * @fmt: format of buffer
531 int vsscanf(const char * buf
, const char * fmt
, va_list args
)
533 const char *str
= buf
;
542 while(*fmt
&& *str
) {
543 /* skip any white space in format */
544 /* white space in format matchs any amount of
545 * white space, including none, in the input.
548 while (isspace(*fmt
))
550 while (isspace(*str
))
554 /* anything that is not a conversion must match exactly */
555 if (*fmt
!= '%' && *fmt
) {
556 if (*fmt
++ != *str
++)
565 /* skip this conversion.
566 * advance both strings to next white space
569 while (!isspace(*fmt
) && *fmt
)
571 while (!isspace(*str
) && *str
)
576 /* get field width */
579 field_width
= skip_atoi(&fmt
);
581 /* get conversion qualifier */
583 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
584 *fmt
== 'Z' || *fmt
== 'z') {
597 char *s
= (char *) va_arg(args
,char*);
598 if (field_width
== -1)
602 } while(field_width
-- > 0 && *str
);
608 char *s
= (char *) va_arg(args
, char *);
609 if(field_width
== -1)
610 field_width
= INT_MAX
;
611 /* first, skip leading white space in buffer */
612 while (isspace(*str
))
615 /* now copy until next white space */
616 while (*str
&& !isspace(*str
) && field_width
--) {
624 /* return number of characters read so far */
626 int *i
= (int *)va_arg(args
,int*);
644 /* looking for '%' in str */
649 /* invalid format; stop here */
653 /* have some sort of integer conversion.
654 * first, skip white space in buffer.
656 while (isspace(*str
))
660 if (is_sign
&& digit
== '-')
664 || (base
== 16 && !isxdigit(digit
))
665 || (base
== 10 && !isdigit(digit
))
666 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
667 || (base
== 0 && !isdigit(digit
)))
673 short *s
= (short *) va_arg(args
,short *);
674 *s
= (short) simple_strtol(str
,&next
,base
);
676 unsigned short *s
= (unsigned short *) va_arg(args
, unsigned short *);
677 *s
= (unsigned short) simple_strtoul(str
, &next
, base
);
682 long *l
= (long *) va_arg(args
,long *);
683 *l
= simple_strtol(str
,&next
,base
);
685 unsigned long *l
= (unsigned long*) va_arg(args
,unsigned long*);
686 *l
= simple_strtoul(str
,&next
,base
);
691 long long *l
= (long long*) va_arg(args
,long long *);
692 *l
= simple_strtoll(str
,&next
,base
);
694 unsigned long long *l
= (unsigned long long*) va_arg(args
,unsigned long long*);
695 *l
= simple_strtoull(str
,&next
,base
);
701 size_t *s
= (size_t*) va_arg(args
,size_t*);
702 *s
= (size_t) simple_strtoul(str
,&next
,base
);
707 int *i
= (int *) va_arg(args
, int*);
708 *i
= (int) simple_strtol(str
,&next
,base
);
710 unsigned int *i
= (unsigned int*) va_arg(args
, unsigned int*);
711 *i
= (unsigned int) simple_strtoul(str
,&next
,base
);
724 EXPORT_SYMBOL(vsscanf
);
727 * sscanf - Unformat a buffer into a list of arguments
729 * @fmt: formatting of buffer
730 * @...: resulting arguments
732 int sscanf(const char * buf
, const char * fmt
, ...)
738 i
= vsscanf(buf
,fmt
,args
);
743 EXPORT_SYMBOL(sscanf
);