blob: 1f15820bfcedf08ce13eeb011cf4e6b56a6194f4 (
plain)
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
|
#include <io.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libc.h>
__weak void __stdio_cleanup(void)
{
}
FILE *fopen(const char *restrict pathname, const char *restrict mode)
{
int fd, flags, _mode;
FILE *fp;
_mode = 0;
if (mode[0] == 'r') {
flags = O_RDONLY;
} else if (mode[0] == 'w') {
flags = O_WRONLY | O_CREAT | O_TRUNC;
} else if (mode[0] == 'a') {
flags = O_WRONLY | O_CREAT | O_APPEND;
_mode = 0666;
} else {
errno = EINVAL;
return NULL;
}
if (strchr(mode, '+')) {
flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
}
if ((fd = open(pathname, flags, _mode)) < 0)
return NULL;
if ((fp = calloc(1, sizeof(FILE))) == NULL)
return NULL;
fp->fd = fd;
fp->buf_size = BUFSIZ;
fp->flags = flags;
fp->type = _IOLBF;
atomic_flag_clear(&fp->lock);
if ((fp->buf = malloc(BUFSIZ)) == NULL) {
close(fd);
free(fp);
return NULL;
}
__libc_fadd(fp);
return fp;
}
|