]> pd.if.org Git - pdclib/blob - functions/stdlib/atexit.c
Fallback to static array as pre-main malloc() created problems. (_start calls atexit...
[pdclib] / functions / stdlib / atexit.c
1 /* $Id$ */
2
3 /* Release $Name$ */
4
5 /* atexit( void (*)( void ) )
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 extern void (*_PDCLIB_regstack[])( void );
16 extern size_t _PDCLIB_regptr;
17
18 int atexit( void (*func)( void ) )
19 {
20     if ( _PDCLIB_regptr == 0 )
21     {
22         return -1;
23     }
24     else
25     {
26         _PDCLIB_regstack[ --_PDCLIB_regptr ] = func;
27         return 0;
28     }
29 }
30
31 #endif
32
33 #ifdef TEST
34 #include <_PDCLIB_test.h>
35 #include <assert.h>
36
37 static int flags[ 32 ];
38
39 static void counthandler()
40 {
41     static int rc = 0;
42     flags[ rc ] = rc;
43     ++rc;
44 }
45
46 static void checkhandler()
47 {
48     for ( int i = 0; i < 31; ++i )
49     {
50         assert( flags[ i ] == i );
51     }
52 }
53
54 int main()
55 {
56     BEGIN_TESTS;
57     TESTCASE( atexit( &checkhandler ) == 0 );
58     for ( int i = 0; i < 31; ++i )
59     {
60         TESTCASE( atexit( &counthandler ) == 0 );
61     }
62     return TEST_RESULTS;
63 }
64
65 #endif