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
26 memset(®s
, 0, sizeof regs
);
33 regs
.eax
.w
[0] = 0x0e00 | (ch
& 0xff);
34 intcall(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
62 static inline int isdigit(int ch
)
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
) {
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
,
112 char c
, sign
, tmp
[66];
113 const char *digits
= "0123456789abcdef";
117 digits
= "0123456789ABCDEF";
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
) {
148 tmp
[i
++] = digits
[do_div(num
, base
)];
152 if (!(type
& (ZEROPAD
+ LEFT
)))
157 if (type
& SPECIAL
) {
160 else if (base
== 16) {
168 while (i
< precision
--)
177 int vsprintf(char *buf
, const char *fmt
, va_list args
)
185 int flags
; /* flags to number() */
187 int field_width
; /* width of output field */
188 int precision
; /* min. # of digits for integers; max
189 number of chars for from string */
190 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
192 for (str
= buf
; *fmt
; ++fmt
) {
201 ++fmt
; /* this also skips first '%' */
220 /* get field width */
223 field_width
= skip_atoi(&fmt
);
224 else if (*fmt
== '*') {
226 /* it's the next argument */
227 field_width
= va_arg(args
, int);
228 if (field_width
< 0) {
229 field_width
= -field_width
;
234 /* get the precision */
239 precision
= skip_atoi(&fmt
);
240 else if (*fmt
== '*') {
242 /* it's the next argument */
243 precision
= va_arg(args
, int);
249 /* get the conversion qualifier */
251 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L') {
262 while (--field_width
> 0)
264 *str
++ = (unsigned char)va_arg(args
, int);
265 while (--field_width
> 0)
270 s
= va_arg(args
, char *);
271 len
= strnlen(s
, precision
);
274 while (len
< field_width
--)
276 for (i
= 0; i
< len
; ++i
)
278 while (len
< field_width
--)
283 if (field_width
== -1) {
284 field_width
= 2 * sizeof(void *);
288 (unsigned long)va_arg(args
, void *), 16,
289 field_width
, precision
, flags
);
293 if (qualifier
== 'l') {
294 long *ip
= va_arg(args
, long *);
297 int *ip
= va_arg(args
, int *);
306 /* integer number formats - set up the flags and "break" */
331 if (qualifier
== 'l')
332 num
= va_arg(args
, unsigned long);
333 else if (qualifier
== 'h') {
334 num
= (unsigned short)va_arg(args
, int);
337 } else if (flags
& SIGN
)
338 num
= va_arg(args
, int);
340 num
= va_arg(args
, unsigned int);
341 str
= number(str
, num
, base
, field_width
, precision
, flags
);
348 int sprintf(char *buf
, const char *fmt
, ...)
354 i
= vsprintf(buf
, fmt
, args
);
360 int vprintf(const char *fmt
, va_list args
)
362 char printf_buf
[2048];
365 printed
= vsprintf(printf_buf
, fmt
, args
);
370 int printf(const char *fmt
, ...)
376 printed
= vprintf(fmt
, args
);
382 * Jump here if all hope is gone...
384 void __attribute__ ((noreturn
)) die(const char *fmt
, ...)