1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * Output to the screen
32 regs.eax.w[0] = 0x0e00|(ch&0xff);
33 syscall(0x10, ®s, NULL);
38 int puts(const char *s)
52 * Oh, it's a waste of space, but oh-so-yummy for debugging. It's just
53 * initialization code anyway, so it doesn't take up space when we're
54 * actually running. This version of printf() does not include 64-bit
55 * support. "Live with it."
57 * Most of this code was shamelessly snarfed from the Linux kernel, then
64 return (ch >= '0') && (ch <= '9');
67 static int skip_atoi(const char **s)
72 i = i*10 + *((*s)++) - '0';
76 unsigned int atou(const char *s)
80 i = i*10 + (*s++ - '0');
84 static int strnlen(const char *s, int maxlen)
87 while ( *es && maxlen ) {
94 #define ZEROPAD 1 /* pad with zero */
95 #define SIGN 2 /* unsigned/signed long */
96 #define PLUS 4 /* show plus */
97 #define SPACE 8 /* space if plus */
98 #define LEFT 16 /* left justified */
99 #define SPECIAL 32 /* 0x */
100 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
102 #define do_div(n,base) ({ \
104 __res = ((unsigned long) n) % (unsigned) base; \
105 n = ((unsigned long) n) / (unsigned) base; \
108 static char * number(char * str, long num, int base, int size, int precision
112 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
116 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
119 if (base < 2 || base > 36)
121 c = (type & ZEROPAD) ? '0' : ' ';
128 } else if (type & PLUS) {
131 } else if (type & SPACE) {
136 if (type & SPECIAL) {
145 else while (num != 0)
146 tmp[i++] = digits[do_div(num,base)];
150 if (!(type&(ZEROPAD+LEFT)))
155 if (type & SPECIAL) {
166 while (i < precision--)
175 /* Forward decl. needed for IP address printing stuff... */
176 int sprintf(char * buf, const char *fmt, ...);
178 int vsprintf(char *buf, const char *fmt, va_list args)
186 int flags; /* flags to number() */
188 int field_width; /* width of output field */
189 int precision; /* min. # of digits for integers; max
190 number of chars for from string */
191 int qualifier; /* 'h', 'l', or 'L' for integer fields */
193 for (str=buf ; *fmt ; ++fmt) {
202 ++fmt; /* this also skips first '%' */
204 case '-': flags |= LEFT; goto repeat;
205 case '+': flags |= PLUS; goto repeat;
206 case ' ': flags |= SPACE; goto repeat;
207 case '#': flags |= SPECIAL; goto repeat;
208 case '0': flags |= ZEROPAD; goto repeat;
211 /* get field width */
214 field_width = skip_atoi(&fmt);
215 else if (*fmt == '*') {
217 /* it's the next argument */
218 field_width = va_arg(args, int);
219 if (field_width < 0) {
220 field_width = -field_width;
225 /* get the precision */
230 precision = skip_atoi(&fmt);
231 else if (*fmt == '*') {
233 /* it's the next argument */
234 precision = va_arg(args, int);
240 /* get the conversion qualifier */
242 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
253 while (--field_width > 0)
255 *str++ = (unsigned char) va_arg(args, int);
256 while (--field_width > 0)
261 s = va_arg(args, char *);
262 len = strnlen(s, precision);
265 while (len < field_width--)
267 for (i = 0; i < len; ++i)
269 while (len < field_width--)
274 if (field_width == -1) {
275 field_width = 2*sizeof(void *);
279 (unsigned long) va_arg(args, void *), 16,
280 field_width, precision, flags);
285 if (qualifier == 'l') {
286 long * ip = va_arg(args, long *);
289 int * ip = va_arg(args, int *);
298 /* integer number formats - set up the flags and "break" */
323 if (qualifier == 'l')
324 num = va_arg(args, unsigned long);
325 else if (qualifier == 'h') {
326 num = (unsigned short) va_arg(args, int);
329 } else if (flags & SIGN)
330 num = va_arg(args, int);
332 num = va_arg(args, unsigned int);
333 str = number(str, num, base, field_width, precision, flags);
339 int sprintf(char * buf, const char *fmt, ...)
345 i=vsprintf(buf,fmt,args);
350 int printf(const char *fmt, ...)
352 char printf_buf[1024];
357 printed = vsprintf(printf_buf, fmt, args);