From 903dcfa52cc8b73703dcf4e970cb417613c2009e Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 2 Jul 2024 12:44:43 +0200 Subject: [PATCH] libc: Partially implement freopen() when a null pathname is provided The specification says "It is implementation-defined which changes of mode are permitted (if any)", so we can comply by not permitting any mode changes, at least for now. --- libc/src/stdio.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) 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; }