11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1990, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Chris Torek. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 4. Neither the name of the University nor the names of its contributors 171573Srgrimes * may be used to endorse or promote products derived from this software 181573Srgrimes * without specific prior written permission. 191573Srgrimes * 201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 341573Srgrimesstatic char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93"; 351573Srgrimes#endif /* LIBC_SCCS and not lint */ 3692986Sobrien#include <sys/cdefs.h> 3792986Sobrien__FBSDID("$FreeBSD$"); 381573Srgrimes 3971579Sdeischen#include "namespace.h" 401573Srgrimes#include <sys/types.h> 411573Srgrimes#include <sys/stat.h> 421573Srgrimes#include <fcntl.h> 431573Srgrimes#include <errno.h> 44176629Sjhb#include <limits.h> 451573Srgrimes#include <unistd.h> 461573Srgrimes#include <stdio.h> 471573Srgrimes#include <stdlib.h> 4871579Sdeischen#include "un-namespace.h" 4971579Sdeischen#include "libc_private.h" 501573Srgrimes#include "local.h" 511573Srgrimes 528870Srgrimes/* 538870Srgrimes * Re-direct an existing, open (probably) file to some other file. 541573Srgrimes * ANSI is written such that the original file gets closed if at 551573Srgrimes * all possible, no matter what. 561573Srgrimes */ 571573SrgrimesFILE * 581573Srgrimesfreopen(file, mode, fp) 59104989Smike const char * __restrict file; 60104989Smike const char * __restrict mode; 6172373Sdeischen FILE *fp; 621573Srgrimes{ 6372373Sdeischen int f; 64109871Stjr int dflags, flags, isopen, oflags, sverrno, wantfd; 651573Srgrimes 661573Srgrimes if ((flags = __sflags(mode, &oflags)) == 0) { 67163406Sache sverrno = errno; 681573Srgrimes (void) fclose(fp); 69163406Sache errno = sverrno; 701573Srgrimes return (NULL); 711573Srgrimes } 721573Srgrimes 7353460Sdt FLOCKFILE(fp); 7453460Sdt 751573Srgrimes if (!__sdidinit) 761573Srgrimes __sinit(); 771573Srgrimes 781573Srgrimes /* 79109871Stjr * If the filename is a NULL pointer, the caller is asking us to 80109871Stjr * re-open the same file with a different mode. We allow this only 81109871Stjr * if the modes are compatible. 82109871Stjr */ 83109871Stjr if (file == NULL) { 84109871Stjr /* See comment below regarding freopen() of closed files. */ 85109871Stjr if (fp->_flags == 0) { 86109871Stjr FUNLOCKFILE(fp); 87109871Stjr errno = EINVAL; 88109871Stjr return (NULL); 89109871Stjr } 90109871Stjr if ((dflags = _fcntl(fp->_file, F_GETFL)) < 0) { 91109871Stjr sverrno = errno; 92109871Stjr fclose(fp); 93109871Stjr FUNLOCKFILE(fp); 94109871Stjr errno = sverrno; 95109871Stjr return (NULL); 96109871Stjr } 97109871Stjr if ((dflags & O_ACCMODE) != O_RDWR && (dflags & O_ACCMODE) != 98109871Stjr (oflags & O_ACCMODE)) { 99109871Stjr fclose(fp); 100109871Stjr FUNLOCKFILE(fp); 101109871Stjr errno = EINVAL; 102109871Stjr return (NULL); 103109871Stjr } 104163430Sache if (fp->_flags & __SWR) 105163430Sache (void) __sflush(fp); 106109871Stjr if ((oflags ^ dflags) & O_APPEND) { 107109871Stjr dflags &= ~O_APPEND; 108109871Stjr dflags |= oflags & O_APPEND; 109109871Stjr if (_fcntl(fp->_file, F_SETFL, dflags) < 0) { 110109871Stjr sverrno = errno; 111109871Stjr fclose(fp); 112109871Stjr FUNLOCKFILE(fp); 113109871Stjr errno = sverrno; 114109871Stjr return (NULL); 115109871Stjr } 116109871Stjr } 117163405Sache if (oflags & O_TRUNC) 118163405Sache (void) ftruncate(fp->_file, (off_t)0); 119163430Sache if (!(oflags & O_APPEND)) 120163430Sache (void) _sseek(fp, (fpos_t)0, SEEK_SET); 121257229Sjilles if (oflags & O_CLOEXEC) 122257229Sjilles (void) _fcntl(fp->_file, F_SETFD, FD_CLOEXEC); 123109871Stjr f = fp->_file; 124109871Stjr isopen = 0; 125109871Stjr wantfd = -1; 126109871Stjr goto finish; 127109871Stjr } 128109871Stjr 129109871Stjr /* 1301573Srgrimes * There are actually programs that depend on being able to "freopen" 1311573Srgrimes * descriptors that weren't originally open. Keep this from breaking. 1321573Srgrimes * Remember whether the stream was open to begin with, and which file 1331573Srgrimes * descriptor (if any) was associated with it. If it was attached to 1341573Srgrimes * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin) 1351573Srgrimes * should work. This is unnecessary if it was not a Unix file. 1361573Srgrimes */ 1371573Srgrimes if (fp->_flags == 0) { 1381573Srgrimes fp->_flags = __SEOF; /* hold on to it */ 1391573Srgrimes isopen = 0; 1401573Srgrimes wantfd = -1; 1411573Srgrimes } else { 1421573Srgrimes /* flush the stream; ANSI doesn't require this. */ 1431573Srgrimes if (fp->_flags & __SWR) 1441573Srgrimes (void) __sflush(fp); 1451573Srgrimes /* if close is NULL, closing is a no-op, hence pointless */ 1461573Srgrimes isopen = fp->_close != NULL; 1471573Srgrimes if ((wantfd = fp->_file) < 0 && isopen) { 1481573Srgrimes (void) (*fp->_close)(fp->_cookie); 1491573Srgrimes isopen = 0; 1501573Srgrimes } 1511573Srgrimes } 1521573Srgrimes 1531573Srgrimes /* Get a new descriptor to refer to the new file. */ 15456698Sjasone f = _open(file, oflags, DEFFILEMODE); 1551573Srgrimes sverrno = errno; 1561573Srgrimes 157109871Stjrfinish: 1581573Srgrimes /* 1591573Srgrimes * Finish closing fp. Even if the open succeeded above, we cannot 1601573Srgrimes * keep fp->_base: it may be the wrong size. This loses the effect 1611573Srgrimes * of any setbuffer calls, but stdio has always done this before. 162216334Sjhb * 163216334Sjhb * Leave the existing file descriptor open until dup2() is called 164216334Sjhb * below to avoid races where a concurrent open() in another thread 165216334Sjhb * could claim the existing descriptor. 1661573Srgrimes */ 1671573Srgrimes if (fp->_flags & __SMBF) 1681573Srgrimes free((char *)fp->_bf._base); 1691573Srgrimes fp->_w = 0; 1701573Srgrimes fp->_r = 0; 1711573Srgrimes fp->_p = NULL; 1721573Srgrimes fp->_bf._base = NULL; 1731573Srgrimes fp->_bf._size = 0; 1741573Srgrimes fp->_lbfsize = 0; 1751573Srgrimes if (HASUB(fp)) 1761573Srgrimes FREEUB(fp); 1771573Srgrimes fp->_ub._size = 0; 1781573Srgrimes if (HASLB(fp)) 1791573Srgrimes FREELB(fp); 1801573Srgrimes fp->_lb._size = 0; 181178287Sjhb fp->_orientation = 0; 182178287Sjhb memset(&fp->_mbstate, 0, sizeof(mbstate_t)); 1831573Srgrimes 1841573Srgrimes if (f < 0) { /* did not get it after all */ 185216334Sjhb if (isopen) 186216334Sjhb (void) (*fp->_close)(fp->_cookie); 1871573Srgrimes fp->_flags = 0; /* set it free */ 188163406Sache FUNLOCKFILE(fp); 1891573Srgrimes errno = sverrno; /* restore in case _close clobbered */ 1901573Srgrimes return (NULL); 1911573Srgrimes } 1921573Srgrimes 1931573Srgrimes /* 1941573Srgrimes * If reopening something that was open before on a real file, try 1951573Srgrimes * to maintain the descriptor. Various C library routines (perror) 1961573Srgrimes * assume stderr is always fd STDERR_FILENO, even if being freopen'd. 1971573Srgrimes */ 198216334Sjhb if (wantfd >= 0) { 199257229Sjilles if ((oflags & O_CLOEXEC ? _fcntl(f, F_DUP2FD_CLOEXEC, wantfd) : 200257229Sjilles _dup2(f, wantfd)) >= 0) { 20156698Sjasone (void)_close(f); 2021573Srgrimes f = wantfd; 203216334Sjhb } else 204216334Sjhb (void)_close(fp->_file); 2051573Srgrimes } 2061573Srgrimes 207176628Sjhb /* 208176628Sjhb * File descriptors are a full int, but _file is only a short. 209176628Sjhb * If we get a valid file descriptor that is greater than 210176628Sjhb * SHRT_MAX, then the fd will get sign-extended into an 211176628Sjhb * invalid file descriptor. Handle this case by failing the 212176628Sjhb * open. 213176628Sjhb */ 214176628Sjhb if (f > SHRT_MAX) { 215176628Sjhb fp->_flags = 0; /* set it free */ 216176628Sjhb FUNLOCKFILE(fp); 217176628Sjhb errno = EMFILE; 218176628Sjhb return (NULL); 219176628Sjhb } 220176628Sjhb 2211573Srgrimes fp->_flags = flags; 2221573Srgrimes fp->_file = f; 2231573Srgrimes fp->_cookie = fp; 2241573Srgrimes fp->_read = __sread; 2251573Srgrimes fp->_write = __swrite; 2261573Srgrimes fp->_seek = __sseek; 2271573Srgrimes fp->_close = __sclose; 228163430Sache /* 229163430Sache * When opening in append mode, even though we use O_APPEND, 230163430Sache * we need to seek to the end so that ftell() gets the right 231163430Sache * answer. If the user then alters the seek pointer, or 232163430Sache * the file extends, this will fail, but there is not much 233163430Sache * we can do about this. (We could set __SAPP and check in 234163430Sache * fseek and ftell.) 235163430Sache */ 236163430Sache if (oflags & O_APPEND) 237163430Sache (void) _sseek(fp, (fpos_t)0, SEEK_END); 23853460Sdt FUNLOCKFILE(fp); 2391573Srgrimes return (fp); 2401573Srgrimes} 241