-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbootstrap.c
More file actions
275 lines (231 loc) · 4.42 KB
/
bootstrap.c
File metadata and controls
275 lines (231 loc) · 4.42 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* Copyright (C) 2016 Jeremiah Orians
* Copyright (C) 2020 deesix <deesix@tuta.io>
* This file is part of M2-Planet.
*
* M2-Planet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* M2-Planet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
*/
enum
{
stdin = 0,
stdout = 1,
stderr = 2,
};
enum
{
EOF = 0xFFFFFFFF,
NULL = 0,
};
enum
{
EXIT_FAILURE = 1,
EXIT_SUCCESS = 0,
};
enum
{
TRUE = 1,
FALSE = 0,
};
void* malloc(int size);
char* __fputc_buffer;
int fgetc(FILE* f)
{
/* We don't have operator & */
if(__fputc_buffer == NULL) {
__fputc_buffer = malloc(1);
}
if(read(f, __fputc_buffer, 1) <= 0) {
return EOF;
}
return __fputc_buffer[0];
}
unsigned fread(char* buffer, unsigned size, unsigned count, FILE* f) {
return read(f, buffer, size * count);
}
void fputc(char s, FILE* f)
{
/* We don't have operator & */
if(__fputc_buffer == NULL) {
__fputc_buffer = malloc(1);
}
__fputc_buffer[0] = s;
write(f, __fputc_buffer, 1);
}
unsigned fwrite(char* buffer, unsigned size, unsigned count, FILE* f) {
if(size == 0 || count == 0) {
return 0;
}
return write(f, buffer, size * count);
}
FILE* fopen(char* filename, char* mode)
{
FILE* f;
if('w' == mode[0])
{ /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
f = open(filename, 577 , 384);
}
else
{ /* Everything else is a read */
f = open(filename, 0, 0);
}
/* Negative numbers are error codes */
if(0 > f)
{
return 0;
}
return f;
}
int fclose(FILE* stream)
{
int error = close(stream);
return error;
}
long _malloc_ptr;
long _brk_ptr;
void* malloc(int size)
{
size = (size + sizeof(char*) - 1) & ~ (sizeof(char*) - 1);
if(NULL == _brk_ptr)
{
_brk_ptr = brk(0);
_malloc_ptr = _brk_ptr;
}
if(_brk_ptr < _malloc_ptr + size)
{
long old_brk = _brk_ptr;
_brk_ptr = brk(_malloc_ptr + size);
if(-4 /* EINTR */ == _brk_ptr || _brk_ptr == old_brk || old_brk + size < _brk_ptr) return 0;
}
long old_malloc = _malloc_ptr;
_malloc_ptr = _malloc_ptr + size;
return old_malloc;
}
int strlen(char* str )
{
int i = 0;
while(0 != str[i]) i = i + 1;
return i;
}
void fputs(char* s, FILE* f)
{
write(f, s, strlen(s));
}
void* memset(void* ptr, int value, int num)
{
char* s;
for(s = ptr; 0 < num; num = num - 1)
{
s[0] = value;
s = s + 1;
}
}
/* The real memcpy has void* parameters and return types, but
* for M2-Planet it doesn't matter and it leads to slightly better
* codegen if they're char* from the start. */
char* memcpy(char* dst, char* src, unsigned count)
{
if(NULL == dst) return dst;
if(NULL == src) return NULL;
unsigned i = 0;
while(i < count)
{
dst[i] = src[i];
i = i + 1;
}
return dst;
}
int strcmp(char* lhs, char* rhs)
{
int i = 0;
while(0 != lhs[i])
{
if(lhs[i] != rhs[i]) return lhs[i] - rhs[i];
i = i + 1;
}
return lhs[i] - rhs[i];
}
char* strchr(char* str, int ch)
{
char* p = str;
while(ch != p[0])
{
if(0 == p[0]) return NULL;
p = p + 1;
}
if(0 == p[0]) return NULL;
return p;
}
int isspace(int c) {
return c == ' ' || c == '\n' || c == '\t' || c == '\r';
}
int isdigit(int c) {
return c >= '0' && c <= '9';
}
int isalpha(int c) {
c = c | 32; /* lowercase the char */
return c >= 'a' && c <= 'z';
}
int isalnum(int c) {
return isalpha(c) || isdigit(c);
}
int atoi(char* str)
{
while(isspace(str[0]))
{
str = str + 1;
}
int negative = 0;
if (str[0] == '-') {
negative = 1;
}
if (str[0] == '+') {
str = str + 1;
}
int value = 0;
while(isdigit(str[0]))
{
value = 10 * value - (str[0] - '0');
str = str + 1;
}
if(negative == 1)
{
return value;
}
else
{
return -value;
}
}
void* calloc(int count, int size)
{
void* ret = malloc(count * size);
if(NULL == ret) return NULL;
memset(ret, 0, (count * size));
return ret;
}
void assert(int condition) {
if(!condition) {
fputs("M2libc assertion failed\n", stderr);
exit(EXIT_FAILURE);
}
}
void free(void* l)
{
return;
}
int abs(int n) {
if (n < 0) {
return -n;
}
return n;
}