-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_fopen.c
53 lines (52 loc) · 1.23 KB
/
_fopen.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
#include "_stdio.h"
#include "_fcntl.h"
FILE *_fopen(const char *filename, const char *mode, FILE *fp) {
int oflag;
int oprot = 0666;
switch (*mode++) {
case 'r':
fp->_flag = __IOREAD;
break;
case 'w':
fp->_flag = __IOWRT;
break;
case 'a':
fp->_flag = __IOAPPD;
break;
default:
return NULL;
}
while (*mode != '\0') {
switch (*mode++) {
case '+':
fp->_flag |= __IOUPDT;
break;
case 'b':
fp->_flag |= __IOBIN;
break;
default:
return NULL;
}
}
if (fp->_flag & __IOUPDT)
oflag = O_RDWR;
else if (fp->_flag & __IOREAD)
oflag = O_RDONLY;
else
oflag = O_WRONLY;
if (fp->_flag & __IOWRT)
oflag |= O_TRUNC;
if (fp->_flag & __IOAPPD)
oflag |= O_APPEND;
if (fp->_flag & (__IOWRT | __IOAPPD))
oflag |= O_CREAT;
if (fp->_flag & __IOBIN)
oflag |= O_BINARY;
fp->_file = open(filename, oflag, oprot);
if (fp->_file < 0) {
fp->_flag = 0;
_freefile(fp);
return NULL;
}
return fp;
}