OSDN Git Service

Revert stdio to initializing itself. Not quite a pretty but that ensures that
[uclinux-h8/uClibc.git] / libc / stdlib / atexit.c
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  */
5
6 /*
7  * Manuel Novoa III       Dec 2000
8  *
9  * Modifications:
10  *   Made atexit handling conform to standards... i.e. no args.
11  *   Removed on_exit since it did not match gnu libc definition.
12  *   Combined atexit and __do_exit into one object file.
13  */
14
15 #include <stdlib.h>
16 #include <errno.h>
17
18 typedef void (*vfuncp) (void);
19 extern vfuncp __cleanup;
20
21 #ifdef L_atexit
22 static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
23 static int __atexit_count = 0;
24
25 static void __do_exit(void)
26 {
27         int count = __atexit_count - 1;
28
29         __atexit_count = -1;            /* ensure no more will be added */
30         __cleanup = 0;                          /* Calling exit won't re-do this */
31
32         /* In reverse order */
33         for (; count >= 0; count--) {
34                 (*__atexit_table[count])();
35         }
36 }
37
38 int atexit(vfuncp ptr)
39 {
40         if ((__atexit_count < 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
41                 errno = ENOMEM;
42                 return -1;
43         }
44         if (ptr) {
45                 __cleanup = __do_exit;
46                 __atexit_table[__atexit_count++] = ptr;
47         }
48         return 0;
49 }
50 #endif
51
52 #ifdef L_exit
53 vfuncp __cleanup = 0;
54
55 void exit(int rv)
56 {
57         if (__cleanup)
58                 __cleanup();
59         _exit(rv);
60 }
61 #endif