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
|
#include "file.h"
#include <camellia/syscalls.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static FILE _stdin_null = { .fd = 0 };
static FILE _stdout_null = { .fd = 1 };
static FILE _stderr_null = { .fd = 2 };
FILE *const stdin = &_stdin_null;
FILE *const stdout = &_stdout_null;
FILE *const stderr = &_stderr_null;
FILE *fopen(const char *path, const char *mode) {
FILE *f;
handle_t h;
int flags = 0;
if (path && path[0] == '!') {
/* special handling for "!files" */
path++;
if (!strcmp(path, "stdin")) return file_clone(stdin, mode);
if (!strcmp(path, "stdout")) return file_clone(stdout, mode);
if (!strcmp(path, "stderr")) return file_clone(stderr, mode);
errno = -1;
return NULL;
} else {
if (mode[0] == 'w' || mode[0] == 'a')
flags |= OPEN_CREATE;
h = _syscall_open(path, strlen(path), flags);
if (h < 0) {
errno = -h;
return NULL;
}
if (mode[0] == 'w')
_syscall_write(h, NULL, 0, 0, WRITE_TRUNCATE);
f = fdopen(h, mode);
if (!f) close(h);
return f;
}
}
FILE *freopen(const char *path, const char *mode, FILE *f) {
/* partially based on the musl implementation of freopen */
FILE *f2;
if (!path) goto fail;
f2 = fopen(path, mode);
if (!f2) goto fail;
if (f->fd == f2->fd) {
f2->fd = -1;
} else {
if (_syscall_dup(f2->fd, f->fd, 0) < 0) goto fail2;
}
f->pos = f2->pos;
f->eof = f2->eof;
fclose(f2);
return f;
fail2:
fclose(f2);
fail:
fclose(f);
return NULL;
}
FILE *fdopen(int fd, const char *mode) {
FILE *f;
f = malloc(sizeof *f);
if (!f) return NULL;
f->pos = mode[0] == 'a' ? -1 : 0;
f->eof = false;
f->fd = fd;
f->error = false;
return f;
}
FILE *file_clone(const FILE *f, const char *mode) {
handle_t h = _syscall_dup(f->fd, -1, 0);
FILE *f2;
if (h < 0) return NULL;
f2 = fdopen(h, mode);
if (!f2) {
close(h);
return NULL;
}
f2->pos = f->pos;
f2->eof = f->eof;
f2->fd = h;
return f2;
}
static void fadvance(long amt, FILE *f) {
bool pos_neg = f->pos < 0;
f->pos += amt;
if (pos_neg && f->pos >= 0)
f->pos = -1;
}
size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict f) {
size_t total = size*nitems, pos = 0;
unsigned char *buf = ptr;
if (f->fd < 0) {
errno = EBADF;
return 0;
}
if (size == 0)
return 0;
while (pos < total) {
long res = _syscall_read(f->fd, buf + pos, total - pos, f->pos);
if (res < 0) {
f->error = true;
errno = -res;
return pos/size;
} else if (res == 0) {
f->eof = true;
return pos/size;
} else {
pos += res;
fadvance(res, f);
}
}
return nitems;
}
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict f) {
size_t total = size*nitems, pos = 0;
const unsigned char *buf = ptr;
if (f->fd < 0) {
errno = EBADF;
return 0;
}
if (size == 0)
return 0;
while (pos < total) {
long res = _syscall_write(f->fd, buf + pos, total - pos, f->pos, 0);
if (res < 0) {
f->error = true;
errno = -res;
return pos/size;
} else if (res == 0) {
f->eof = true;
return pos/size;
} else {
pos += res;
fadvance(res, f);
}
}
return nitems;
}
char *fgets(char *buf, int size, FILE *f) {
char c = '\0';
long pos = 0;
while (pos < (size-1) && c != '\n' && fread(&c, 1, 1, f))
buf[pos++] = c;
buf[pos++] = '\0';
if (f->eof && pos == 1) return NULL;
if (f->error) return NULL;
return buf;
}
int fseek(FILE *f, long offset, int whence) {
if (fflush(f))
return -1;
switch (whence) {
case SEEK_SET:
f->pos = 0;
break;
case SEEK_CUR:
break;
case SEEK_END:
f->pos = -1;
break;
default:
errno = EINVAL;
return -1;
}
bool pos_neg = f->pos < 0;
f->pos += offset;
if (pos_neg && f->pos >= 0) {
// TODO getting the file size
errno = ENOSYS;
return -1;
}
f->eof = false;
return 0;
}
int fclose(FILE *f) {
fflush(f);
if (f->fd > 0) close(f->fd);
if (f != &_stdin_null && f != &_stdout_null && f != &_stderr_null)
free(f);
return 0;
}
int fflush(FILE *f) {
(void)f;
return 0;
}
int feof(FILE *f) {
return f->eof;
}
int ferror(FILE *f) {
return f->error;
}
|