summaryrefslogtreecommitdiff
path: root/src/stdlib/atexit.c
blob: e1d57df40569b29c76f321b8e823d55284d88278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <errno.h>
#include <stdlib.h>
#include "_stdlib.h"

/** register a function to run at program exit **/

int atexit(void (*func)(void))
{
	struct atexit *ae = &(__stdlib_h.atexit);

	SIGNAL_SAFE(0);

	while (ae->nfns == sizeof(ae->fns) / sizeof(ae->fns[0])) {
		if (ae->next == NULL) {
			ae->next = calloc(1, sizeof(*ae->next));
			if (ae->next == NULL) {
				#ifdef ENOMEM
				errno =	ENOMEM;
				#endif
				return 1;
			}
			ae->next->prev = ae;
		}
		ae = ae->next;
	}
	ae->fns[ae->nfns++] = func;

	return 0;
}

/***
registers the function ARGUMENT(func) to be called when the program
exits normally by calling FUNCTION(exit) or returning from FUNCTION(main). The
function must take no parameters and return no value.
***/

/*
IMPLEMENTATION(The number of registrations allowed (at least 32))
RETURN_FAILURE(NONZERO)
RETURN_SUCCESS(0)
STDC(1)
*/