* tiny
[mascara-docs.git] / compilers / bcc / linux86-0.16.17 / libc / misc / atexit.c
blob6e8e45b9554b3a251c487b49391042edaf765e0f
1 /* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
2 * This file is part of the Linux-8086 C library and is distributed
3 * under the GNU Library General Public License.
4 */
6 /*
7 * This deals with both the atexit and on_exit function calls
8 *
9 * Note calls installed with atexit are called with the same args as on_exit
10 * fuctions; the void* is given the NULL value.
14 #include <errno.h>
16 /* ATEXIT.H */
17 #define MAXONEXIT 20 /* AIUI Posix requires 10 */
19 typedef void (*vfuncp) ();
21 extern vfuncp __cleanup;
22 extern void __do_exit();
24 extern struct exit_table
26 vfuncp called;
27 void *argument;
29 __on_exit_table[MAXONEXIT];
31 extern int __on_exit_count;
33 /* End ATEXIT.H */
35 #ifdef L_atexit
36 int
37 atexit(ptr)
38 vfuncp ptr;
40 if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT)
42 errno = ENOMEM;
43 return -1;
45 __cleanup = __do_exit;
46 if( ptr )
48 __on_exit_table[__on_exit_count].called = ptr;
49 __on_exit_table[__on_exit_count].argument = 0;
50 __on_exit_count++;
52 return 0;
55 #endif
57 #ifdef L_on_exit
58 int
59 on_exit(ptr, arg)
60 vfuncp ptr;
61 void *arg;
63 if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT)
65 errno = ENOMEM;
66 return -1;
68 __cleanup = __do_exit;
69 if( ptr )
71 __on_exit_table[__on_exit_count].called = ptr;
72 __on_exit_table[__on_exit_count].argument = arg;
73 __on_exit_count++;
75 return 0;
78 #endif
80 #ifdef L___do_exit
82 int __on_exit_count = 0;
83 struct exit_table __on_exit_table[MAXONEXIT];
85 void
86 __do_exit(rv)
87 int rv;
89 register int count = __on_exit_count-1;
90 register vfuncp ptr;
91 __on_exit_count = -1; /* ensure no more will be added */
92 __cleanup = 0; /* Calling exit won't re-do this */
94 /* In reverse order */
95 for (; count >= 0; count--)
97 ptr = __on_exit_table[count].called;
98 (*ptr) (rv, __on_exit_table[count].argument);
102 #endif