libc: Add freopen()
This commit is contained in:
parent
7efc3a6ea1
commit
acfad51ac0
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user