shithub: scc

Download patch

ref: 11845819cdd4384de7c96ccc63d97b9e35d6610c
parent: de09cc394bc133530b1e14bcb745eb48dc261829
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Mar 1 11:56:08 EST 2017

[libc] Add exit() and atexit()

--- 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
 
--- a/libc/src/Makefile
+++ b/libc/src/Makefile
@@ -10,7 +10,7 @@
           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
 
--- /dev/null
+++ b/libc/src/atexit.c
@@ -1,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;
+}
--- /dev/null
+++ b/libc/src/exit.c
@@ -1,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();
+}