summaryrefslogtreecommitdiff
path: root/src/stdlib/exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/exit.c')
-rw-r--r--src/stdlib/exit.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/stdlib/exit.c b/src/stdlib/exit.c
new file mode 100644
index 00000000..b0ba271d
--- /dev/null
+++ b/src/stdlib/exit.c
@@ -0,0 +1,51 @@
+#include <stdlib.h>
+#include "limits.h"
+#include "stddef.h"
+#include "nonstd/internal.h"
+#include "nonstd/syscall.h"
+
+/** cause normal program termination **/
+_Noreturn void exit(int status)
+{
+ struct atexit *ae = __libc(ATEXIT);
+
+ /* execute all atexit() registered functions in reverse order */
+ while (ae) {
+ int i = ae->nfns;
+ while (i > 0) {
+ ae->fns[--i]();
+ }
+ ae = ae->prev;
+ }
+
+ /* close all open files */
+ /*
+ while (__libc.stdio.lastfile) {
+ fclose(__libc.stdio.lastfile);
+ }
+ */
+
+ (void)status;
+ /* __syscall(__libc(SYSCALL_LOOKUP)("exit"), status); */
+ for (;;); /* quiet _Noreturn functions returns warning */
+}
+
+/***
+causes the program to terminate normally, returning the
+value ARGUMENT(status) to the host environment.
+
+First, all functions registered by FUNCTION(atexit) are called in the reverse
+order in which they were registered.
+
+Then, all open streams with unwritten buffered data are flushed. All open
+streams are closed. All temporary files created by FUNCTION(tmpfile) are
+removed.
+***/
+
+/*
+IMPLEMENTATION(The successful termination value returned to the host environment when ARGUMENT(status) is 0 or CONSTANT(EXIT_SUCESS))
+IMPLEMENTATION(The unsuccessful termination value returned to the host environment when ARGUMENT(status) is CONSTANT(EXIT_FAILURE))
+*/
+/*
+STDC(1)
+*/