summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2024-05-28 12:51:10 -0400
committerJakob Kaivo <jkk@ung.org>2024-05-28 12:51:10 -0400
commit49c142c8d87681bfc979b0f2ad81df2d652bde07 (patch)
tree039ab55e247e7ce50089d5557692339b8df39d71
parentdb616c2d5cff8477336b318d889e02112fe97f9a (diff)
properly handler overriding std{in,out,err} on musl
-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)