xref: /freebsd/lib/libc/stdio/freopen.c (revision cc3d85727dff868e59352aef7cff20b2aaa1a063)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Chris Torek.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes  *    without specific prior written permission.
1958f0484fSRodney W. Grimes  *
2058f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes  * SUCH DAMAGE.
3158f0484fSRodney W. Grimes  */
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3458f0484fSRodney W. Grimes static char sccsid[] = "@(#)freopen.c	8.1 (Berkeley) 6/4/93";
3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
36333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3858f0484fSRodney W. Grimes 
39d201fe46SDaniel Eischen #include "namespace.h"
4058f0484fSRodney W. Grimes #include <sys/types.h>
4158f0484fSRodney W. Grimes #include <sys/stat.h>
4258f0484fSRodney W. Grimes #include <fcntl.h>
4358f0484fSRodney W. Grimes #include <errno.h>
44fc9ab4f6SJohn Baldwin #include <limits.h>
4558f0484fSRodney W. Grimes #include <unistd.h>
4658f0484fSRodney W. Grimes #include <stdio.h>
4758f0484fSRodney W. Grimes #include <stdlib.h>
48d201fe46SDaniel Eischen #include "un-namespace.h"
49d201fe46SDaniel Eischen #include "libc_private.h"
5058f0484fSRodney W. Grimes #include "local.h"
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes /*
5358f0484fSRodney W. Grimes  * Re-direct an existing, open (probably) file to some other file.
5458f0484fSRodney W. Grimes  * ANSI is written such that the original file gets closed if at
5558f0484fSRodney W. Grimes  * all possible, no matter what.
5658f0484fSRodney W. Grimes  */
5758f0484fSRodney W. Grimes FILE *
5858f0484fSRodney W. Grimes freopen(file, mode, fp)
5954e4e385SMike Barcroft 	const char * __restrict file;
6054e4e385SMike Barcroft 	const char * __restrict mode;
6129ac6bd2SDaniel Eischen 	FILE *fp;
6258f0484fSRodney W. Grimes {
6329ac6bd2SDaniel Eischen 	int f;
64c2974987STim J. Robbins 	int dflags, flags, isopen, oflags, sverrno, wantfd;
6558f0484fSRodney W. Grimes 
6658f0484fSRodney W. Grimes 	if ((flags = __sflags(mode, &oflags)) == 0) {
675a8d08f5SAndrey A. Chernov 		sverrno = errno;
6858f0484fSRodney W. Grimes 		(void) fclose(fp);
695a8d08f5SAndrey A. Chernov 		errno = sverrno;
7058f0484fSRodney W. Grimes 		return (NULL);
7158f0484fSRodney W. Grimes 	}
7258f0484fSRodney W. Grimes 
7332fc781eSDmitrij Tejblum 	FLOCKFILE(fp);
7432fc781eSDmitrij Tejblum 
7558f0484fSRodney W. Grimes 	if (!__sdidinit)
7658f0484fSRodney W. Grimes 		__sinit();
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes 	/*
79c2974987STim J. Robbins 	 * If the filename is a NULL pointer, the caller is asking us to
80c2974987STim J. Robbins 	 * re-open the same file with a different mode. We allow this only
81c2974987STim J. Robbins 	 * if the modes are compatible.
82c2974987STim J. Robbins 	 */
83c2974987STim J. Robbins 	if (file == NULL) {
84c2974987STim J. Robbins 		/* See comment below regarding freopen() of closed files. */
85c2974987STim J. Robbins 		if (fp->_flags == 0) {
86c2974987STim J. Robbins 			FUNLOCKFILE(fp);
87c2974987STim J. Robbins 			errno = EINVAL;
88c2974987STim J. Robbins 			return (NULL);
89c2974987STim J. Robbins 		}
90c2974987STim J. Robbins 		if ((dflags = _fcntl(fp->_file, F_GETFL)) < 0) {
91c2974987STim J. Robbins 			sverrno = errno;
92c2974987STim J. Robbins 			fclose(fp);
93c2974987STim J. Robbins 			FUNLOCKFILE(fp);
94c2974987STim J. Robbins 			errno = sverrno;
95c2974987STim J. Robbins 			return (NULL);
96c2974987STim J. Robbins 		}
97c2974987STim J. Robbins 		if ((dflags & O_ACCMODE) != O_RDWR && (dflags & O_ACCMODE) !=
98c2974987STim J. Robbins 		    (oflags & O_ACCMODE)) {
99c2974987STim J. Robbins 			fclose(fp);
100c2974987STim J. Robbins 			FUNLOCKFILE(fp);
101c2974987STim J. Robbins 			errno = EINVAL;
102c2974987STim J. Robbins 			return (NULL);
103c2974987STim J. Robbins 		}
104b0bf75c8SAndrey A. Chernov 		if (fp->_flags & __SWR)
105b0bf75c8SAndrey A. Chernov 			(void) __sflush(fp);
106c2974987STim J. Robbins 		if ((oflags ^ dflags) & O_APPEND) {
107c2974987STim J. Robbins 			dflags &= ~O_APPEND;
108c2974987STim J. Robbins 			dflags |= oflags & O_APPEND;
109c2974987STim J. Robbins 			if (_fcntl(fp->_file, F_SETFL, dflags) < 0) {
110c2974987STim J. Robbins 				sverrno = errno;
111c2974987STim J. Robbins 				fclose(fp);
112c2974987STim J. Robbins 				FUNLOCKFILE(fp);
113c2974987STim J. Robbins 				errno = sverrno;
114c2974987STim J. Robbins 				return (NULL);
115c2974987STim J. Robbins 			}
116c2974987STim J. Robbins 		}
1176f8d08b3SAndrey A. Chernov 		if (oflags & O_TRUNC)
1186f8d08b3SAndrey A. Chernov 			(void) ftruncate(fp->_file, (off_t)0);
119b0bf75c8SAndrey A. Chernov 		if (!(oflags & O_APPEND))
120b0bf75c8SAndrey A. Chernov 			(void) _sseek(fp, (fpos_t)0, SEEK_SET);
121c2974987STim J. Robbins 		f = fp->_file;
122c2974987STim J. Robbins 		isopen = 0;
123c2974987STim J. Robbins 		wantfd = -1;
124c2974987STim J. Robbins 		goto finish;
125c2974987STim J. Robbins 	}
126c2974987STim J. Robbins 
127c2974987STim J. Robbins 	/*
12858f0484fSRodney W. Grimes 	 * There are actually programs that depend on being able to "freopen"
12958f0484fSRodney W. Grimes 	 * descriptors that weren't originally open.  Keep this from breaking.
13058f0484fSRodney W. Grimes 	 * Remember whether the stream was open to begin with, and which file
13158f0484fSRodney W. Grimes 	 * descriptor (if any) was associated with it.  If it was attached to
13258f0484fSRodney W. Grimes 	 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
13358f0484fSRodney W. Grimes 	 * should work.  This is unnecessary if it was not a Unix file.
13458f0484fSRodney W. Grimes 	 */
13558f0484fSRodney W. Grimes 	if (fp->_flags == 0) {
13658f0484fSRodney W. Grimes 		fp->_flags = __SEOF;	/* hold on to it */
13758f0484fSRodney W. Grimes 		isopen = 0;
13858f0484fSRodney W. Grimes 		wantfd = -1;
13958f0484fSRodney W. Grimes 	} else {
14058f0484fSRodney W. Grimes 		/* flush the stream; ANSI doesn't require this. */
14158f0484fSRodney W. Grimes 		if (fp->_flags & __SWR)
14258f0484fSRodney W. Grimes 			(void) __sflush(fp);
14358f0484fSRodney W. Grimes 		/* if close is NULL, closing is a no-op, hence pointless */
14458f0484fSRodney W. Grimes 		isopen = fp->_close != NULL;
14558f0484fSRodney W. Grimes 		if ((wantfd = fp->_file) < 0 && isopen) {
14658f0484fSRodney W. Grimes 			(void) (*fp->_close)(fp->_cookie);
14758f0484fSRodney W. Grimes 			isopen = 0;
14858f0484fSRodney W. Grimes 		}
14958f0484fSRodney W. Grimes 	}
15058f0484fSRodney W. Grimes 
15158f0484fSRodney W. Grimes 	/* Get a new descriptor to refer to the new file. */
1529233c4d9SJason Evans 	f = _open(file, oflags, DEFFILEMODE);
15358f0484fSRodney W. Grimes 	sverrno = errno;
15458f0484fSRodney W. Grimes 
155c2974987STim J. Robbins finish:
15658f0484fSRodney W. Grimes 	/*
15758f0484fSRodney W. Grimes 	 * Finish closing fp.  Even if the open succeeded above, we cannot
15858f0484fSRodney W. Grimes 	 * keep fp->_base: it may be the wrong size.  This loses the effect
15958f0484fSRodney W. Grimes 	 * of any setbuffer calls, but stdio has always done this before.
160*cc3d8572SJohn Baldwin 	 *
161*cc3d8572SJohn Baldwin 	 * Leave the existing file descriptor open until dup2() is called
162*cc3d8572SJohn Baldwin 	 * below to avoid races where a concurrent open() in another thread
163*cc3d8572SJohn Baldwin 	 * could claim the existing descriptor.
16458f0484fSRodney W. Grimes 	 */
16558f0484fSRodney W. Grimes 	if (fp->_flags & __SMBF)
16658f0484fSRodney W. Grimes 		free((char *)fp->_bf._base);
16758f0484fSRodney W. Grimes 	fp->_w = 0;
16858f0484fSRodney W. Grimes 	fp->_r = 0;
16958f0484fSRodney W. Grimes 	fp->_p = NULL;
17058f0484fSRodney W. Grimes 	fp->_bf._base = NULL;
17158f0484fSRodney W. Grimes 	fp->_bf._size = 0;
17258f0484fSRodney W. Grimes 	fp->_lbfsize = 0;
17358f0484fSRodney W. Grimes 	if (HASUB(fp))
17458f0484fSRodney W. Grimes 		FREEUB(fp);
17558f0484fSRodney W. Grimes 	fp->_ub._size = 0;
17658f0484fSRodney W. Grimes 	if (HASLB(fp))
17758f0484fSRodney W. Grimes 		FREELB(fp);
17858f0484fSRodney W. Grimes 	fp->_lb._size = 0;
1791e98f887SJohn Baldwin 	fp->_orientation = 0;
1801e98f887SJohn Baldwin 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
18158f0484fSRodney W. Grimes 
18258f0484fSRodney W. Grimes 	if (f < 0) {			/* did not get it after all */
183*cc3d8572SJohn Baldwin 		if (isopen)
184*cc3d8572SJohn Baldwin 			(void) (*fp->_close)(fp->_cookie);
18558f0484fSRodney W. Grimes 		fp->_flags = 0;		/* set it free */
18632fc781eSDmitrij Tejblum 		FUNLOCKFILE(fp);
1875a8d08f5SAndrey A. Chernov 		errno = sverrno;	/* restore in case _close clobbered */
18858f0484fSRodney W. Grimes 		return (NULL);
18958f0484fSRodney W. Grimes 	}
19058f0484fSRodney W. Grimes 
19158f0484fSRodney W. Grimes 	/*
19258f0484fSRodney W. Grimes 	 * If reopening something that was open before on a real file, try
19358f0484fSRodney W. Grimes 	 * to maintain the descriptor.  Various C library routines (perror)
19458f0484fSRodney W. Grimes 	 * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
19558f0484fSRodney W. Grimes 	 */
196*cc3d8572SJohn Baldwin 	if (wantfd >= 0) {
197d201fe46SDaniel Eischen 		if (_dup2(f, wantfd) >= 0) {
1989233c4d9SJason Evans 			(void)_close(f);
19958f0484fSRodney W. Grimes 			f = wantfd;
200*cc3d8572SJohn Baldwin 		} else
201*cc3d8572SJohn Baldwin 			(void)_close(fp->_file);
20258f0484fSRodney W. Grimes 	}
20358f0484fSRodney W. Grimes 
204c55d7e86SJohn Baldwin 	/*
205c55d7e86SJohn Baldwin 	 * File descriptors are a full int, but _file is only a short.
206c55d7e86SJohn Baldwin 	 * If we get a valid file descriptor that is greater than
207c55d7e86SJohn Baldwin 	 * SHRT_MAX, then the fd will get sign-extended into an
208c55d7e86SJohn Baldwin 	 * invalid file descriptor.  Handle this case by failing the
209c55d7e86SJohn Baldwin 	 * open.
210c55d7e86SJohn Baldwin 	 */
211c55d7e86SJohn Baldwin 	if (f > SHRT_MAX) {
212c55d7e86SJohn Baldwin 		fp->_flags = 0;		/* set it free */
213c55d7e86SJohn Baldwin 		FUNLOCKFILE(fp);
214c55d7e86SJohn Baldwin 		errno = EMFILE;
215c55d7e86SJohn Baldwin 		return (NULL);
216c55d7e86SJohn Baldwin 	}
217c55d7e86SJohn Baldwin 
21858f0484fSRodney W. Grimes 	fp->_flags = flags;
21958f0484fSRodney W. Grimes 	fp->_file = f;
22058f0484fSRodney W. Grimes 	fp->_cookie = fp;
22158f0484fSRodney W. Grimes 	fp->_read = __sread;
22258f0484fSRodney W. Grimes 	fp->_write = __swrite;
22358f0484fSRodney W. Grimes 	fp->_seek = __sseek;
22458f0484fSRodney W. Grimes 	fp->_close = __sclose;
225b0bf75c8SAndrey A. Chernov 	/*
226b0bf75c8SAndrey A. Chernov 	 * When opening in append mode, even though we use O_APPEND,
227b0bf75c8SAndrey A. Chernov 	 * we need to seek to the end so that ftell() gets the right
228b0bf75c8SAndrey A. Chernov 	 * answer.  If the user then alters the seek pointer, or
229b0bf75c8SAndrey A. Chernov 	 * the file extends, this will fail, but there is not much
230b0bf75c8SAndrey A. Chernov 	 * we can do about this.  (We could set __SAPP and check in
231b0bf75c8SAndrey A. Chernov 	 * fseek and ftell.)
232b0bf75c8SAndrey A. Chernov 	 */
233b0bf75c8SAndrey A. Chernov 	if (oflags & O_APPEND)
234b0bf75c8SAndrey A. Chernov 		(void) _sseek(fp, (fpos_t)0, SEEK_END);
23532fc781eSDmitrij Tejblum 	FUNLOCKFILE(fp);
23658f0484fSRodney W. Grimes 	return (fp);
23758f0484fSRodney W. Grimes }
238