summaryrefslogtreecommitdiff
path: root/libc/stdlib/atexit.c
blob: a7ec0fb1fa985e8d7590645abbcc57b265dbde70 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
 * This file is part of the Linux-8086 C library and is distributed
 * under the GNU Library General Public License.
 */

/*
 * Manuel Novoa III       Dec 2000
 *
 * Modifications:
 *   Made atexit handling conform to standards... i.e. no args.
 *   Removed on_exit since it did not match gnu libc definition.
 *   Combined atexit and __do_exit into one object file.
 */

#include <stdlib.h>
#include <errno.h>

typedef void (*vfuncp) (void);
extern vfuncp __cleanup;

#ifdef L_atexit
static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
static int __atexit_count = 0;

static void __do_exit(void)
{
	int count = __atexit_count - 1;

	__atexit_count = -1;		/* ensure no more will be added */
	__cleanup = 0;				/* Calling exit won't re-do this */

	/* In reverse order */
	for (; count >= 0; count--) {
		(*__atexit_table[count])();
	}
}

int atexit(vfuncp ptr)
{
	if ((__atexit_count < 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
		errno = ENOMEM;
		return -1;
	}
	if (ptr) {
		__cleanup = __do_exit;
		__atexit_table[__atexit_count++] = ptr;
	}
	return 0;
}
#endif

#ifdef L_exit
void __stdio_close_all(void);	/* note: see _start.S - could be faked */

vfuncp __cleanup = 0;

void exit(int rv)
{
	if (__cleanup)
		__cleanup();
	__stdio_close_all();
	_exit(rv);
}
#endif