]> pd.if.org Git - pdclib/blob - functions/stdlib/atexit.c
Porting current working set from CVS.
[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( void )
40 {
41     static int count = 0;
42     flags[ count ] = count;
43     ++count;
44 }
45
46 static void checkhandler( void )
47 {
48     for ( int i = 0; i < 31; ++i )
49     {
50         assert( flags[ i ] == i );
51     }
52 }
53
54 int main( void )
55 {
56     TESTCASE( atexit( &checkhandler ) == 0 );
57     for ( int i = 0; i < 31; ++i )
58     {
59         TESTCASE( atexit( &counthandler ) == 0 );
60     }
61     return TEST_RESULTS;
62 }
63
64 #endif