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 unsigned long simple_strtoul(const char *cp
,char **endp
,unsigned int base
)
19 unsigned long result
= 0,value
;
26 if ((*cp
== 'x') && isxdigit(cp
[1])) {
32 while (isxdigit(*cp
) && (value
= isdigit(*cp
) ? *cp
-'0' : (islower(*cp
)
33 ? toupper(*cp
) : *cp
)-'A'+10) < base
) {
34 result
= result
*base
+ value
;
42 long simple_strtol(const char *cp
,char **endp
,unsigned int base
)
45 return -simple_strtoul(cp
+1,endp
,base
);
46 return simple_strtoul(cp
,endp
,base
);
49 /* we use this so that we can do without the ctype library */
50 #define is_digit(c) ((c) >= '0' && (c) <= '9')
52 static int skip_atoi(const char **s
)
57 i
= i
*10 + *((*s
)++) - '0';
61 #define ZEROPAD 1 /* pad with zero */
62 #define SIGN 2 /* unsigned/signed long */
63 #define PLUS 4 /* show plus */
64 #define SPACE 8 /* space if plus */
65 #define LEFT 16 /* left justified */
66 #define SPECIAL 32 /* 0x */
67 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
69 #define do_div(n,base) ({ \
71 __res = ((unsigned long) n) % (unsigned) base; \
72 n = ((unsigned long) n) / (unsigned) base; \
75 static char * number(char * str
, long num
, int base
, int size
, int precision
79 const char *digits
="0123456789abcdefghijklmnopqrstuvwxyz";
83 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
86 if (base
< 2 || base
> 36)
88 c
= (type
& ZEROPAD
) ? '0' : ' ';
95 } else if (type
& PLUS
) {
98 } else if (type
& SPACE
) {
103 if (type
& SPECIAL
) {
112 else while (num
!= 0)
113 tmp
[i
++] = digits
[do_div(num
,base
)];
117 if (!(type
&(ZEROPAD
+LEFT
)))
122 if (type
& SPECIAL
) {
133 while (i
< precision
--)
142 /* Forward decl. needed for IP address printing stuff... */
143 int sprintf(char * buf
, const char *fmt
, ...);
145 int vsprintf(char *buf
, const char *fmt
, va_list args
)
153 int flags
; /* flags to number() */
155 int field_width
; /* width of output field */
156 int precision
; /* min. # of digits for integers; max
157 number of chars for from string */
158 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
160 for (str
=buf
; *fmt
; ++fmt
) {
169 ++fmt
; /* this also skips first '%' */
171 case '-': flags
|= LEFT
; goto repeat
;
172 case '+': flags
|= PLUS
; goto repeat
;
173 case ' ': flags
|= SPACE
; goto repeat
;
174 case '#': flags
|= SPECIAL
; goto repeat
;
175 case '0': flags
|= ZEROPAD
; goto repeat
;
178 /* get field width */
181 field_width
= skip_atoi(&fmt
);
182 else if (*fmt
== '*') {
184 /* it's the next argument */
185 field_width
= va_arg(args
, int);
186 if (field_width
< 0) {
187 field_width
= -field_width
;
192 /* get the precision */
197 precision
= skip_atoi(&fmt
);
198 else if (*fmt
== '*') {
200 /* it's the next argument */
201 precision
= va_arg(args
, int);
207 /* get the conversion qualifier */
209 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
220 while (--field_width
> 0)
222 *str
++ = (unsigned char) va_arg(args
, int);
223 while (--field_width
> 0)
228 s
= va_arg(args
, char *);
232 len
= strnlen(s
, precision
);
235 while (len
< field_width
--)
237 for (i
= 0; i
< len
; ++i
)
239 while (len
< field_width
--)
244 if (field_width
== -1) {
245 field_width
= 2*sizeof(void *);
249 (unsigned long) va_arg(args
, void *), 16,
250 field_width
, precision
, flags
);
255 if (qualifier
== 'l') {
256 long * ip
= va_arg(args
, long *);
259 int * ip
= va_arg(args
, int *);
264 /* integer number formats - set up the flags and "break" */
290 if (qualifier
== 'l')
291 num
= va_arg(args
, unsigned long);
292 else if (qualifier
== 'h') {
294 num
= va_arg(args
, short);
296 num
= va_arg(args
, unsigned short);
297 } else if (flags
& SIGN
)
298 num
= va_arg(args
, int);
300 num
= va_arg(args
, unsigned int);
301 str
= number(str
, num
, base
, field_width
, precision
, flags
);
307 int sprintf(char * buf
, const char *fmt
, ...)
313 i
=vsprintf(buf
,fmt
,args
);