chiark / gitweb /
[PATCH] update klibc to version 0.181
[elogind.git] / klibc / klibc / fopen.c
1 /*
2  * fopen.c
3  */
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 /* This depends on O_RDONLY == 0, O_WRONLY == 1, O_RDWR == 2 */
10
11
12 FILE *fopen(const char *file, const char *mode)
13 {
14   int flags = O_RDONLY;
15   int plus = 0;
16
17   while ( *mode ) {
18     switch ( *mode++ ) {
19     case 'r':
20       flags = O_RDONLY;
21       break;
22     case 'w':
23       flags = O_WRONLY|O_CREAT|O_TRUNC;
24       break;
25     case 'a':
26       flags = O_WRONLY|O_CREAT|O_APPEND;
27       break;
28     case '+':
29       plus = 1;
30       break;
31     }
32   }
33
34   if ( plus ) {
35     flags = (flags & ~(O_RDONLY|O_WRONLY)) | O_RDWR;
36   }
37
38   /* Note: __create_file(-1) == NULL, so this is safe */
39   return __create_file(open(file, flags, 0666));
40 }