summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/__main.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/__main.c b/src/__main.c
index 6247ccdc..aac8317c 100644
--- a/src/__main.c
+++ b/src/__main.c
@@ -4,6 +4,15 @@
#include "stdio/_stdio.h"
#include "stdlib/_stdlib.h"
+#undef stdin
+#undef stdout
+#undef stderr
+
+/* works on musl */
+extern FILE *stdin;
+extern FILE *stdout;
+extern FILE *stderr;
+
__attribute__((constructor))
void __init_libc(void)
{
@@ -13,18 +22,18 @@ void __init_libc(void)
}
init = 1;
- stdin = __stdio.FILES + 0;
- stdin->fd = 0;
- freopen(NULL, "r", stdin);
+ __stdin = stdin ? stdin : __stdio.FILES + 0;
+ __stdin->fd = 0;
+ freopen(NULL, "r", __stdin);
- stdout = __stdio.FILES + 1;
- stdout->fd = 1;
- freopen(NULL, "w", stdout);
+ __stdout = stdout ? stdout : __stdio.FILES + 1;
+ __stdout->fd = 1;
+ freopen(NULL, "w", __stdout);
- stderr = __stdio.FILES + 2;
- stderr->fd = 2;
- freopen(NULL, "w", stderr);
- setvbuf(stderr, NULL, _IONBF, 0);
+ __stderr = stderr ? stderr : __stdio.FILES + 2;
+ __stderr->fd = 2;
+ freopen(NULL, "w", __stderr);
+ setvbuf(__stderr, NULL, _IONBF, 0);
}
void __main(int (*main)(int, char*[]), int argc, char **argv)