1 #include "stdio_impl.h" 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <string.h> 5 #include <errno.h> 6 #include <limits.h> 7 fopen(const char * restrict filename,const char * restrict mode)8FILE *fopen(const char *restrict filename, const char *restrict mode) 9 { 10 FILE *f; 11 int fd; 12 int flags; 13 14 /* Check for valid initial mode character */ 15 if ((mode == NULL) || (!strchr("rwa", *mode))) { 16 errno = EINVAL; 17 return 0; 18 } 19 20 /* Compute the flags to pass to open() */ 21 flags = __fmodeflags(mode); 22 23 fd = open(filename, flags, 0666); 24 if (fd < 0) return 0; 25 26 #if !defined(__LP64__) 27 if (fd > SHRT_MAX) { 28 errno = EMFILE; 29 return 0; 30 } 31 #endif 32 33 f = __fdopen(fd, mode); 34 if (f) return f; 35 36 close(fd); 37 return 0; 38 } 39 40 weak_alias(fopen, fopen64); 41