2011-02-12 00:22:29 -05:00
|
|
|
#include "stdio_impl.h"
|
|
|
|
|
2012-09-06 22:44:55 -04:00
|
|
|
FILE *fopen(const char *restrict filename, const char *restrict mode)
|
2011-02-12 00:22:29 -05:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int fd;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* Check for valid initial mode character */
|
|
|
|
if (!strchr("rwa", *mode)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the flags to pass to open() */
|
2012-08-25 23:15:13 -04:00
|
|
|
if (strchr(mode, '+')) flags = O_RDWR;
|
2011-02-12 00:22:29 -05:00
|
|
|
else if (*mode == 'r') flags = O_RDONLY;
|
|
|
|
else flags = O_WRONLY;
|
2012-08-25 23:15:13 -04:00
|
|
|
if (strchr(mode, 'x')) flags |= O_EXCL;
|
2011-02-12 00:22:29 -05:00
|
|
|
if (*mode != 'r') flags |= O_CREAT;
|
|
|
|
if (*mode == 'w') flags |= O_TRUNC;
|
|
|
|
if (*mode == 'a') flags |= O_APPEND;
|
|
|
|
|
2012-02-02 00:11:29 -05:00
|
|
|
fd = syscall_cp(SYS_open, filename, flags|O_LARGEFILE, 0666);
|
2011-02-12 00:22:29 -05:00
|
|
|
if (fd < 0) return 0;
|
|
|
|
|
|
|
|
f = __fdopen(fd, mode);
|
|
|
|
if (f) return f;
|
|
|
|
|
2011-04-17 16:32:15 -04:00
|
|
|
__syscall(SYS_close, fd);
|
2011-02-12 00:22:29 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LFS64(fopen);
|