remove flush hook cruft that was never used from stdio
[musl] / src / stdio / freopen.c
1 #include "stdio_impl.h"
2
3 /* The basic idea of this implementation is to open a new FILE,
4  * hack the necessary parts of the new FILE into the old one, then
5  * close the new FILE. */
6
7 /* Locking is not necessary because, in the event of failure, the stream
8  * passed to freopen is invalid as soon as freopen is called. */
9
10 FILE *freopen(const char *filename, const char *mode, FILE *f)
11 {
12         int fl;
13         FILE *f2;
14
15         fflush(f);
16
17         if (!filename) {
18                 f2 = fopen("/dev/null", mode);
19                 if (!f2) goto fail;
20                 fl = syscall(SYS_fcntl, f2->fd, F_GETFL, 0);
21                 if (fl < 0 || syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0)
22                         goto fail2;
23         } else {
24                 f2 = fopen(filename, mode);
25                 if (!f2) goto fail;
26                 if (syscall(SYS_dup2, f2->fd, f->fd) < 0)
27                         goto fail2;
28         }
29
30         f->flags = (f->flags & F_PERM) | f2->flags;
31         f->read = f2->read;
32         f->write = f2->write;
33         f->seek = f2->seek;
34         f->close = f2->close;
35
36         fclose(f2);
37         return f;
38
39 fail2:
40         fclose(f2);
41 fail:
42         fclose(f);
43         return NULL;
44 }
45
46 LFS64(freopen);