summaryrefslogtreecommitdiff
path: root/src/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio')
-rw-r--r--src/stdio/__stdio_exit.c23
-rw-r--r--src/stdio/__toread.c8
-rw-r--r--src/stdio/__towrite.c7
3 files changed, 35 insertions, 3 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);
+}
diff --git a/src/stdio/__toread.c b/src/stdio/__toread.c
index f00cc467..c2ae80fd 100644
--- a/src/stdio/__toread.c
+++ b/src/stdio/__toread.c
@@ -12,3 +12,11 @@ int __toread(FILE *f)
f->rpos = f->rend = f->buf;
return 0;
}
+
+static const int dummy = 0;
+weak_alias(dummy, __towrite_used);
+
+void __seek_on_exit()
+{
+ if (!__towrite_used) __stdio_exit();
+}
diff --git a/src/stdio/__towrite.c b/src/stdio/__towrite.c
index 4bf96f4d..ba67e0df 100644
--- a/src/stdio/__towrite.c
+++ b/src/stdio/__towrite.c
@@ -17,8 +17,9 @@ int __towrite(FILE *f)
return 0;
}
-/* Link flush-on-exit code iff any stdio write functions are linked. */
-void __fflush_on_exit()
+const int __towrite_used = 1;
+
+void __flush_on_exit()
{
- fflush(0);
+ __stdio_exit();
}