diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index b3a066f6..b19f62da 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -273,13 +273,20 @@ extern "C" if ((flags = fopen_parse_mode(mode)) < 0) return nullptr; - close(stream->_fd); + fflush(stream); - s_open_files[stream->_fd] = nullptr; + if (!path) + { + // FIXME: No mode changes are permitted. + errno = EBADF; + return nullptr; + } if (stream->_buf.buffer && (stream->_buf.status & FileStatusFlags::BufferIsMalloced)) free(stream->_buf.buffer); - if (!path) { fail("FIXME: freopen() called with path=nullptr"); } + close(stream->_fd); + + s_open_files[stream->_fd] = nullptr; int fd = open(path, flags, 0666); if (fd < 0) { return nullptr; }