clean up stdio_impl.h
[musl] / src / stdio / freopen.c
1 #include "stdio_impl.h"
2 #include <fcntl.h>
3
4 /* The basic idea of this implementation is to open a new FILE,
5  * hack the necessary parts of the new FILE into the old one, then
6  * close the new FILE. */
7
8 /* Locking IS necessary because another thread may provably hold the
9  * lock, via flockfile or otherwise, when freopen is called, and in that
10  * case, freopen cannot act until the lock is released. */
11
12 int __dup3(int, int, int);
13
14 FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f)
15 {
16         int fl = __fmodeflags(mode);
17         FILE *f2;
18
19         FLOCK(f);
20
21         fflush(f);
22
23         if (!filename) {
24                 if (fl&O_CLOEXEC)
25                         __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC);
26                 fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC);
27                 if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0)
28                         goto fail;
29         } else {
30                 f2 = fopen(filename, mode);
31                 if (!f2) goto fail;
32                 if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */
33                 else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2;
34
35                 f->flags = (f->flags & F_PERM) | f2->flags;
36                 f->read = f2->read;
37                 f->write = f2->write;
38                 f->seek = f2->seek;
39                 f->close = f2->close;
40
41                 fclose(f2);
42         }
43
44         FUNLOCK(f);
45         return f;
46
47 fail2:
48         fclose(f2);
49 fail:
50         fclose(f);
51         return NULL;
52 }
53
54 LFS64(freopen);