| /* The basic idea of this implementation is to open a new FILE, |
| * hack the necessary parts of the new FILE into the old one, then |
| /* Locking IS necessary because another thread may provably hold the |
| * lock, via flockfile or otherwise, when freopen is called, and in that |
| * case, freopen cannot act until the lock is released. */ |
| FILE *freopen(const char *restrict filename, const char *restrict mode, FILE *restrict f) |
| int fl = __fmodeflags(mode); |
| __syscall(SYS_fcntl, f->fd, F_SETFD, FD_CLOEXEC); |
| fl &= ~(O_CREAT|O_EXCL|O_CLOEXEC); |
| if (syscall(SYS_fcntl, f->fd, F_SETFL, fl) < 0) |
| f2 = fopen(filename, mode); |
| if (f2->fd == f->fd) f2->fd = -1; /* avoid closing in fclose */ |
| else if (__dup3(f2->fd, f->fd, fl&O_CLOEXEC)<0) goto fail2; |
| f->flags = (f->flags & F_PERM) | f2->flags; |