View | Details | Raw Unified | Return to bug 79887
Collapse All | Expand All

(-)freopen.c (-11 / +6 lines)
Lines 155-168 Link Here
155
155
156
	/* Get a new descriptor to refer to the new file. */
156
	/* Get a new descriptor to refer to the new file. */
157
	f = _open(file, oflags, DEFFILEMODE);
157
	f = _open(file, oflags, DEFFILEMODE);
158
	if (f < 0 && isopen) {
159
		/* If out of fd's close the old one and try again. */
160
		if (errno == ENFILE || errno == EMFILE) {
161
			(void) (*fp->_close)(fp->_cookie);
162
			isopen = 0;
163
			f = _open(file, oflags, DEFFILEMODE);
164
		}
165
	}
166
	sverrno = errno;
158
	sverrno = errno;
167
159
168
finish:
160
finish:
Lines 171-178 Link Here
171
	 * keep fp->_base: it may be the wrong size.  This loses the effect
163
	 * keep fp->_base: it may be the wrong size.  This loses the effect
172
	 * of any setbuffer calls, but stdio has always done this before.
164
	 * of any setbuffer calls, but stdio has always done this before.
173
	 */
165
	 */
174
	if (isopen)
175
		(void) (*fp->_close)(fp->_cookie);
176
	if (fp->_flags & __SMBF)
166
	if (fp->_flags & __SMBF)
177
		free((char *)fp->_bf._base);
167
		free((char *)fp->_bf._base);
178
	fp->_w = 0;
168
	fp->_w = 0;
Lines 191-196 Link Here
191
	memset(&fp->_extra->mbstate, 0, sizeof(mbstate_t));
181
	memset(&fp->_extra->mbstate, 0, sizeof(mbstate_t));
192
182
193
	if (f < 0) {			/* did not get it after all */
183
	if (f < 0) {			/* did not get it after all */
184
		if (isopen)
185
			(void) (*fp->_close)(fp->_cookie);
194
		fp->_flags = 0;		/* set it free */
186
		fp->_flags = 0;		/* set it free */
195
		errno = sverrno;	/* restore in case _close clobbered */
187
		errno = sverrno;	/* restore in case _close clobbered */
196
		FUNLOCKFILE(fp);
188
		FUNLOCKFILE(fp);
Lines 202-211 Link Here
202
	 * to maintain the descriptor.  Various C library routines (perror)
194
	 * to maintain the descriptor.  Various C library routines (perror)
203
	 * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
195
	 * assume stderr is always fd STDERR_FILENO, even if being freopen'd.
204
	 */
196
	 */
205
	if (wantfd >= 0 && f != wantfd) {
197
	if (wantfd >= 0) {
206
		if (_dup2(f, wantfd) >= 0) {
198
		if (_dup2(f, wantfd) >= 0) {
207
			(void)_close(f);
199
			(void)_close(f);
208
			f = wantfd;
200
			f = wantfd;
201
		} else {
202
			if (isopen)
203
				(void) (*fp->_close)(fp->_cookie);
209
		}
204
		}
210
	}
205
	}

Return to bug 79887