1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 25 unchanged lines hidden (view full) --- 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)freopen.c 8.1 (Berkeley) 6/4/93"; 40#endif 41static const char rcsid[] = |
42 "$FreeBSD: head/lib/libc/stdio/freopen.c 56698 2000-01-27 23:07:25Z jasone $"; |
43#endif /* LIBC_SCCS and not lint */ 44 45#include <sys/types.h> 46#include <sys/stat.h> 47#include <fcntl.h> 48#include <errno.h> 49#include <unistd.h> 50#include <stdio.h> --- 44 unchanged lines hidden (view full) --- 95 isopen = fp->_close != NULL; 96 if ((wantfd = fp->_file) < 0 && isopen) { 97 (void) (*fp->_close)(fp->_cookie); 98 isopen = 0; 99 } 100 } 101 102 /* Get a new descriptor to refer to the new file. */ |
103 f = _open(file, oflags, DEFFILEMODE); |
104 if (f < 0 && isopen) { 105 /* If out of fd's close the old one and try again. */ 106 if (errno == ENFILE || errno == EMFILE) { 107 (void) (*fp->_close)(fp->_cookie); 108 isopen = 0; |
109 f = _open(file, oflags, DEFFILEMODE); |
110 } 111 } 112 sverrno = errno; 113 114 /* 115 * Finish closing fp. Even if the open succeeded above, we cannot 116 * keep fp->_base: it may be the wrong size. This loses the effect 117 * of any setbuffer calls, but stdio has always done this before. --- 24 unchanged lines hidden (view full) --- 142 143 /* 144 * If reopening something that was open before on a real file, try 145 * to maintain the descriptor. Various C library routines (perror) 146 * assume stderr is always fd STDERR_FILENO, even if being freopen'd. 147 */ 148 if (wantfd >= 0 && f != wantfd) { 149 if (dup2(f, wantfd) >= 0) { |
150 (void)_close(f); |
151 f = wantfd; 152 } 153 } 154 155 fp->_flags = flags; 156 fp->_file = f; 157 fp->_cookie = fp; 158 fp->_read = __sread; 159 fp->_write = __swrite; 160 fp->_seek = __sseek; 161 fp->_close = __sclose; 162 FUNLOCKFILE(fp); 163 return (fp); 164} |