158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1990, 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes * Chris Torek.
958f0484fSRodney W. Grimes *
1058f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes * are met:
1358f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
181d8053c5SEd Maste * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes * without specific prior written permission.
2158f0484fSRodney W. Grimes *
2258f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes * SUCH DAMAGE.
3358f0484fSRodney W. Grimes */
3458f0484fSRodney W. Grimes
35d201fe46SDaniel Eischen #include "namespace.h"
3658f0484fSRodney W. Grimes #include <sys/types.h>
3758f0484fSRodney W. Grimes #include <sys/stat.h>
3858f0484fSRodney W. Grimes #include <fcntl.h>
3958f0484fSRodney W. Grimes #include <errno.h>
40fc9ab4f6SJohn Baldwin #include <limits.h>
4158f0484fSRodney W. Grimes #include <unistd.h>
4258f0484fSRodney W. Grimes #include <stdio.h>
4358f0484fSRodney W. Grimes #include <stdlib.h>
44d201fe46SDaniel Eischen #include "un-namespace.h"
45d201fe46SDaniel Eischen #include "libc_private.h"
4658f0484fSRodney W. Grimes #include "local.h"
4758f0484fSRodney W. Grimes
4858f0484fSRodney W. Grimes /*
4958f0484fSRodney W. Grimes * Re-direct an existing, open (probably) file to some other file.
5058f0484fSRodney W. Grimes * ANSI is written such that the original file gets closed if at
5158f0484fSRodney W. Grimes * all possible, no matter what.
5258f0484fSRodney W. Grimes */
5358f0484fSRodney W. Grimes FILE *
freopen(const char * __restrict file,const char * __restrict mode,FILE * __restrict fp)545a6307cfSEd Maste freopen(const char * __restrict file, const char * __restrict mode,
555a6307cfSEd Maste FILE * __restrict fp)
5658f0484fSRodney W. Grimes {
5729ac6bd2SDaniel Eischen int f;
58c2974987STim J. Robbins int dflags, flags, isopen, oflags, sverrno, wantfd;
5958f0484fSRodney W. Grimes
6058f0484fSRodney W. Grimes if ((flags = __sflags(mode, &oflags)) == 0) {
615a8d08f5SAndrey A. Chernov sverrno = errno;
6258f0484fSRodney W. Grimes (void) fclose(fp);
635a8d08f5SAndrey A. Chernov errno = sverrno;
6458f0484fSRodney W. Grimes return (NULL);
656a2a9844SPedro F. Giffuni }
6658f0484fSRodney W. Grimes
67fda0a14fSKonstantin Belousov FLOCKFILE_CANCELSAFE(fp);
6832fc781eSDmitrij Tejblum
6958f0484fSRodney W. Grimes if (!__sdidinit)
7058f0484fSRodney W. Grimes __sinit();
7158f0484fSRodney W. Grimes
7258f0484fSRodney W. Grimes /*
73c2974987STim J. Robbins * If the filename is a NULL pointer, the caller is asking us to
74c2974987STim J. Robbins * re-open the same file with a different mode. We allow this only
75c2974987STim J. Robbins * if the modes are compatible.
76c2974987STim J. Robbins */
77c2974987STim J. Robbins if (file == NULL) {
78c2974987STim J. Robbins /* See comment below regarding freopen() of closed files. */
79c2974987STim J. Robbins if (fp->_flags == 0) {
80c2974987STim J. Robbins errno = EINVAL;
81fda0a14fSKonstantin Belousov fp = NULL;
82fda0a14fSKonstantin Belousov goto end;
83c2974987STim J. Robbins }
84c2974987STim J. Robbins if ((dflags = _fcntl(fp->_file, F_GETFL)) < 0) {
85c2974987STim J. Robbins sverrno = errno;
86c2974987STim J. Robbins fclose(fp);
87c2974987STim J. Robbins errno = sverrno;
88fda0a14fSKonstantin Belousov fp = NULL;
89fda0a14fSKonstantin Belousov goto end;
90c2974987STim J. Robbins }
9193a65e1bSJilles Tjoelker /* Work around incorrect O_ACCMODE. */
9293a65e1bSJilles Tjoelker if ((dflags & O_ACCMODE) != O_RDWR &&
9393a65e1bSJilles Tjoelker (dflags & (O_ACCMODE | O_EXEC)) != (oflags & O_ACCMODE)) {
94c2974987STim J. Robbins fclose(fp);
95120d6dd5SPedro F. Giffuni errno = EBADF;
96fda0a14fSKonstantin Belousov fp = NULL;
97fda0a14fSKonstantin Belousov goto end;
98c2974987STim J. Robbins }
99b0bf75c8SAndrey A. Chernov if (fp->_flags & __SWR)
100b0bf75c8SAndrey A. Chernov (void) __sflush(fp);
101c2974987STim J. Robbins if ((oflags ^ dflags) & O_APPEND) {
102c2974987STim J. Robbins dflags &= ~O_APPEND;
103c2974987STim J. Robbins dflags |= oflags & O_APPEND;
104c2974987STim J. Robbins if (_fcntl(fp->_file, F_SETFL, dflags) < 0) {
105c2974987STim J. Robbins sverrno = errno;
106c2974987STim J. Robbins fclose(fp);
107c2974987STim J. Robbins errno = sverrno;
108fda0a14fSKonstantin Belousov fp = NULL;
109fda0a14fSKonstantin Belousov goto end;
110c2974987STim J. Robbins }
111c2974987STim J. Robbins }
1126f8d08b3SAndrey A. Chernov if (oflags & O_TRUNC)
1136f8d08b3SAndrey A. Chernov (void) ftruncate(fp->_file, (off_t)0);
114b0bf75c8SAndrey A. Chernov if (!(oflags & O_APPEND))
115b0bf75c8SAndrey A. Chernov (void) _sseek(fp, (fpos_t)0, SEEK_SET);
1165ba651f0SJilles Tjoelker if (oflags & O_CLOEXEC)
1175ba651f0SJilles Tjoelker (void) _fcntl(fp->_file, F_SETFD, FD_CLOEXEC);
118c2974987STim J. Robbins f = fp->_file;
119c2974987STim J. Robbins isopen = 0;
120c2974987STim J. Robbins wantfd = -1;
121c2974987STim J. Robbins goto finish;
122c2974987STim J. Robbins }
123c2974987STim J. Robbins
124c2974987STim J. Robbins /*
12558f0484fSRodney W. Grimes * There are actually programs that depend on being able to "freopen"
12658f0484fSRodney W. Grimes * descriptors that weren't originally open. Keep this from breaking.
12758f0484fSRodney W. Grimes * Remember whether the stream was open to begin with, and which file
12858f0484fSRodney W. Grimes * descriptor (if any) was associated with it. If it was attached to
12958f0484fSRodney W. Grimes * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
13058f0484fSRodney W. Grimes * should work. This is unnecessary if it was not a Unix file.
13158f0484fSRodney W. Grimes */
13258f0484fSRodney W. Grimes if (fp->_flags == 0) {
13358f0484fSRodney W. Grimes fp->_flags = __SEOF; /* hold on to it */
13458f0484fSRodney W. Grimes isopen = 0;
13558f0484fSRodney W. Grimes wantfd = -1;
13658f0484fSRodney W. Grimes } else {
13758f0484fSRodney W. Grimes /* flush the stream; ANSI doesn't require this. */
13858f0484fSRodney W. Grimes if (fp->_flags & __SWR)
13958f0484fSRodney W. Grimes (void) __sflush(fp);
14058f0484fSRodney W. Grimes /* if close is NULL, closing is a no-op, hence pointless */
14158f0484fSRodney W. Grimes isopen = fp->_close != NULL;
14258f0484fSRodney W. Grimes if ((wantfd = fp->_file) < 0 && isopen) {
14358f0484fSRodney W. Grimes (void) (*fp->_close)(fp->_cookie);
14458f0484fSRodney W. Grimes isopen = 0;
14558f0484fSRodney W. Grimes }
14658f0484fSRodney W. Grimes }
14758f0484fSRodney W. Grimes
14858f0484fSRodney W. Grimes /* Get a new descriptor to refer to the new file. */
1499233c4d9SJason Evans f = _open(file, oflags, DEFFILEMODE);
1507922e2bbSAndrey A. Chernov /* If out of fd's close the old one and try again. */
1517922e2bbSAndrey A. Chernov if (f < 0 && isopen && wantfd > STDERR_FILENO &&
1527922e2bbSAndrey A. Chernov (errno == ENFILE || errno == EMFILE)) {
1537922e2bbSAndrey A. Chernov (void) (*fp->_close)(fp->_cookie);
1547922e2bbSAndrey A. Chernov isopen = 0;
1557922e2bbSAndrey A. Chernov wantfd = -1;
1567922e2bbSAndrey A. Chernov f = _open(file, oflags, DEFFILEMODE);
1577922e2bbSAndrey A. Chernov }
15858f0484fSRodney W. Grimes sverrno = errno;
15958f0484fSRodney W. Grimes
160c2974987STim J. Robbins finish:
16158f0484fSRodney W. Grimes /*
16258f0484fSRodney W. Grimes * Finish closing fp. Even if the open succeeded above, we cannot
16358f0484fSRodney W. Grimes * keep fp->_base: it may be the wrong size. This loses the effect
16458f0484fSRodney W. Grimes * of any setbuffer calls, but stdio has always done this before.
165cc3d8572SJohn Baldwin *
166cc3d8572SJohn Baldwin * Leave the existing file descriptor open until dup2() is called
167cc3d8572SJohn Baldwin * below to avoid races where a concurrent open() in another thread
168cc3d8572SJohn Baldwin * could claim the existing descriptor.
16958f0484fSRodney W. Grimes */
17058f0484fSRodney W. Grimes if (fp->_flags & __SMBF)
17158f0484fSRodney W. Grimes free((char *)fp->_bf._base);
17258f0484fSRodney W. Grimes fp->_w = 0;
17358f0484fSRodney W. Grimes fp->_r = 0;
17458f0484fSRodney W. Grimes fp->_p = NULL;
17558f0484fSRodney W. Grimes fp->_bf._base = NULL;
17658f0484fSRodney W. Grimes fp->_bf._size = 0;
17758f0484fSRodney W. Grimes fp->_lbfsize = 0;
17858f0484fSRodney W. Grimes if (HASUB(fp))
17958f0484fSRodney W. Grimes FREEUB(fp);
18058f0484fSRodney W. Grimes fp->_ub._size = 0;
18158f0484fSRodney W. Grimes if (HASLB(fp))
18258f0484fSRodney W. Grimes FREELB(fp);
18358f0484fSRodney W. Grimes fp->_lb._size = 0;
1841e98f887SJohn Baldwin fp->_orientation = 0;
1851e98f887SJohn Baldwin memset(&fp->_mbstate, 0, sizeof(mbstate_t));
1868b63538dSAndrey A. Chernov fp->_flags2 = 0;
18758f0484fSRodney W. Grimes
18858f0484fSRodney W. Grimes if (f < 0) { /* did not get it after all */
189cc3d8572SJohn Baldwin if (isopen)
190cc3d8572SJohn Baldwin (void) (*fp->_close)(fp->_cookie);
19158f0484fSRodney W. Grimes fp->_flags = 0; /* set it free */
1925a8d08f5SAndrey A. Chernov errno = sverrno; /* restore in case _close clobbered */
193fda0a14fSKonstantin Belousov fp = NULL;
194fda0a14fSKonstantin Belousov goto end;
19558f0484fSRodney W. Grimes }
19658f0484fSRodney W. Grimes
19758f0484fSRodney W. Grimes /*
19858f0484fSRodney W. Grimes * If reopening something that was open before on a real file, try
19958f0484fSRodney W. Grimes * to maintain the descriptor. Various C library routines (perror)
20058f0484fSRodney W. Grimes * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
20158f0484fSRodney W. Grimes */
202cc3d8572SJohn Baldwin if (wantfd >= 0) {
2035ba651f0SJilles Tjoelker if ((oflags & O_CLOEXEC ? _fcntl(f, F_DUP2FD_CLOEXEC, wantfd) :
2045ba651f0SJilles Tjoelker _dup2(f, wantfd)) >= 0) {
2059233c4d9SJason Evans (void)_close(f);
20658f0484fSRodney W. Grimes f = wantfd;
207cc3d8572SJohn Baldwin } else
208cc3d8572SJohn Baldwin (void)_close(fp->_file);
20958f0484fSRodney W. Grimes }
21058f0484fSRodney W. Grimes
211c55d7e86SJohn Baldwin /*
212c55d7e86SJohn Baldwin * File descriptors are a full int, but _file is only a short.
213c55d7e86SJohn Baldwin * If we get a valid file descriptor that is greater than
214c55d7e86SJohn Baldwin * SHRT_MAX, then the fd will get sign-extended into an
215c55d7e86SJohn Baldwin * invalid file descriptor. Handle this case by failing the
216c55d7e86SJohn Baldwin * open.
217c55d7e86SJohn Baldwin */
218c55d7e86SJohn Baldwin if (f > SHRT_MAX) {
219c55d7e86SJohn Baldwin fp->_flags = 0; /* set it free */
220c55d7e86SJohn Baldwin errno = EMFILE;
221fda0a14fSKonstantin Belousov fp = NULL;
222fda0a14fSKonstantin Belousov goto end;
223c55d7e86SJohn Baldwin }
224c55d7e86SJohn Baldwin
22558f0484fSRodney W. Grimes fp->_flags = flags;
22658f0484fSRodney W. Grimes fp->_file = f;
22758f0484fSRodney W. Grimes fp->_cookie = fp;
22858f0484fSRodney W. Grimes fp->_read = __sread;
22958f0484fSRodney W. Grimes fp->_write = __swrite;
23058f0484fSRodney W. Grimes fp->_seek = __sseek;
23158f0484fSRodney W. Grimes fp->_close = __sclose;
232b0bf75c8SAndrey A. Chernov /*
233b0bf75c8SAndrey A. Chernov * When opening in append mode, even though we use O_APPEND,
234b0bf75c8SAndrey A. Chernov * we need to seek to the end so that ftell() gets the right
235b0bf75c8SAndrey A. Chernov * answer. If the user then alters the seek pointer, or
236b0bf75c8SAndrey A. Chernov * the file extends, this will fail, but there is not much
237b0bf75c8SAndrey A. Chernov * we can do about this. (We could set __SAPP and check in
238b0bf75c8SAndrey A. Chernov * fseek and ftell.)
239b0bf75c8SAndrey A. Chernov */
240b956b176SAndrey A. Chernov if (oflags & O_APPEND) {
2418b63538dSAndrey A. Chernov fp->_flags2 |= __S2OAP;
242b0bf75c8SAndrey A. Chernov (void) _sseek(fp, (fpos_t)0, SEEK_END);
243b956b176SAndrey A. Chernov }
244fda0a14fSKonstantin Belousov end:
245fda0a14fSKonstantin Belousov FUNLOCKFILE_CANCELSAFE();
24658f0484fSRodney W. Grimes return (fp);
24758f0484fSRodney W. Grimes }
248