-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint.c
158 lines (132 loc) · 2.11 KB
/
print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdarg.h>
#include "print.h"
#include "stdlib.h"
#include "tty.h"
#define BUF_SIZ 64
unsigned short *vmem = (unsigned short *)0xB8000;
void clear_screen(colour c)
{
unsigned char colour;
unsigned int i;
switch(c)
{
case GREEN:
colour = 0xAA;
break;
default:
colour = 0x00;
break;
}
vmem = (unsigned short *)0xB8000;
for(i = 0; i < 80*25; i++)
{
vmem[i] = colour<<8;
}
}
static void newline(void)
{
long p = (long)vmem;
p = p - ((p - 0xB8000) % 160);
p += 160;
vmem = (unsigned short *)p;
}
static void putchar(unsigned char c)
{
tty_putchar(c);
}
static void put_number(unsigned long n, int islong, int base, char pad_char, int width)
{
unsigned char digit;
int len;
char buf[BUF_SIZ];
char *p = &buf[BUF_SIZ-1];
if(!islong)
n = (unsigned int)n;
do {
digit = n % base;
n /= base;
*p-- = digit["0123456789ABCDEF"];
} while(n);
len = &buf[BUF_SIZ-1] - p;
while(width && len < width)
{
*p-- = pad_char;
len++;
}
while(++p != &buf[BUF_SIZ])
putchar(*p);
}
static void puts(const char *str)
{
tty_write_str(str);
}
static void vprint(const char *fmt, va_list ap)
{
const char *p;
unsigned char c;
unsigned long l;
int long_int;
char pad_char;
int width;
int base;
p = fmt;
while(1)
{
width = 0;
pad_char = ' ';
long_int = 0;
for(; *p && *p != '%'; p++)
putchar(*p);
if(!*p)
return;
p++;
again: c = *p++;
if(c == '0')
{
pad_char = '0';
c = *p++;
}
while(c >= '0' && c <= '9')
{
width = width*10 + c - '0';
c = *p++;
}
switch(c)
{
case '%':
putchar(c);
break;
case 'l':
long_int = 1;
goto again;
break;
case 'X':
case 'x':
base = 16;
goto print_num;
case 'd':
base = 10;
goto print_num;
case 's':
puts(va_arg(ap, const char *));
break;
case 'c':
putchar(va_arg(ap, int));
break;
print_num:
if(long_int)
l = va_arg(ap, long);
else
l = va_arg(ap, int);
put_number(l, long_int, base, pad_char, width);
break;
}
}
}
void printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprint(fmt, ap);
va_end(ap);
}