• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * fopen.c
3  */
4 
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 
fopen(const char * file,const char * mode)9 FILE *fopen(const char *file, const char *mode)
10 {
11     int flags = O_RDONLY;
12     int plus = 0;
13     int fd;
14 
15     while (*mode) {
16 	switch (*mode) {
17 	case 'r':
18 	    flags = O_RDONLY;
19 	    break;
20 	case 'w':
21 	    flags = O_WRONLY | O_CREAT | O_TRUNC;
22 	    break;
23 	case 'a':
24 	    flags = O_WRONLY | O_CREAT | O_APPEND;
25 	    break;
26 	case '+':
27 	    plus = 1;
28 	    break;
29 	}
30 	mode++;
31     }
32 
33     if (plus) {
34 	flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
35     }
36 
37     fd = open(file, flags, 0666);
38 
39     if (fd < 0)
40 	return NULL;
41     else
42 	return fdopen(fd, mode);
43 }
44