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
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
35 ? toupper(*cp
) : *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 /* we use this so that we can do without the ctype library */
52 #define is_digit(c) ((c) >= '0' && (c) <= '9')
54 static int skip_atoi(const char **s
)
59 i
= i
*10 + *((*s
)++) - '0';
63 #define ZEROPAD 1 /* pad with zero */
64 #define SIGN 2 /* unsigned/signed long */
65 #define PLUS 4 /* show plus */
66 #define SPACE 8 /* space if plus */
67 #define LEFT 16 /* left justified */
68 #define SPECIAL 32 /* 0x */
69 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
71 static char * number(char * str
, long long num
, int base
, int size
, int precision
, int type
)
74 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
78 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
81 if (base
< 2 || base
> 36)
83 c
= (type
& ZEROPAD
) ? '0' : ' ';
90 } else if (type
& PLUS
) {
93 } else if (type
& SPACE
) {
107 else while (num
!= 0)
108 tmp
[i
++] = digits
[do_div(num
,base
)];
112 if (!(type
&(ZEROPAD
+LEFT
)))
117 if (type
& SPECIAL
) {
128 while (i
< precision
--)
137 /* Forward decl. needed for IP address printing stuff... */
138 int sprintf(char * buf
, const char *fmt
, ...);
140 int vsprintf(char *buf
, const char *fmt
, va_list args
)
143 unsigned long long num
;
148 int flags
; /* flags to number() */
150 int field_width
; /* width of output field */
151 int precision
; /* min. # of digits for integers; max
152 number of chars for from string */
153 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
154 /* 'z' support added 23/7/1999 S.H. */
155 /* 'z' changed to 'Z' --davidm 1/25/99 */
158 for (str
=buf
; *fmt
; ++fmt
) {
167 ++fmt
; /* this also skips first '%' */
169 case '-': flags
|= LEFT
; goto repeat
;
170 case '+': flags
|= PLUS
; goto repeat
;
171 case ' ': flags
|= SPACE
; goto repeat
;
172 case '#': flags
|= SPECIAL
; goto repeat
;
173 case '0': flags
|= ZEROPAD
; goto repeat
;
176 /* get field width */
179 field_width
= skip_atoi(&fmt
);
180 else if (*fmt
== '*') {
182 /* it's the next argument */
183 field_width
= va_arg(args
, int);
184 if (field_width
< 0) {
185 field_width
= -field_width
;
190 /* get the precision */
195 precision
= skip_atoi(&fmt
);
196 else if (*fmt
== '*') {
198 /* it's the next argument */
199 precision
= va_arg(args
, int);
205 /* get the conversion qualifier */
207 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
=='Z') {
218 while (--field_width
> 0)
220 *str
++ = (unsigned char) va_arg(args
, int);
221 while (--field_width
> 0)
226 s
= va_arg(args
, char *);
230 len
= strnlen(s
, precision
);
233 while (len
< field_width
--)
235 for (i
= 0; i
< len
; ++i
)
237 while (len
< field_width
--)
242 if (field_width
== -1) {
243 field_width
= 2*sizeof(void *);
247 (unsigned long) va_arg(args
, void *), 16,
248 field_width
, precision
, flags
);
253 if (qualifier
== 'l') {
254 long * ip
= va_arg(args
, long *);
256 } else if (qualifier
== 'Z') {
257 size_t * ip
= va_arg(args
, size_t *);
260 int * ip
= va_arg(args
, int *);
269 /* integer number formats - set up the flags and "break" */
294 if (qualifier
== 'L')
295 num
= va_arg(args
, long long);
296 else if (qualifier
== 'l') {
297 num
= va_arg(args
, unsigned long);
299 num
= (signed long) num
;
300 } else if (qualifier
== 'Z') {
301 num
= va_arg(args
, size_t);
302 } else if (qualifier
== 'h') {
303 num
= (unsigned short) va_arg(args
, int);
305 num
= (signed short) num
;
307 num
= va_arg(args
, unsigned int);
309 num
= (signed int) num
;
311 str
= number(str
, num
, base
, field_width
, precision
, flags
);
317 int sprintf(char * buf
, const char *fmt
, ...)
323 i
=vsprintf(buf
,fmt
,args
);