From 4b90740d3bdc090c6d86c9d7a94de3d32c8abc35 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sat, 29 May 2021 16:45:35 +0200 Subject: [PATCH] stdlib: add exit() based on target-provided _Exit() This is implemented for gint only currently; on Vhex, _Exit() is likely just going to be a syscall. For CASIOWIN, this is slightly more difficult, as there is no native exit syscall. --- CMakeLists.txt | 1 + STATUS | 7 ++++++- include/stdlib.h | 9 +++++++++ include/target/casiowin-cg/bits/exit.h | 8 ++++++++ include/target/casiowin-fx/bits/exit.h | 8 ++++++++ include/target/gint/bits/exit.h | 8 ++++++++ include/target/vhex-generic/bits/exit.h | 7 +++++++ src/libc/stdlib/abort.c | 10 ++++++++++ src/libc/stdlib/exit.c | 11 +++++++++++ 9 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 include/target/casiowin-cg/bits/exit.h create mode 100644 include/target/casiowin-fx/bits/exit.h create mode 100644 include/target/gint/bits/exit.h create mode 100644 include/target/vhex-generic/bits/exit.h create mode 100644 src/libc/stdlib/abort.c create mode 100644 src/libc/stdlib/exit.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e5ed38..488606d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ set(SOURCES src/libc/stdlib/atoll.c src/libc/stdlib/calloc.c src/libc/stdlib/div.c + src/libc/stdlib/exit.c src/libc/stdlib/labs.c src/libc/stdlib/ldiv.c src/libc/stdlib/llabs.c diff --git a/STATUS b/STATUS index 6109988..4a7db56 100644 --- a/STATUS +++ b/STATUS @@ -102,7 +102,12 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested 7.20.1.4 strtol, strtoul, strtoll, strtoull: DONE ! 7.20.2 Pseudo-random sequence generation functions: TODO ! 7.20.3 Memory management functions: TODO (check existing code first) -! 7.20.4 Communication with the environment: TODO +! 7.20.4.1 abort: BDEPS(raise) +! 7.20.4.2 atexit: TODO + 7.20.4.3 exit: DONE (missing stream flushing/closing/etc) + 7.20.4.4 _Exit: DONE (gint only) +! 7.20.4.5 getenv: TODO +! 7.20.4.6 system: TODO ! 7.20.5 Searching and sorting utilities: TODO 7.20.6.1 abs, labs, llabs: DONE 7.20.6.2 div, ldiv, lldiv: DONE diff --git a/include/stdlib.h b/include/stdlib.h index 37ad1c8..d13e30b 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -3,6 +3,7 @@ #include #include +#include /* Dynamic memory management. */ @@ -27,6 +28,14 @@ extern void *reallocarray(void *__ptr, size_t __nmemb, size_t __size); /* Free a block allocated by `malloc', `realloc' or `calloc'. */ extern void free(void *__ptr); +/* Communication with the environment. */ + +/* Exit; calls handlers, flushes and closes streams and temporary files. */ +void exit(int __status); + +/* Exit immediately, bypassing exit handlers or signal handlers. */ +void _Exit(int __status); + /* Integer arithmetic functions. */ extern int abs(int __j); diff --git a/include/target/casiowin-cg/bits/exit.h b/include/target/casiowin-cg/bits/exit.h new file mode 100644 index 0000000..30e4207 --- /dev/null +++ b/include/target/casiowin-cg/bits/exit.h @@ -0,0 +1,8 @@ +#ifndef __BITS_EXIT_H__ +# define __BITS_EXIT_H__ + +/* Exit codes for CASIOWIN add-ins. */ +#define EXIT_SUCCESS 1 +#define EXIT_FAILURE 0 + +#endif /*__BITS_EXIT_H__*/ diff --git a/include/target/casiowin-fx/bits/exit.h b/include/target/casiowin-fx/bits/exit.h new file mode 100644 index 0000000..30e4207 --- /dev/null +++ b/include/target/casiowin-fx/bits/exit.h @@ -0,0 +1,8 @@ +#ifndef __BITS_EXIT_H__ +# define __BITS_EXIT_H__ + +/* Exit codes for CASIOWIN add-ins. */ +#define EXIT_SUCCESS 1 +#define EXIT_FAILURE 0 + +#endif /*__BITS_EXIT_H__*/ diff --git a/include/target/gint/bits/exit.h b/include/target/gint/bits/exit.h new file mode 100644 index 0000000..30e4207 --- /dev/null +++ b/include/target/gint/bits/exit.h @@ -0,0 +1,8 @@ +#ifndef __BITS_EXIT_H__ +# define __BITS_EXIT_H__ + +/* Exit codes for CASIOWIN add-ins. */ +#define EXIT_SUCCESS 1 +#define EXIT_FAILURE 0 + +#endif /*__BITS_EXIT_H__*/ diff --git a/include/target/vhex-generic/bits/exit.h b/include/target/vhex-generic/bits/exit.h new file mode 100644 index 0000000..f2e1124 --- /dev/null +++ b/include/target/vhex-generic/bits/exit.h @@ -0,0 +1,7 @@ +#ifndef __BITS_EXIT_H__ +# define __BITS_EXIT_H__ + +#define EXIT_SUCCESS 0 +#define EXIT_FAILURE 1 + +#endif /*__BITS_EXIT_H__*/ diff --git a/src/libc/stdlib/abort.c b/src/libc/stdlib/abort.c new file mode 100644 index 0000000..2191df9 --- /dev/null +++ b/src/libc/stdlib/abort.c @@ -0,0 +1,10 @@ +#include +#include + +void abort(int rc) +{ + /* TODO: Close BFile handles (essential) */ + + raise(SIGABRT); + _Exit(EXIT_FAILURE); +} diff --git a/src/libc/stdlib/exit.c b/src/libc/stdlib/exit.c new file mode 100644 index 0000000..596a48a --- /dev/null +++ b/src/libc/stdlib/exit.c @@ -0,0 +1,11 @@ +#include + +void exit(int rc) +{ + /* TODO: invoke atexit callbacks */ + /* TODO: exit: Flush all streams */ + /* TODO: exit: Close all streams */ + /* TODO: exit: Remove temporary files */ + + _Exit(rc); +}