This repository has been archived by the owner on Jul 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
metal_newlib_syscalls.c
307 lines (267 loc) · 7.47 KB
/
metal_newlib_syscalls.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* @file metal_syscalls.c
* @date 19.07.2016
* @author Klemens D. Morgenstern
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef enum metal_func_t
{
metal_func_chown = 0,
metal_func_close,
metal_func_fstat,
metal_func_isatty,
metal_func_link,
metal_func_lseek,
metal_func_open,
metal_func_read,
metal_func_stat,
metal_func_sysmlink,
metal_func_unlink,
metal_func_write
} metal_func;
int metal_func_stub(metal_func func_type __attribute__((unused)),
const char* arg1 __attribute__((unused)),
const char* arg2 __attribute__((unused)),
int arg3 __attribute__((unused)),
int arg4 __attribute__((unused)),
int arg5 __attribute__((unused)),
char* arg6 __attribute__((unused)),
struct stat* arg7 __attribute__((unused)))
{
return -1;
}
int _open (char* file, int flags, int mode);
int _read (int file, char* ptr, int len);
int _write(int file, char* ptr, int len);
int _lseek(int file, int ptr, int dir);
int _close(int);
int _fstat(int fildes, struct stat* st)
{
return metal_func_stub(metal_func_fstat,
0, // const char* arg1,
0, // const char* arg2
fildes, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
st // struct stat* arg7
);
}
int _isatty(int file)
{
return metal_func_stub(metal_func_isatty,
0, // const char* arg1,
0, // const char* arg2
file, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
0 // struct stat* arg7
);
}
int _link(char* existing, char* _new)
{
return metal_func_stub(metal_func_link,
existing, // const char* arg1,
_new, // const char* arg2
0, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
0 // struct stat* arg7
);
}
#ifndef METAL_NEWLIB_BUFFER_SIZE
#define METAL_NEWLIB_BUFFER_SIZE 0x400
#endif
//write buffer
static int write_fd = -1;
static int write_pos = 0;
static char write_buf[METAL_NEWLIB_BUFFER_SIZE];
static void flush_write()
{
if (write_pos > 0)
{
_write(write_fd, write_buf, write_pos);
write_fd = -1;
write_pos = 0;
}
}
static int _buffered_write(int file, char* ptr, int len __attribute__((unused)))
{
if ((file != write_fd) || (write_pos == METAL_NEWLIB_BUFFER_SIZE))
{
flush_write();
write_fd = file;
}
write_buf[write_pos++] = *ptr;
if ((*ptr == '\n')) //flush on newline, but put it into the buffer first.
{
flush_write();
write_fd = file;
}
return 1;
}
//read buffer
static int read_fd = -1;
static int read_pos = 0;
static int read_end = 0;
static char read_buf[METAL_NEWLIB_BUFFER_SIZE];
void read_clear()
{
read_pos = 0;
read_end = 0;
read_fd = -1;
}
void _read_init_buffer(int fd)
{
read_fd = fd;
read_pos = 0;
read_end = metal_func_stub(metal_func_read,
0, // const char* arg1,
0, // const char* arg2
read_fd, // int arg3
METAL_NEWLIB_BUFFER_SIZE, // int arg4
0, // int arg5
read_buf, // char* arg6
0 // struct stat* arg7
);
}
int _read_buffered(char* ptr, int len)
{
if( (read_pos == read_end) && (read_end == METAL_NEWLIB_BUFFER_SIZE))
{
read_pos = 0;
read_end = metal_func_stub(metal_func_read,
0, // const char* arg1,
0, // const char* arg2
read_fd, // int arg3
METAL_NEWLIB_BUFFER_SIZE, // int arg4
0, // int arg5
read_buf, // char* arg6
0 // struct stat* arg7
);
}
//read what's available
int i = 0;
for (; ((read_pos + i) < read_end) && (i<len); i++) //read current buffer
ptr[i] = read_buf[read_pos+i];
read_pos += i;
return i;
}
int _open(char* file, int flags, int mode)
{
return metal_func_stub(metal_func_open,
file, // const char* arg1
0, // const char* arg2
flags, // int arg3
mode, // int arg4
0, // int arg5
0, // char* arg6
0 // struct stat* arg7
);
}
int _close(int fildes)
{
flush_write();
if (read_fd == fildes)
read_clear();
return metal_func_stub(metal_func_close,
0, // const char* arg1,
0, // const char* arg2
fildes, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
0 // struct stat* ar7B
);
}
int _lseek(int file, int ptr, int dir)
{
return metal_func_stub(metal_func_lseek,
0, // const char* arg1,
0, // const char* arg2
file, // int arg3
ptr, // int arg4
dir, //int arg5
0, // char * arg6
0 // struct stat* arg7
);
}
int _read(int file, char* ptr, int len)
{
if ((read_fd != -1) && (file == read_fd))
return _read_buffered(ptr, len);
else if ((read_fd == -1) && (len>0))
{
_read_init_buffer(file);
return _read_buffered(ptr, len);
}
else
return metal_func_stub(metal_func_read,
0, // const char* arg1,
0, // const char* arg2
file, // int arg3
len, // int arg4
0, // int arg5
ptr, // char* arg6
0 // struct stat* arg7
);
}
int _write(int file, char* ptr, int len)
{
if (len == 1)
return _buffered_write(file, ptr, len);
else
{
return metal_func_stub(metal_func_write,
0, // const char* arg1
0, // const char* arg2
file, // int arg3
len, // int arg4
0, // int arg5
ptr, // char* arg6
0 // struct stat* arg7
);
}
}
int _stat(const char* file, struct stat* st)
{
return metal_func_stub(metal_func_stat,
file, // const char* arg1
0, // const char* arg2
0, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
st // struct stat* arg7
);
}
int _symlink(const char* path1, const char* path2)
{
return metal_func_stub(metal_func_sysmlink,
path1, // const char* arg1,
path2, // const char* arg2
0, // int arg3
0, // int arg4
0, // int arg5
0, // char* arg6
0 // struct stat* arg7
);
}
int _unlink(char* name)
{
return metal_func_stub(metal_func_unlink,
0, // const char* arg1
0, // const char* arg2
0, // int arg3
0, // int arg4
0, // int arg5
name, // char* arg6
0 // struct stat* arg7
);
}