libc: Add freopen()

This commit is contained in:
apio 2023-06-19 10:46:08 +02:00
parent 7efc3a6ea1
commit acfad51ac0
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 22 additions and 0 deletions

View File

@ -41,6 +41,9 @@ extern "C"
/* Bind a stream to a file descriptor. */
FILE* fdopen(int fd, const char* mode);
/* Change the underlying file and mode of a stream. */
FILE* freopen(const char* path, const char* mode, FILE* stream);
/* Close a file and frees up its stream. */
int fclose(FILE* stream);

View File

@ -85,6 +85,25 @@ extern "C"
return f;
}
FILE* freopen(const char* path, const char* mode, FILE* stream)
{
int flags;
if ((flags = fopen_parse_mode(mode)) < 0) return nullptr;
close(stream->_fd);
if (!path) { fail("FIXME: freopen() called with path=nullptr"); }
int fd = open(path, flags, 0666);
if (fd < 0) { return nullptr; }
stream->_fd = fd;
clearerr(stream);
return stream;
}
int fclose(FILE* stream)
{
if (close(stream->_fd) < 0) return EOF;