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 *);
21 return (ch >= '0') && (ch <= '9');
24 unsigned int skip_atou(const char **s);
25 unsigned int atou(const char *s);
27 static int strnlen(const char *s, int maxlen)
30 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
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) {
89 tmp[i++] = digits[do_div(num,base)];
93 if (!(type&(ZEROPAD+LEFT)))
109 while (i < precision--)
118 /* Forward decl. needed for IP address printing stuff... */
119 int sprintf(char * buf, const char *fmt, ...);
121 int vsprintf(char *buf, const char *fmt, va_list args)
129 int flags; /* flags to number() */
131 int field_width; /* width of output field */
132 int precision; /* min. # of digits for integers; max
133 number of chars for from string */
134 int qualifier; /* 'h', 'l', or 'L' for integer fields */
136 for (str=buf ; *fmt ; ++fmt) {
145 ++fmt; /* this also skips first '%' */
147 case '-': flags |= LEFT; goto repeat;
148 case '+': flags |= PLUS; goto repeat;
149 case ' ': flags |= SPACE; goto repeat;
150 case '#': flags |= SPECIAL; goto repeat;
151 case '0': flags |= ZEROPAD; goto repeat;
154 /* get field width */
157 field_width = skip_atou(&fmt);
158 else if (*fmt == '*') {
160 /* it's the next argument */
161 field_width = va_arg(args, int);
162 if (field_width < 0) {
163 field_width = -field_width;
168 /* get the precision */
173 precision = skip_atou(&fmt);
174 else if (*fmt == '*') {
176 /* it's the next argument */
177 precision = va_arg(args, int);
183 /* get the conversion qualifier */
185 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
196 while (--field_width > 0)
198 *str++ = (unsigned char) va_arg(args, int);
199 while (--field_width > 0)
204 s = va_arg(args, char *);
205 len = strnlen(s, precision);
208 while (len < field_width--)
210 for (i = 0; i < len; ++i)
212 while (len < field_width--)
217 if (field_width == -1) {
218 field_width = 2*sizeof(void *);
222 (unsigned long) va_arg(args, void *), 16,
223 field_width, precision, flags);
228 if (qualifier == 'l') {
229 long * ip = va_arg(args, long *);
232 int * ip = va_arg(args, int *);
241 /* integer number formats - set up the flags and "break" */
266 if (qualifier == 'l')
267 num = va_arg(args, unsigned long);
268 else if (qualifier == 'h') {
269 num = (unsigned short) va_arg(args, int);
272 } else if (flags & SIGN)
273 num = va_arg(args, int);
275 num = va_arg(args, unsigned int);
276 str = number(str, num, base, field_width, precision, flags);
282 int sprintf(char * buf, const char *fmt, ...)
288 i=vsprintf(buf,fmt,args);
293 int printf(const char *fmt, ...)
295 char printf_buf[1024];
300 printed = vsprintf(printf_buf, fmt, args);