1#include "stdio_impl.h" 2#include <fcntl.h> 3#include <string.h> 4#include <errno.h> 5 6FILE *fopen(const char *restrict filename, const char *restrict mode) 7{ 8 FILE *f = NULL; 9 int fd = -1; 10 int file_flags = 0; 11 int mode_flags = 0; 12 13 /* Compute the flags to pass to open() */ 14 mode_flags = __fmodeflags(mode, &file_flags); 15 if (mode_flags < 0) { 16 return NULL; 17 } 18 19 fd = sys_open(filename, mode_flags, 0666); 20 if (fd < 0) return 0; 21 if (mode_flags & O_CLOEXEC) 22 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC); 23 24 f = __fdopenx(fd, file_flags); 25 if (f) { 26 return f; 27 } 28 29 __syscall(SYS_close, fd); 30 return 0; 31} 32 33weak_alias(fopen, fopen64); 34