about summary refs log tree commit diff
path: root/src/stdio/__stdio_exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio/__stdio_exit.c')
-rw-r--r--src/stdio/__stdio_exit.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
new file mode 100644
index 00000000..3f87e7ed
--- /dev/null
+++ b/src/stdio/__stdio_exit.c
@@ -0,0 +1,23 @@
+#include "stdio_impl.h"
+
+static FILE *const dummy_file = 0;
+weak_alias(dummy_file, __stdin_used);
+weak_alias(dummy_file, __stdout_used);
+weak_alias(dummy_file, __stderr_used);
+
+static void close_file(FILE *f)
+{
+	if (!f) return;
+	FLOCK(f);
+	if (f->wpos > f->wbase) f->write(f, 0, 0);
+	if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
+}
+
+void __stdio_exit(void)
+{
+	FILE *f;
+	OFLLOCK();
+	for (f=libc.ofl_head; f; f=f->next) close_file(f);
+	close_file(__stdin_used);
+	close_file(__stdout_used);
+}