summaryrefslogtreecommitdiff
path: root/src/nonstd/__libc_start.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nonstd/__libc_start.c')
-rw-r--r--src/nonstd/__libc_start.c33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/nonstd/__libc_start.c b/src/nonstd/__libc_start.c
index e10e081e..c1cf7803 100644
--- a/src/nonstd/__libc_start.c
+++ b/src/nonstd/__libc_start.c
@@ -3,18 +3,37 @@
#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 sin, sout, serr;
+ 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);
- sin.fd = 0;
- stdin = &sin;
+ stderr = files + 2;
+ stderr->fd = 2;
+ freopen(NULL, "w", stderr);
+ setvbuf(stderr, NULL, _IONBF, 0);
- sout.fd = 1;
- stdout = &sout;
+ stdin->next = stdout;
+ stdout->next = stderr;
- serr.fd = 2;
- stderr = &serr;
+ stdout->prev = stdin;
+ stderr->prev = stdout;
#if defined _POSIX_SOURCE
setlocale(LC_ALL, "POSIX");