forked from lowleveltv/raspberry-pi-baremetal-c
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyscalls.c
More file actions
91 lines (54 loc) · 1.54 KB
/
syscalls.c
File metadata and controls
91 lines (54 loc) · 1.54 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
#include <stdio.h>
#include <stdlib.h>
#define FOPEN_SIGNAL '0'
#define NEXT_ARG '1'
struct FILE {
};
void sendByteToGM(char byte) {
printf("%c", byte);
}
void sendStringToGM(char* s) {
printf("%s", s);
}
void awaitSignalFromGM(char b) {
//hang until GM sends b signal
char buffer[2];
fgets(buffer, 2, stdin);
if (buffer[0] == b)
//GM properly sent the signal
return;
//GM failed to process the syscall
exit(1);
}
//read numBytes bytes from GM into a buffer and return the pointer to the buffer
char* getStreamFromGM(uint numBytes) {
char *buffer = malloc(numBytes * sizeof(char)); // array of 10 ints
fgets(buffer, numBytes, stdin);
return buffer;
}
FILE* fopen(const char *filename, const char *mode) {
//send the fopen signal to GM
sendByteToGM(FOPEN_SIGNAL);
//wait for GM to request the next argument to fopen
awaitSignalFromGM(NEXT_ARG);
//send filename to GM
sendStringToGM(filename);
char* buffer[8];
sendByteToGM(mode);
return getStreamFromGM(8);
}
int fclose(FILE *stream) {
//send the fclose signal to GM
}
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
//send the fwrite signal to GM, wait for the confirmation signal from GM, then send the data to GM
}
size_t fread(void *ptr, size_t size, size_t count, FILE *stream) {
//send the fread signal to GM, read the data incoming from GM
}
int fseek(FILE *stream, long offset, int whence) {
}
long ftell(FILE *stream) {
}
int main() {
}