commit cae77758d639ac4ec3883dfebfc48641adf3d358
parent 8587dc4a95607e21c0ca737f6e4eafc1e8b903a6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 1 Mar 2017 16:56:08 +0100
[libc] Add exit() and atexit()
Diffstat:
4 files changed, 36 insertions(+), 1 deletion(-)
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
@@ -8,6 +8,8 @@
#define NULL ((void *) 0)
#endif
+#define _ATEXIT_MAX 32
+
#define MB_CUR_MAX 1
#define RAND_MAX 32767
diff --git a/libc/src/Makefile b/libc/src/Makefile
@@ -10,7 +10,7 @@ LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \
isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \
isxdigit.o toupper.o tolower.o ctype.o setlocale.o \
- localeconv.o atoi.o
+ localeconv.o atoi.o atexit.o exit.o
all: libc.a
diff --git a/libc/src/atexit.c b/libc/src/atexit.c
@@ -0,0 +1,18 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdlib.h>
+
+extern void (*_atexitf[_ATEXIT_MAX])(void);
+
+int
+atexit(void (*fun)(void))
+{
+ void (**bp)(void);
+
+ for (bp = _atexitf; bp < &_atexitf[_ATEXIT_MAX] && *bp; ++bp)
+ /* nothing */;
+ if (bp == &_atexitf[_ATEXIT_MAX])
+ return 0;
+ *bp = fun;
+ return 1;
+}
diff --git a/libc/src/exit.c b/libc/src/exit.c
@@ -0,0 +1,15 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdlib.h>
+
+void (*_atexitf[_ATEXIT_MAX])(void);
+
+void
+exit(int status)
+{
+ void (**bp)(void);
+
+ for (bp = _atexitf; bp < &_atexitf[_ATEXIT_MAX] && *bp; ++bp)
+ (*bp)();
+ _Exit();
+}