]>
git.proxmox.com Git - wasi-libc.git/blob - libc-top-half/musl/src/stdio/freopen.c
1 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall, dup
2 #include "stdio_impl.h"
8 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall, dup
10 // Move this below fcntl.h and unistd.h so that the __syscall macro doesn't
12 #include "stdio_impl.h"
15 /* The basic idea of this implementation is to open a new FILE,
16 * hack the necessary parts of the new FILE into the old one, then
17 * close the new FILE. */
19 /* Locking IS necessary because another thread may provably hold the
20 * lock, via flockfile or otherwise, when freopen is called, and in that
21 * case, freopen cannot act until the lock is released. */
23 FILE *freopen(const char *restrict filename
, const char *restrict mode
, FILE *restrict f
)
25 int fl
= __fmodeflags(mode
);
34 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall
35 __syscall(SYS_fcntl
, f
->fd
, F_SETFD
, FD_CLOEXEC
);
37 fcntl(f
->fd
, F_SETFD
, FD_CLOEXEC
);
39 fl
&= ~(O_CREAT
|O_EXCL
|O_CLOEXEC
);
40 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall
41 if (syscall(SYS_fcntl
, f
->fd
, F_SETFL
, fl
) < 0)
43 if (fcntl(f
->fd
, F_SETFL
, fl
) < 0)
47 f2
= fopen(filename
, mode
);
49 if (f2
->fd
== f
->fd
) f2
->fd
= -1; /* avoid closing in fclose */
50 #ifdef __wasilibc_unmodified_upstream // WASI has no dup
51 else if (__dup3(f2
->fd
, f
->fd
, fl
&O_CLOEXEC
)<0) goto fail2
;
53 // WASI doesn't have dup3, but does have a way to renumber
54 // an existing file descriptor.
56 if (__wasilibc_fd_renumber(f2
->fd
, f
->fd
)<0) goto fail2
;
57 f2
->fd
= -1; /* avoid closing in fclose */
61 f
->flags
= (f
->flags
& F_PERM
) | f2
->flags
;
80 weak_alias(freopen
, freopen64
);