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