libc: Implement freopen()

This commit is contained in:
apio 2022-10-19 17:32:59 +02:00
parent f3af3e252b
commit aa90e4a8d9
2 changed files with 17 additions and 0 deletions

View File

@ -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);

View File

@ -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;