-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkernio.c
115 lines (108 loc) · 2.6 KB
/
kernio.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
#include "kernio.h"
#include "terminal.h"
#include "common.h"
#include <stdarg.h>
int BOOTED;
// Potential base for stdarg. Doesn't work.
// We need to know more about how gcc handles the stack.
//typedef char *va_list;
//#define va_start(ap,parmn) (void)((ap) = (char*)(&(parmn) + 1))
//#define va_end(ap) (void)((ap) = 0)
//#define va_arg(ap, type) \
// (((type*)((ap) = ((ap) + sizeof(type))))[-1])
int printf(char *fmt, ...) {
//return;
va_list argp;
va_start(argp, fmt);
char *p;
for(p = fmt; *p != 0; p++) {
if(*p != '%') {
terminal_putchar(*p);
continue;
}
p++; // Skip the %
int i;
char *s;
switch(*p) {
case 'c':
i = va_arg(argp, int);
terminal_putchar(i);
break;
case 's':
s = va_arg(argp, char *);
terminal_writestring(s);
break;
case 'd':
i = va_arg(argp, int);
terminal_write_dec(i);
break;
case 'x':
i = va_arg(argp, int);
terminal_write_hex(i);
break;
case '%':
terminal_putchar('%');
break;
default:
terminal_putchar('%');
terminal_putchar(*p);
break;
}
}
return 0;
}
int sprintf(char *str, char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
char *p;
for(p = fmt; *p != 0; p++) {
if(*p != '%') {
*str++ = *p;
continue;
}
p++; // Skip the %
int i;
char *s;
switch(*p) {
case 'c':
i = va_arg(argp, int);
*str++ = i;
break;
case 's':
s = va_arg(argp, char *);
while(*s) {
*str++ = *s++;
}
break;
case 'd':
i = va_arg(argp, int);
char decbuff[13]; // At most 12 decimal places for 32 bit int.
char *dec = itos(i, decbuff, 13);
while(*dec) {
*str++ = *dec++;
}
break;
case 'x':
i = va_arg(argp, int);
for(int j = 28; j >= 0; j-=4)
{
*str++ = hex_char(i>>j);
}
break;
case '%':
*str++ = '%';
break;
default:
*str++ = '%';
*str++ = *p;
break;
}
}
*str++ = 0;
return 0;
}
void set_status(char *str) {
if(terminal_set_status) {
terminal_set_status(str);
}
}