chiark / gitweb /
[PATCH] add prototype for ftruncate to klibc
[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   int fd;
17
18   while ( *mode ) {
19     switch ( *mode ) {
20     case 'r':
21       flags = O_RDONLY;
22       break;
23     case 'w':
24       flags = O_WRONLY|O_CREAT|O_TRUNC;
25       break;
26     case 'a':
27       flags = O_WRONLY|O_CREAT|O_APPEND;
28       break;
29     case '+':
30       plus = 1;
31       break;
32     }
33     mode++;
34   }
35
36   if ( plus ) {
37     flags = (flags & ~(O_RDONLY|O_WRONLY)) | O_RDWR;
38   }
39
40   fd = open(file, flags, 0666);
41
42   if ( fd < 0 )
43     return NULL;
44   else
45     return fdopen(fd, mode);
46 }