summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-02-28 16:47:30 -0500
committerJakob Kaivo <jkk@ung.org>2019-02-28 16:47:30 -0500
commit50fc4e08328a13ad37810ebaf1fe6c71c876c4df (patch)
tree62f15595f66565455f7fe2cb5c4ec365eb4b2cbd /src
parent4b93a94c0aab8c93a0f04ba0ab5061c7300de5fc (diff)
use freopen() and setvbuf() to properly set up stdin/stdout/stderr
Diffstat (limited to 'src')
-rw-r--r--src/nonstd/__libc_start.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/nonstd/__libc_start.c b/src/nonstd/__libc_start.c
index f9fadfd5..c1cf7803 100644
--- a/src/nonstd/__libc_start.c
+++ b/src/nonstd/__libc_start.c
@@ -3,18 +3,31 @@
#include "nonstd/io.h"
+#ifdef _POSIX_SOURCE
+#include "unistd.h"
+#else
+#include "nonstd/syscall.h"
+#define isatty(fd) __syscall(__lookup("tty"), fd)
+#endif
+
void __libc_start(int argc, char **argv)
{
struct __FILE *files = __libc(FILE_STREAMS);
stdin = files + 0;
stdin->fd = 0;
+ freopen(NULL, "r", stdin);
+ setvbuf(stdin, NULL, isatty(0) ? _IOLBF : _IOFBF, BUFSIZ);
stdout = files + 1;
stdout->fd = 1;
+ freopen(NULL, "w", stdout);
+ setvbuf(stdin, NULL, isatty(1) ? _IOLBF : _IOFBF, BUFSIZ);
stderr = files + 2;
stderr->fd = 2;
+ freopen(NULL, "w", stderr);
+ setvbuf(stderr, NULL, _IONBF, 0);
stdin->next = stdout;
stdout->next = stderr;