]> pd.if.org Git - pdclib/blob - functions/stdlib/exit.c
Fallback to static array as pre-main malloc() created problems. (_start calls atexit...
[pdclib] / functions / stdlib / exit.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* exit( int )
6
7    This file is part of the Public Domain C Library (PDCLib).
8    Permission is granted to use, modify, and / or redistribute at will.
9 */
10
11 #include <stdlib.h>
12
13 #ifndef REGTEST
14
15 /* TODO - "except that a function is called after any previously registered
16    functions that had already been called at the time it was registered.
17 */
18
19 /* TODO: 32 is guaranteed. This should be dynamic but ATM gives problems
20    with my malloc.
21 */
22 #define NUMBER_OF_SLOTS 40
23
24 void (*_PDCLIB_regstack[ NUMBER_OF_SLOTS ])( void );
25 size_t _PDCLIB_regptr = NUMBER_OF_SLOTS;
26
27 void exit( int status )
28 {
29     while ( _PDCLIB_regptr < NUMBER_OF_SLOTS )
30     {
31         _PDCLIB_regstack[ _PDCLIB_regptr++ ]();
32     }
33     _Exit( status );
34 }
35
36 #endif
37
38 #ifdef TEST
39 #include <_PDCLIB_test.h>
40
41 int main()
42 {
43     BEGIN_TESTS;
44     /* Unwinding of regstack tested in atexit(). */
45     return TEST_RESULTS;
46 }
47
48 #endif