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