diff --git a/libs/libc/include/stdio.h b/libs/libc/include/stdio.h index 77707434..268b0dba 100644 --- a/libs/libc/include/stdio.h +++ b/libs/libc/include/stdio.h @@ -42,6 +42,9 @@ extern "C" /* Returns a new file associated with the file descriptor fd. */ FILE* fdopen(int fd, const char* mode); + /* Opens the file specified by pathname and points the file handle stream to it. */ + FILE* freopen(const char* pathname, const char* mode, FILE* stream); + /* Returns the file descriptor associated with the file stream. */ int fileno(FILE* stream); diff --git a/libs/libc/src/file.cpp b/libs/libc/src/file.cpp index aeda988f..6dc51cfa 100644 --- a/libs/libc/src/file.cpp +++ b/libs/libc/src/file.cpp @@ -51,6 +51,20 @@ extern "C" return stream; } + FILE* freopen(const char* pathname, const char* mode, + FILE* stream) // FIXME: If pathname is NULL, open the original file with the new mode. + { + int fd = open(pathname, O_RDWR); // FIXME: Use the mode string. + if (fd < 0) { return 0; } + + fflush(stream); // To make it future-proof. + fclose(stream); + + stream->f_fd = fd; + clearerr(stream); + return stream; + } + int fileno(FILE* stream) { return stream->f_fd;