aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdio/freopen.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/stdio/freopen.c')
-rw-r--r--lib/libc/stdio/freopen.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c
index 0ff83bf96175..4dcd50fbb13d 100644
--- a/lib/libc/stdio/freopen.c
+++ b/lib/libc/stdio/freopen.c
@@ -97,7 +97,7 @@ freopen(const char * __restrict file, const char * __restrict mode,
(dflags & (O_ACCMODE | O_EXEC)) != (oflags & O_ACCMODE)) {
fclose(fp);
FUNLOCKFILE(fp);
- errno = EINVAL;
+ errno = EBADF;
return (NULL);
}
if (fp->_flags & __SWR)
@@ -151,6 +151,14 @@ freopen(const char * __restrict file, const char * __restrict mode,
/* Get a new descriptor to refer to the new file. */
f = _open(file, oflags, DEFFILEMODE);
+ /* If out of fd's close the old one and try again. */
+ if (f < 0 && isopen && wantfd > STDERR_FILENO &&
+ (errno == ENFILE || errno == EMFILE)) {
+ (void) (*fp->_close)(fp->_cookie);
+ isopen = 0;
+ wantfd = -1;
+ f = _open(file, oflags, DEFFILEMODE);
+ }
sverrno = errno;
finish: