1 #ident "$Id: conio.c,v 1.8 2004/12/14 22:46:25 hpa Exp $"
2 /* ----------------------------------------------------------------------- *
4 * Copyright 2001 H. Peter Anvin - All Rights Reserved
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
17 * Output to the screen
33 regs.eax.w[0] = 0x0e00|(ch&0xff);
34 syscall(0x10, ®s, NULL);
39 int puts(const char *s)
53 * Oh, it's a waste of space, but oh-so-yummy for debugging. It's just
54 * initialization code anyway, so it doesn't take up space when we're
55 * actually running. This version of printf() does not include 64-bit
56 * support. "Live with it."
58 * Most of this code was shamelessly snarfed from the Linux kernel, then
65 return (ch >= '0') && (ch <= '9');
68 static int skip_atoi(const char **s)
73 i = i*10 + *((*s)++) - '0';
77 unsigned int atou(const char *s)
81 i = i*10 + (*s++ - '0');
85 static int strnlen(const char *s, int maxlen)
88 while ( *es && maxlen ) {
95 #define ZEROPAD 1 /* pad with zero */
96 #define SIGN 2 /* unsigned/signed long */
97 #define PLUS 4 /* show plus */
98 #define SPACE 8 /* space if plus */
99 #define LEFT 16 /* left justified */
100 #define SPECIAL 32 /* 0x */
101 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
103 #define do_div(n,base) ({ \
105 __res = ((unsigned long) n) % (unsigned) base; \
106 n = ((unsigned long) n) / (unsigned) base; \
109 static char * number(char * str, long num, int base, int size, int precision
113 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
117 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
120 if (base < 2 || base > 36)
122 c = (type & ZEROPAD) ? '0' : ' ';
129 } else if (type & PLUS) {
132 } else if (type & SPACE) {
137 if (type & SPECIAL) {
146 else while (num != 0)
147 tmp[i++] = digits[do_div(num,base)];
151 if (!(type&(ZEROPAD+LEFT)))
156 if (type & SPECIAL) {
167 while (i < precision--)
176 /* Forward decl. needed for IP address printing stuff... */
177 int sprintf(char * buf, const char *fmt, ...);
179 int vsprintf(char *buf, const char *fmt, va_list args)
187 int flags; /* flags to number() */
189 int field_width; /* width of output field */
190 int precision; /* min. # of digits for integers; max
191 number of chars for from string */
192 int qualifier; /* 'h', 'l', or 'L' for integer fields */
194 for (str=buf ; *fmt ; ++fmt) {
203 ++fmt; /* this also skips first '%' */
205 case '-': flags |= LEFT; goto repeat;
206 case '+': flags |= PLUS; goto repeat;
207 case ' ': flags |= SPACE; goto repeat;
208 case '#': flags |= SPECIAL; goto repeat;
209 case '0': flags |= ZEROPAD; goto repeat;
212 /* get field width */
215 field_width = skip_atoi(&fmt);
216 else if (*fmt == '*') {
218 /* it's the next argument */
219 field_width = va_arg(args, int);
220 if (field_width < 0) {
221 field_width = -field_width;
226 /* get the precision */
231 precision = skip_atoi(&fmt);
232 else if (*fmt == '*') {
234 /* it's the next argument */
235 precision = va_arg(args, int);
241 /* get the conversion qualifier */
243 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
254 while (--field_width > 0)
256 *str++ = (unsigned char) va_arg(args, int);
257 while (--field_width > 0)
262 s = va_arg(args, char *);
263 len = strnlen(s, precision);
266 while (len < field_width--)
268 for (i = 0; i < len; ++i)
270 while (len < field_width--)
275 if (field_width == -1) {
276 field_width = 2*sizeof(void *);
280 (unsigned long) va_arg(args, void *), 16,
281 field_width, precision, flags);
286 if (qualifier == 'l') {
287 long * ip = va_arg(args, long *);
290 int * ip = va_arg(args, int *);
299 /* integer number formats - set up the flags and "break" */
324 if (qualifier == 'l')
325 num = va_arg(args, unsigned long);
326 else if (qualifier == 'h') {
327 num = (unsigned short) va_arg(args, int);
330 } else if (flags & SIGN)
331 num = va_arg(args, int);
333 num = va_arg(args, unsigned int);
334 str = number(str, num, base, field_width, precision, flags);
340 int sprintf(char * buf, const char *fmt, ...)
346 i=vsprintf(buf,fmt,args);
351 int printf(const char *fmt, ...)
353 char printf_buf[1024];
358 printed = vsprintf(printf_buf, fmt, args);