diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-11-08 01:23:23 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-11-08 01:23:47 +0100 |
commit | da3857b0c23a4e99edc2481c93c7f3bebda510cc (patch) | |
tree | e32c162d311a06f3678b28a967344b04c9f77ee0 /LibC/stdio.cpp | |
parent | e287f8ef3adc88112e1c2336adc4a27365058db8 (diff) | |
download | serenity-da3857b0c23a4e99edc2481c93c7f3bebda510cc.zip |
Add some simple write buffering to LibC's stdio.
Plumb it all the way to the VirtualConsole. Also fix /bin/cat to write()
the whole chunks we get from read() directly to stdout.
Diffstat (limited to 'LibC/stdio.cpp')
-rw-r--r-- | LibC/stdio.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/LibC/stdio.cpp b/LibC/stdio.cpp index c75f99d621..5c1409591f 100644 --- a/LibC/stdio.cpp +++ b/LibC/stdio.cpp @@ -28,7 +28,11 @@ int fflush(FILE* stream) // FIXME: Implement buffered streams, duh. if (!stream) return -EBADF; - return 0; + if (!stream->write_buffer_index) + return 0; + int rc = write(stream->fd, stream->write_buffer, stream->write_buffer_index); + stream->write_buffer_index = 0; + return rc; } char* fgets(char* buffer, int size, FILE* stream) @@ -71,7 +75,10 @@ int getchar() int fputc(int ch, FILE* stream) { assert(stream); - write(stream->fd, &ch, 1); + assert(stream->write_buffer_index < __STDIO_FILE_BUFFER_SIZE); + stream->write_buffer[stream->write_buffer_index++] = ch; + if (ch == '\n' || stream->write_buffer_index >= __STDIO_FILE_BUFFER_SIZE) + fflush(stream); if (stream->eof) return EOF; return (byte)ch; @@ -99,7 +106,7 @@ int fputs(const char* s, FILE* stream) int puts(const char* s) { - fputs(s, stdout); + return fputs(s, stdout); } void clearerr(FILE* stream) @@ -128,6 +135,7 @@ size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream) size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream) { assert(stream); + fflush(stream); ssize_t nwritten = write(stream->fd, ptr, nmemb * size); if (nwritten < 0) return 0; @@ -162,7 +170,7 @@ static void sys_putch(char*&, char ch) static FILE* __current_stream = nullptr; static void stream_putch(char*&, char ch) { - write(__current_stream->fd, &ch, 1); + fputc(ch, __current_stream); } int fprintf(FILE* fp, const char* fmt, ...) @@ -211,9 +219,8 @@ FILE* fopen(const char* pathname, const char* mode) if (fd < 0) return nullptr; auto* fp = (FILE*)malloc(sizeof(FILE)); + memset(fp, 0, sizeof(FILE)); fp->fd = fd; - fp->eof = false; - fp->error = 0; return fp; } @@ -223,9 +230,8 @@ FILE* fdopen(int fd, const char* mode) if (fd < 0) return nullptr; auto* fp = (FILE*)malloc(sizeof(FILE)); + memset(fp, 0, sizeof(FILE)); fp->fd = fd; - fp->eof = false; - fp->error = 0; return fp; } |