forked from mwkmwkmwk/zso-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdio.c
177 lines (165 loc) · 4.73 KB
/
stdio.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdarg.h>
#include "io.h"
#include "malloc.h"
#include "stdio.h"
#include "utils.h"
static int row = 2;
static int col = 0;
static void __new_line() {
row = (row + 1) % SCREEN_HEIGHT;
col = 0;
}
void putc(char c) {
if (c == '\n') {
__new_line();
return;
}
ekran[row][col] = c | (PRINT_COLOR << 8);
if (++col >= SCREEN_WIDTH) {
__new_line();
}
}
size_t puts(const char *p) {
char c = 0;
size_t len = 0;
while (c = *p++) {
putc(c);
++len;
}
return len;
}
static void print_padding(char pad, size_t pad_len, size_t len) {
size_t i = 0;
if (len < pad_len) {
for (i = 0; i < pad_len - len; ++i) {
putc(pad);
}
}
}
void printf(const char *fmt, ...) {
va_list vl;
char c = 0,
pad = ' ';
const char *pad_size_st = NULL;
char *str = NULL;
uint64_t pad_size = 0;
size_t pad_str_len = 0,
pad_len = 0,
i = 0,
print_len = 0;
uint64_t x = 0;
int size_mod = 0;
va_start(vl, fmt);
while (c = *fmt++) {
if (c == '%') {
pad_size_st = fmt;
if ((c = *fmt) == '0') {
pad = '0';
++pad_size_st;
++fmt;
}
while (c = *fmt, is_digit(c)) {
++fmt;
}
pad_str_len = fmt - pad_size_st;
if (pad_str_len) {
pad_len = str_to_uint(pad_size_st, pad_str_len);
} else {
pad_len = 0;
}
switch (c = *fmt) {
case 'h':
++fmt;
if (*fmt == 'h') {
++fmt;
size_mod = PRINT_SIZE_8;
} else {
size_mod = PRINT_SIZE_16;
}
break;
case 'l':
size_mod = PRINT_SIZE_64;
++fmt;
break;
default:
size_mod = PRINT_SIZE_32;
break;
}
switch (c = *fmt++) {
case 'd':
case 'u':
case 'x':
switch (size_mod) {
case PRINT_SIZE_8:
x = va_arg(vl, uint32_t);
x &= 0xff;
break;
case PRINT_SIZE_16:
x = va_arg(vl, uint32_t);
x &= 0xffff;
break;
case PRINT_SIZE_32:
x = va_arg(vl, uint32_t);
break;
case PRINT_SIZE_64:
x = va_arg(vl, uint64_t);
break;
}
switch (c) {
case 'd':
print_len = int_to_str_dec(x, &str, size_mod);
break;
case 'u':
print_len = int_to_str_udec(x, &str);
break;
case 'x':
print_len = int_to_str_hex(x, &str);
break;
}
print_padding(pad, pad_len, print_len);
puts(str);
free(str);
break;
case 's':
str = va_arg(vl, char *);
print_len = str_len(str);
print_padding(pad, pad_len, print_len);
puts(str);
break;
default:
// TODO FIXME XXX KURWA start_ptr
putc('%');
if (pad == '0') {
putc('0');
}
for (i = 0; i < pad_len; ++i) {
putc(pad_size_st[i]);
}
switch (size_mod) {
case -2:
putc('h');
putc('h');
break;
case -1:
putc('h');
break;
case 1:
putc('l');
break;
default:
break;
}
putc(c);
break;
}
pad = ' ';
} else {
putc(c);
}
}
va_end(vl);
}
int getc() {
while (!(inb(0x64) & 1));
return inb(0x60);
}