mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 09:47:41 +00:00
LibC: Partially implement 'freopen'
This commit is contained in:
parent
cb7526fca0
commit
9adbbff4dd
1 changed files with 32 additions and 4 deletions
|
@ -77,6 +77,8 @@ public:
|
||||||
pid_t popen_child() { return m_popen_child; }
|
pid_t popen_child() { return m_popen_child; }
|
||||||
void set_popen_child(pid_t child_pid) { m_popen_child = child_pid; }
|
void set_popen_child(pid_t child_pid) { m_popen_child = child_pid; }
|
||||||
|
|
||||||
|
void reopen(int fd, int mode);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Buffer {
|
struct Buffer {
|
||||||
// A ringbuffer that also transparently implements ungetc().
|
// A ringbuffer that also transparently implements ungetc().
|
||||||
|
@ -415,6 +417,23 @@ long FILE::tell()
|
||||||
return lseek(m_fd, 0, SEEK_CUR);
|
return lseek(m_fd, 0, SEEK_CUR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FILE::reopen(int fd, int mode)
|
||||||
|
{
|
||||||
|
// Dr. POSIX says: "Failure to flush or close the file descriptor
|
||||||
|
// successfully shall be ignored"
|
||||||
|
// and so we ignore any failures these two might have.
|
||||||
|
flush();
|
||||||
|
close();
|
||||||
|
|
||||||
|
// Just in case flush() and close() didn't drop the buffer.
|
||||||
|
m_buffer.drop();
|
||||||
|
|
||||||
|
m_fd = fd;
|
||||||
|
m_mode = mode;
|
||||||
|
m_error = 0;
|
||||||
|
m_eof = false;
|
||||||
|
}
|
||||||
|
|
||||||
FILE::Buffer::~Buffer()
|
FILE::Buffer::~Buffer()
|
||||||
{
|
{
|
||||||
if (m_data_is_malloced)
|
if (m_data_is_malloced)
|
||||||
|
@ -967,10 +986,19 @@ FILE* fopen(const char* pathname, const char* mode)
|
||||||
|
|
||||||
FILE* freopen(const char* pathname, const char* mode, FILE* stream)
|
FILE* freopen(const char* pathname, const char* mode, FILE* stream)
|
||||||
{
|
{
|
||||||
(void)pathname;
|
ASSERT(stream);
|
||||||
(void)mode;
|
if (!pathname) {
|
||||||
(void)stream;
|
// FIXME: Someone should probably implement this path.
|
||||||
ASSERT_NOT_REACHED();
|
TODO();
|
||||||
|
}
|
||||||
|
|
||||||
|
int flags = parse_mode(mode);
|
||||||
|
int fd = open(pathname, flags, 0666);
|
||||||
|
if (fd < 0)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
stream->reopen(fd, flags);
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fdopen(int fd, const char* mode)
|
FILE* fdopen(int fd, const char* mode)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue