4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
17 #include <asm/div64.h>
19 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
21 unsigned long result
= 0,value
;
28 if ((*cp
== 'x') && isxdigit(cp
[1])) {
34 while (isxdigit(*cp
) &&
35 (value
= isdigit(*cp
) ? *cp
-'0' : toupper(*cp
)-'A'+10) < base
) {
36 result
= result
*base
+ value
;
44 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
47 return -simple_strtoul(cp
+1,endp
,base
);
48 return simple_strtoul(cp
,endp
,base
);
51 static int skip_atoi(const char **s
)
56 i
= i
*10 + *((*s
)++) - '0';
60 #define ZEROPAD 1 /* pad with zero */
61 #define SIGN 2 /* unsigned/signed long */
62 #define PLUS 4 /* show plus */
63 #define SPACE 8 /* space if plus */
64 #define LEFT 16 /* left justified */
65 #define SPECIAL 32 /* 0x */
66 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
68 static char * number(char * str
, long long num
, int base
, int size
, int precision
, int type
)
71 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
75 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
78 if (base
< 2 || base
> 36)
80 c
= (type
& ZEROPAD
) ? '0' : ' ';
87 } else if (type
& PLUS
) {
90 } else if (type
& SPACE
) {
104 else while (num
!= 0)
105 tmp
[i
++] = digits
[do_div(num
,base
)];
109 if (!(type
&(ZEROPAD
+LEFT
)))
114 if (type
& SPECIAL
) {
125 while (i
< precision
--)
134 /* Forward decl. needed for IP address printing stuff... */
135 int sprintf(char * buf
, const char *fmt
, ...);
137 int vsprintf(char *buf
, const char *fmt
, va_list args
)
140 unsigned long long num
;
145 int flags
; /* flags to number() */
147 int field_width
; /* width of output field */
148 int precision
; /* min. # of digits for integers; max
149 number of chars for from string */
150 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
151 /* 'z' support added 23/7/1999 S.H. */
152 /* 'z' changed to 'Z' --davidm 1/25/99 */
155 for (str
=buf
; *fmt
; ++fmt
) {
164 ++fmt
; /* this also skips first '%' */
166 case '-': flags
|= LEFT
; goto repeat
;
167 case '+': flags
|= PLUS
; goto repeat
;
168 case ' ': flags
|= SPACE
; goto repeat
;
169 case '#': flags
|= SPECIAL
; goto repeat
;
170 case '0': flags
|= ZEROPAD
; goto repeat
;
173 /* get field width */
176 field_width
= skip_atoi(&fmt
);
177 else if (*fmt
== '*') {
179 /* it's the next argument */
180 field_width
= va_arg(args
, int);
181 if (field_width
< 0) {
182 field_width
= -field_width
;
187 /* get the precision */
192 precision
= skip_atoi(&fmt
);
193 else if (*fmt
== '*') {
195 /* it's the next argument */
196 precision
= va_arg(args
, int);
202 /* get the conversion qualifier */
204 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
=='Z') {
215 while (--field_width
> 0)
217 *str
++ = (unsigned char) va_arg(args
, int);
218 while (--field_width
> 0)
223 s
= va_arg(args
, char *);
227 len
= strnlen(s
, precision
);
230 while (len
< field_width
--)
232 for (i
= 0; i
< len
; ++i
)
234 while (len
< field_width
--)
239 if (field_width
== -1) {
240 field_width
= 2*sizeof(void *);
244 (unsigned long) va_arg(args
, void *), 16,
245 field_width
, precision
, flags
);
250 if (qualifier
== 'l') {
251 long * ip
= va_arg(args
, long *);
253 } else if (qualifier
== 'Z') {
254 size_t * ip
= va_arg(args
, size_t *);
257 int * ip
= va_arg(args
, int *);
266 /* integer number formats - set up the flags and "break" */
291 if (qualifier
== 'L')
292 num
= va_arg(args
, long long);
293 else if (qualifier
== 'l') {
294 num
= va_arg(args
, unsigned long);
296 num
= (signed long) num
;
297 } else if (qualifier
== 'Z') {
298 num
= va_arg(args
, size_t);
299 } else if (qualifier
== 'h') {
300 num
= (unsigned short) va_arg(args
, int);
302 num
= (signed short) num
;
304 num
= va_arg(args
, unsigned int);
306 num
= (signed int) num
;
308 str
= number(str
, num
, base
, field_width
, precision
, flags
);
314 int sprintf(char * buf
, const char *fmt
, ...)
320 i
=vsprintf(buf
,fmt
,args
);