summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-15 20:50:51 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-15 20:50:51 -0400
commitc0b73d8cf2b022e479af74ac9c6068bd60a1a289 (patch)
treeacdd719e87dbcaf0df13ff79f0c8c69ae091e1d2
parentff77b77768631b55a8bef271d69c33c60e570de8 (diff)
implement actual buffering
-rw-r--r--src/stdio/putc_unlocked.c14
1 files 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;