summaryrefslogtreecommitdiff
path: root/src/stdlib/at_quick_exit.c
blob: 6a6db796c808a3b8787bd3446d843e80b63254c9 (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
43
44
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include "_stdlib.h"

/** register a function to run at quick exit **/
int at_quick_exit(void (*func)(void))
{
	SIGNAL_SAFE(0);

	struct atexit *ae = &(__stdlib.at_quick_exit);
	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;
}

/***
The fn(at_quick_exit) registers the function arg(func) to be called when the
program exits via fn(quick_exit). The function must take no parameters.
***/

/* UNSPECIFIED: - */
/* UNDEFINED: - */
/* IMPLEMENTATION: the number of registrations allowed (at least 32) */
/* LOCALE: - */

/* RETURN(0): success */
/* RETURN(NZ): failure */
/*
STDC(201112)
*/