-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommon.c
191 lines (168 loc) · 3.35 KB
/
common.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "common.h"
#include "kernio.h"
#include "terminal.h"
#include "vesa.h"
#include "kheap.h"
extern void halt();
void *memset(void *p, int c, size_t count)
{
for(unsigned int i = 0; i < count; i++)
{
((char *)p)[i] = c;
}
return p;
}
void *memcpy(void *dest, const void *src, size_t n) {
char *cdest = dest;
const char *csrc = src;
size_t i;
for(i = 0; i < n; i++) {
cdest[i] = csrc[i];
}
return dest;
}
int k_toupper(int c) {
if(c >= 97 && c <= 122) {
return c - 32;
}
return c;
}
int k_tolower(int c) {
if(c >=65 && c <= 90) {
return c + 32;
}
return c;
}
char * itos(uint32_t myint, char buffer[], int bufflen)
{
int i = bufflen - 2;
buffer[bufflen-1] = 0;
if(myint == 0) {
buffer[i--] = '0';
}
while(myint > 0 && i >= 0)
{
buffer[i--] = (myint % 10) + '0';
myint/=10;
}
return &buffer[i+1];
}
size_t strlen(const char* str) {
size_t ret = 0;
while ( str[ret] != 0 )
ret++;
return ret;
}
int strcmp(const char *s1, const char *s2) {
while(*s1 && *s2) {
if(*s1 != *s2) {
return s1 - s2;
}
s1++;
s2++;
}
if(*s1) return 1;
if(*s2) return -1;
return 0;
}
char *strdup(const char *s) {
char *news = kmalloc(strlen(s) + 1);
char *temp = news;
while(*temp++ = *s++);
return news;
}
char *strchr(const char *s, int c) {
while(*s) {
if(*s == c) return s;
s++;
}
return NULL;
}
char *strtok_r(char *str, const char *delim, char **saveptr) {
char *begin;
if(str) {
begin = str;
}
else if (*saveptr) {
begin = *saveptr;
}
else {
return NULL;
}
while(strchr(delim, begin[0])) {
begin++;
}
char *next = NULL;
for(int i = 0; i < strlen(delim); i++) {
char *temp = strchr(begin, delim[i]);
if(temp < next || next == NULL) {
next = temp;
}
}
if(!next) {
*saveptr = NULL;
return begin;
}
*next = 0;
*saveptr=next+1;
return begin;
}
int coerce_int(char *s, uint32_t *val) {
uint32_t result = 0;
while(*s && *s != '\n') {
if(*s >= 48 && *s <= 57) {
result *= 10;
result = result + (*s - 48);
}
else {
return 0;
}
s++;
}
*val = result;
return 1;
}
uint8_t hex_char(uint8_t byte)
{
byte = byte & 0x0F;
if(byte < 0xA)
{
char buff[2];
itos(byte, buff, 2);
return buff[0];
}
else
{
switch(byte)
{
case 0x0A:
return 'A';
break;
case 0x0B:
return 'B';
break;
case 0x0C:
return 'C';
break;
case 0x0D:
return 'D';
break;
case 0x0E:
return 'E';
break;
case 0x0F:
return 'F';
break;
}
}
return 0;
}
void PANIC(char *err) {
terminal_set_cursor(0, 1);
set_vesa_color(make_vesa_color(0, 0, 0));
set_vesa_background(make_vesa_color(255, 0, 0));
// terminal_setcolor(make_color(COLOR_DARK_GREY, COLOR_BLACK));
// terminal_settextcolor(make_color(COLOR_RED, COLOR_BLACK));
printf("PANIC: %s", err);
halt();
}