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
8 * modified. It's therefore GPL.
10 * printf() isn't actually needed to build syslinux.com, but during
11 * debugging it's handy.
18 static int strnlen(const char *s
, int maxlen
)
21 while (*es
&& maxlen
) {
29 #define ZEROPAD 1 /* pad with zero */
30 #define SIGN 2 /* unsigned/signed long */
31 #define PLUS 4 /* show plus */
32 #define SPACE 8 /* space if plus */
33 #define LEFT 16 /* left justified */
34 #define SPECIAL 32 /* 0x */
35 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
37 #define do_div(n,base) ({ \
39 __res = ((unsigned long) n) % (unsigned) base; \
40 n = ((unsigned long) n) / (unsigned) base; \
43 static char *number(char *str
, long num
, int base
, int size
, int precision
,
46 char c
, sign
, tmp
[66];
47 const char *digits
= "0123456789abcdefghijklmnopqrstuvwxyz";
51 digits
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
54 if (base
< 2 || base
> 36)
56 c
= (type
& ZEROPAD
) ? '0' : ' ';
63 } else if (type
& PLUS
) {
66 } else if (type
& SPACE
) {
82 tmp
[i
++] = digits
[do_div(num
, base
)];
86 if (!(type
& (ZEROPAD
+ LEFT
)))
94 else if (base
== 16) {
102 while (i
< precision
--)
111 /* Forward decl. needed for IP address printing stuff... */
112 int sprintf(char *buf
, const char *fmt
, ...);
114 int vsprintf(char *buf
, const char *fmt
, va_list args
)
122 int flags
; /* flags to number() */
124 int field_width
; /* width of output field */
125 int precision
; /* min. # of digits for integers; max
126 number of chars for from string */
127 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
129 for (str
= buf
; *fmt
; ++fmt
) {
138 ++fmt
; /* this also skips first '%' */
157 /* get field width */
160 field_width
= skip_atou(&fmt
);
161 else if (*fmt
== '*') {
163 /* it's the next argument */
164 field_width
= va_arg(args
, int);
165 if (field_width
< 0) {
166 field_width
= -field_width
;
171 /* get the precision */
176 precision
= skip_atou(&fmt
);
177 else if (*fmt
== '*') {
179 /* it's the next argument */
180 precision
= va_arg(args
, int);
186 /* get the conversion qualifier */
188 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
199 while (--field_width
> 0)
201 *str
++ = (unsigned char)va_arg(args
, int);
202 while (--field_width
> 0)
207 s
= va_arg(args
, char *);
208 len
= strnlen(s
, precision
);
211 while (len
< field_width
--)
213 for (i
= 0; i
< len
; ++i
)
215 while (len
< field_width
--)
220 if (field_width
== -1) {
221 field_width
= 2 * sizeof(void *);
225 (unsigned long)va_arg(args
, void *), 16,
226 field_width
, precision
, flags
);
230 if (qualifier
== 'l') {
231 long *ip
= va_arg(args
, long *);
234 int *ip
= va_arg(args
, int *);
243 /* integer number formats - set up the flags and "break" */
268 if (qualifier
== 'l')
269 num
= va_arg(args
, unsigned long);
270 else if (qualifier
== 'h') {
271 num
= (unsigned short)va_arg(args
, int);
274 } else if (flags
& SIGN
)
275 num
= va_arg(args
, int);
277 num
= va_arg(args
, unsigned int);
278 str
= number(str
, num
, base
, field_width
, precision
, flags
);
284 int sprintf(char *buf
, const char *fmt
, ...)
290 i
= vsprintf(buf
, fmt
, args
);
295 int printf(const char *fmt
, ...)
297 char printf_buf
[1024];
302 printed
= vsprintf(printf_buf
, fmt
, args
);