1*1bcc0142Skamil /* $NetBSD: freopen.c,v 1.22 2018/01/17 01:24:30 kamil Exp $ */
2255db7b2Sjtc
361f28255Scgd /*-
4255db7b2Sjtc * Copyright (c) 1990, 1993
5255db7b2Sjtc * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * This code is derived from software contributed to Berkeley by
861f28255Scgd * Chris Torek.
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
3523312f88Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93";
3923312f88Schristos #else
40*1bcc0142Skamil __RCSID("$NetBSD: freopen.c,v 1.22 2018/01/17 01:24:30 kamil Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd
4461f28255Scgd #include <sys/types.h>
4561f28255Scgd #include <sys/stat.h>
46b48252f3Slukem
47b48252f3Slukem #include <assert.h>
4861f28255Scgd #include <errno.h>
49b48252f3Slukem #include <fcntl.h>
5061f28255Scgd #include <unistd.h>
5161f28255Scgd #include <stdio.h>
5261f28255Scgd #include <stdlib.h>
5317f3654aSyamt #include <wchar.h>
54749de7f2Schristos #include <limits.h>
553fdac2b8Sthorpej #include "reentrant.h"
5661f28255Scgd #include "local.h"
5761f28255Scgd
5861f28255Scgd /*
5961f28255Scgd * Re-direct an existing, open (probably) file to some other file.
6061f28255Scgd * ANSI is written such that the original file gets closed if at
6161f28255Scgd * all possible, no matter what.
6261f28255Scgd */
6361f28255Scgd FILE *
freopen(const char * file,const char * mode,FILE * fp)64526d9427Schristos freopen(const char *file, const char *mode, FILE *fp)
6561f28255Scgd {
66c8bafd62Sperry int f;
6761f28255Scgd int flags, isopen, oflags, sverrno, wantfd;
6861f28255Scgd
69b48252f3Slukem _DIAGASSERT(file != NULL);
70b48252f3Slukem _DIAGASSERT(mode != NULL);
71b48252f3Slukem _DIAGASSERT(fp != NULL);
72b48252f3Slukem
7361f28255Scgd if ((flags = __sflags(mode, &oflags)) == 0) {
7461f28255Scgd (void) fclose(fp);
75526d9427Schristos return NULL;
7661f28255Scgd }
7761f28255Scgd
7861f28255Scgd if (!__sdidinit)
7961f28255Scgd __sinit();
8061f28255Scgd
8161f28255Scgd /*
8261f28255Scgd * There are actually programs that depend on being able to "freopen"
8361f28255Scgd * descriptors that weren't originally open. Keep this from breaking.
8461f28255Scgd * Remember whether the stream was open to begin with, and which file
8561f28255Scgd * descriptor (if any) was associated with it. If it was attached to
8661f28255Scgd * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
8761f28255Scgd * should work. This is unnecessary if it was not a Unix file.
8861f28255Scgd */
8961f28255Scgd if (fp->_flags == 0) {
9061f28255Scgd fp->_flags = __SEOF; /* hold on to it */
9161f28255Scgd isopen = 0;
9261f28255Scgd wantfd = -1;
9361f28255Scgd } else {
9461f28255Scgd /* flush the stream; ANSI doesn't require this. */
9561f28255Scgd if (fp->_flags & __SWR)
9661f28255Scgd (void)__sflush(fp);
9761f28255Scgd /* if close is NULL, closing is a no-op, hence pointless */
9861f28255Scgd isopen = fp->_close != NULL;
99749de7f2Schristos if ((wantfd = __sfileno(fp)) == -1 && isopen) {
10061f28255Scgd (void) (*fp->_close)(fp->_cookie);
10161f28255Scgd isopen = 0;
10261f28255Scgd }
10361f28255Scgd }
10461f28255Scgd
10561f28255Scgd /* Get a new descriptor to refer to the new file. */
10661f28255Scgd f = open(file, oflags, DEFFILEMODE);
10761f28255Scgd if (f < 0 && isopen) {
10861f28255Scgd /* If out of fd's close the old one and try again. */
10961f28255Scgd if (errno == ENFILE || errno == EMFILE) {
11061f28255Scgd (void) (*fp->_close)(fp->_cookie);
11161f28255Scgd isopen = 0;
11261f28255Scgd f = open(file, oflags, DEFFILEMODE);
11361f28255Scgd }
11461f28255Scgd }
11561f28255Scgd sverrno = errno;
11661f28255Scgd
11761f28255Scgd /*
11861f28255Scgd * Finish closing fp. Even if the open succeeded above, we cannot
11961f28255Scgd * keep fp->_base: it may be the wrong size. This loses the effect
12061f28255Scgd * of any setbuffer calls, but stdio has always done this before.
12161f28255Scgd */
1220fd4b530Smycroft if (isopen && f != wantfd)
12361f28255Scgd (void) (*fp->_close)(fp->_cookie);
12461f28255Scgd if (fp->_flags & __SMBF)
12561f28255Scgd free((char *)fp->_bf._base);
12661f28255Scgd fp->_w = 0;
12761f28255Scgd fp->_r = 0;
12861f28255Scgd fp->_p = NULL;
12961f28255Scgd fp->_bf._base = NULL;
13061f28255Scgd fp->_bf._size = 0;
13161f28255Scgd fp->_lbfsize = 0;
13261f28255Scgd if (HASUB(fp))
13361f28255Scgd FREEUB(fp);
13417f3654aSyamt WCIO_FREE(fp);
13517f3654aSyamt _UB(fp)._size = 0;
13661f28255Scgd FREELB(fp);
13761f28255Scgd
13861f28255Scgd if (f < 0) { /* did not get it after all */
13961f28255Scgd fp->_flags = 0; /* set it free */
14061f28255Scgd errno = sverrno; /* restore in case _close clobbered */
141526d9427Schristos return NULL;
14261f28255Scgd }
14361f28255Scgd
14461f28255Scgd /*
14561f28255Scgd * If reopening something that was open before on a real file, try
14661f28255Scgd * to maintain the descriptor. Various C library routines (perror)
14761f28255Scgd * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
14861f28255Scgd */
14961f28255Scgd if (wantfd >= 0 && f != wantfd) {
15061f28255Scgd if (dup2(f, wantfd) >= 0) {
15161f28255Scgd (void) close(f);
15261f28255Scgd f = wantfd;
15361f28255Scgd }
15461f28255Scgd }
15561f28255Scgd
156749de7f2Schristos /*
157749de7f2Schristos * File descriptors are a full int, but _file is only a short.
158749de7f2Schristos * If we get a valid file descriptor that is greater or equal to
159749de7f2Schristos * USHRT_MAX, then the fd will get sign-extended into an
160749de7f2Schristos * invalid file descriptor. Handle this case by failing the
161749de7f2Schristos * open. (We treat the short as unsigned, and special-case -1).
162749de7f2Schristos */
163749de7f2Schristos if (f >= USHRT_MAX) {
164749de7f2Schristos (void)close(f);
165749de7f2Schristos errno = EMFILE;
166749de7f2Schristos return NULL;
167749de7f2Schristos }
168749de7f2Schristos
16961f28255Scgd fp->_flags = flags;
17061f28255Scgd fp->_file = f;
17161f28255Scgd fp->_cookie = fp;
17261f28255Scgd fp->_read = __sread;
17361f28255Scgd fp->_write = __swrite;
17461f28255Scgd fp->_seek = __sseek;
17561f28255Scgd fp->_close = __sclose;
176cb697ba0Skleink
177cb697ba0Skleink /*
178cb697ba0Skleink * When reopening in append mode, even though we use O_APPEND,
179cb697ba0Skleink * we need to seek to the end so that ftell() gets the right
180cb697ba0Skleink * answer. If the user then alters the seek pointer, or
181cb697ba0Skleink * the file extends, this will fail, but there is not much
182cb697ba0Skleink * we can do about this. (We could set __SAPP and check in
183cb697ba0Skleink * fseek and ftell.)
184cb697ba0Skleink */
185cb697ba0Skleink if (oflags & O_APPEND)
1861897181aSchristos (void) __sseek((void *)fp, (off_t)0, SEEK_END);
187526d9427Schristos return fp;
18861f28255Scgd }
189