2 * Oh, it's a waste of space, but oh-so-yummy for debugging. It's just
3 * initialization code anyway, so it doesn't take up space when we're
4 * actually running. This version of printf() does not include 64-bit
5 * support. "Live with it."
7 * Most of this code was shamelessly snarfed from the Linux kernel, then
10 * FIX THIS: Replace printf() implementation with BSD/MIT-licensed one
16 int puts(const char *);
18 static inline int isdigit(int ch
)
20 return (ch
>= '0') && (ch
<= '9');
23 unsigned int skip_atou(const char **s
);
24 unsigned int atou(const char *s
);
26 static int strnlen(const char *s
, int maxlen
)
29 while (*es
&& maxlen
) {
37 #define ZEROPAD 1 /* pad with zero */
38 #define SIGN 2 /* unsigned/signed long */
39 #define PLUS 4 /* show plus */
40 #define SPACE 8 /* space if plus */
41 #define LEFT 16 /* left justified */
42 #define SPECIAL 32 /* 0x */
43 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
45 #define do_div(n,base) ({ \
47 __res = ((unsigned long) n) % (unsigned) base; \
48 n = ((unsigned long) n) / (unsigned) base; \
51 static char *number(char *str
, long num
, int base
, int size
, int precision
,
54 char c
, sign
, tmp
[66];
55 const char *digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
59 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
62 if (base
< 2 || base
> 36)
64 c
= (type
& ZEROPAD
) ? '0' : ' ';
71 } else if (type
& PLUS
) {
74 } else if (type
& SPACE
) {
90 tmp
[i
++] = digits
[do_div(num
, base
)];
94 if (!(type
& (ZEROPAD
+ LEFT
)))
102 else if (base
== 16) {
110 while (i
< precision
--)
119 /* Forward decl. needed for IP address printing stuff... */
120 int sprintf(char *buf
, const char *fmt
, ...);
122 int vsprintf(char *buf
, const char *fmt
, va_list args
)
130 int flags
; /* flags to number() */
132 int field_width
; /* width of output field */
133 int precision
; /* min. # of digits for integers; max
134 number of chars for from string */
135 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
137 for (str
= buf
; *fmt
; ++fmt
) {
146 ++fmt
; /* this also skips first '%' */
165 /* get field width */
168 field_width
= skip_atou(&fmt
);
169 else if (*fmt
== '*') {
171 /* it's the next argument */
172 field_width
= va_arg(args
, int);
173 if (field_width
< 0) {
174 field_width
= -field_width
;
179 /* get the precision */
184 precision
= skip_atou(&fmt
);
185 else if (*fmt
== '*') {
187 /* it's the next argument */
188 precision
= va_arg(args
, int);
194 /* get the conversion qualifier */
196 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
207 while (--field_width
> 0)
209 *str
++ = (unsigned char)va_arg(args
, int);
210 while (--field_width
> 0)
215 s
= va_arg(args
, char *);
216 len
= strnlen(s
, precision
);
219 while (len
< field_width
--)
221 for (i
= 0; i
< len
; ++i
)
223 while (len
< field_width
--)
228 if (field_width
== -1) {
229 field_width
= 2 * sizeof(void *);
233 (unsigned long)va_arg(args
, void *), 16,
234 field_width
, precision
, flags
);
238 if (qualifier
== 'l') {
239 long *ip
= va_arg(args
, long *);
242 int *ip
= va_arg(args
, int *);
251 /* integer number formats - set up the flags and "break" */
276 if (qualifier
== 'l')
277 num
= va_arg(args
, unsigned long);
278 else if (qualifier
== 'h') {
279 num
= (unsigned short)va_arg(args
, int);
282 } else if (flags
& SIGN
)
283 num
= va_arg(args
, int);
285 num
= va_arg(args
, unsigned int);
286 str
= number(str
, num
, base
, field_width
, precision
, flags
);
292 int sprintf(char *buf
, const char *fmt
, ...)
298 i
= vsprintf(buf
, fmt
, args
);
303 int printf(const char *fmt
, ...)
305 char printf_buf
[1024];
310 printed
= vsprintf(printf_buf
, fmt
, args
);