From c0b73d8cf2b022e479af74ac9c6068bd60a1a289 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 15 Aug 2020 20:50:51 -0400 Subject: implement actual buffering --- src/stdio/putc_unlocked.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/stdio/putc_unlocked.c b/src/stdio/putc_unlocked.c index 61ad6b03..1e031e40 100644 --- a/src/stdio/putc_unlocked.c +++ b/src/stdio/putc_unlocked.c @@ -11,15 +11,21 @@ #endif /** write a character to a file stream with explicit client locking **/ + int putc_unlocked(int c, FILE *stream) { unsigned char ch = (unsigned char)c; ASSERT_NONNULL(stream); - - if (write(stream->fd, &ch, sizeof(ch)) != 1) { - /* error */ - return EOF; + + stream->buf[stream->bpos++] = ch; + if (stream->bpos == stream->bsize || + (stream->bmode == _IOLBF && ch == '\n') || + (stream->bmode == _IONBF)) { + if (write(stream->fd, stream->buf, stream->bpos) != 1) { + /* errno handled by write() */ + return EOF; + } } return ch; -- cgit v1.2.1