aboutsummaryrefslogtreecommitdiffstats
path: root/src/pdclib/functions/stdlib/atexit.c
blob: 489c3df6288c50992ca9eb562e97df7ad46c8899 (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
/* atexit( void (*)( void ) )

   This file is part of the Public Domain C Library (PDCLib).
   Permission is granted to use, modify, and / or redistribute at will.
*/

#include <stdlib.h>

#ifndef REGTEST

extern void (*_PDCLIB_exitstack[])( void );
extern size_t _PDCLIB_exitptr;

int atexit( void (*func)( void ) )
{
    if ( _PDCLIB_exitptr == 0 )
    {
        return -1;
    }
    else
    {
        _PDCLIB_exitstack[ --_PDCLIB_exitptr ] = func;
        return 0;
    }
}

#endif

#ifdef TEST

#include "_PDCLIB_test.h"

#include <assert.h>

static int flags[ 32 ];

static void counthandler( void )
{
    static int count = 0;
    flags[ count ] = count;
    ++count;
}

static void checkhandler( void )
{
    int i;
    for ( i = 0; i < 31; ++i )
    {
        assert( flags[ i ] == i );
    }
}

int main( void )
{
    int i;
    TESTCASE( atexit( &checkhandler ) == 0 );
    for ( i = 0; i < 31; ++i )
    {
        TESTCASE( atexit( &counthandler ) == 0 );
    }
    return TEST_RESULTS;
}

#endif