1 /* ----------------------------------------------------------------------- *
3 * Copyright 2001-2008 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 intcall(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
61 static inline int isdigit(int ch)
63 return (ch >= '0') && (ch <= '9');
66 static int skip_atoi(const char **s)
71 i = i * 10 + *((*s)++) - '0';
75 unsigned int atou(const char *s)
79 i = i * 10 + (*s++ - '0');
83 static int strnlen(const char *s, int maxlen)
86 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,
111 char c, sign, tmp[66];
112 const char *digits = "0123456789abcdef";
116 digits = "0123456789ABCDEF";
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) {
147 tmp[i++] = digits[do_div(num, base)];
151 if (!(type & (ZEROPAD + LEFT)))
156 if (type & SPECIAL) {
159 else if (base == 16) {
167 while (i < precision--)
176 int vsprintf(char *buf, const char *fmt, va_list args)
184 int flags; /* flags to number() */
186 int field_width; /* width of output field */
187 int precision; /* min. # of digits for integers; max
188 number of chars for from string */
189 int qualifier; /* 'h', 'l', or 'L' for integer fields */
191 for (str = buf; *fmt; ++fmt) {
200 ++fmt; /* this also skips first '%' */
219 /* get field width */
222 field_width = skip_atoi(&fmt);
223 else if (*fmt == '*') {
225 /* it's the next argument */
226 field_width = va_arg(args, int);
227 if (field_width < 0) {
228 field_width = -field_width;
233 /* get the precision */
238 precision = skip_atoi(&fmt);
239 else if (*fmt == '*') {
241 /* it's the next argument */
242 precision = va_arg(args, int);
248 /* get the conversion qualifier */
250 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
261 while (--field_width > 0)
263 *str++ = (unsigned char)va_arg(args, int);
264 while (--field_width > 0)
269 s = va_arg(args, char *);
270 len = strnlen(s, precision);
273 while (len < field_width--)
275 for (i = 0; i < len; ++i)
277 while (len < field_width--)
282 if (field_width == -1) {
283 field_width = 2 * sizeof(void *);
287 (unsigned long)va_arg(args, void *), 16,
288 field_width, precision, flags);
292 if (qualifier == 'l') {
293 long *ip = va_arg(args, long *);
296 int *ip = va_arg(args, int *);
305 /* integer number formats - set up the flags and "break" */
330 if (qualifier == 'l')
331 num = va_arg(args, unsigned long);
332 else if (qualifier == 'h') {
333 num = (unsigned short)va_arg(args, int);
336 } else if (flags & SIGN)
337 num = va_arg(args, int);
339 num = va_arg(args, unsigned int);
340 str = number(str, num, base, field_width, precision, flags);
347 int sprintf(char *buf, const char *fmt, ...)
353 i = vsprintf(buf, fmt, args);
359 int vprintf(const char *fmt, va_list args)
361 char printf_buf[2048];
364 printed = vsprintf(printf_buf, fmt, args);
369 int printf(const char *fmt, ...)
375 printed = vprintf(fmt, args);
381 * Jump here if all hope is gone...
383 void __attribute__ ((noreturn)) die(const char *fmt, ...)