Skip to content

Commit

Permalink
libc/stdio: Flush stdout on read from stdin
Browse files Browse the repository at this point in the history
Fix read from stdin to flush stdout. This happens in Linux (glibc) and
in traditional UNIX, although it was never guaranteed by any standard.

This behavior was added in UNIX because it was useful for programs which
print a prompt and read input - forgetting to fflush(stdout) in the meantime.
It was later done to Linux to run programs which assumed it, and for the
same reason we also want it in OSv.

Fixes #464.

Signed-off-by: Nadav Har'El <[email protected]>
Signed-off-by: Pekka Enberg <[email protected]>
  • Loading branch information
nyh authored and Pekka Enberg committed Aug 21, 2014
1 parent 24d0e2d commit 000ce9a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion libc/stdio/stdin.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#include "stdio_impl.h"

static size_t __stdin_read(FILE *fp, unsigned char *buf, size_t len)
{
// Flush stdout when reading from stdin - this is a long UNIX tradition
// and also happens in glibc. The latter flushes stdout if it is line-
// buffered whenever any line-buffered file is read - but the intended
// effect was on stdin.
if (stdout->lbf == '\n') {
fflush(stdout);
}
return __stdio_read(fp, buf, len);
}

static unsigned char buf[BUFSIZ+UNGET];
static FILE f = {
.buf = buf+UNGET,
.buf_size = sizeof buf-UNGET,
.fd = 0,
.flags = F_PERM | F_NOWR,
.read = __stdio_read,
.read = __stdin_read,
.seek = __stdio_seek,
.close = __stdio_close,
};
Expand Down

0 comments on commit 000ce9a

Please sign in to comment.