xref: /titanic_44/usr/src/lib/libast/common/comp/atexit.c (revision c6fc6ddd482fa5c17788a38bd238258f2c22597c)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * ANSI C atexit()
26  * arrange for func to be called LIFO on exit()
27  */
28 
29 #include <ast.h>
30 
31 #if _lib_atexit
32 
33 NoN(atexit)
34 
35 #else
36 
37 #if _lib_onexit || _lib_on_exit
38 
39 #if !_lib_onexit
40 #define onexit		on_exit
41 #endif
42 
43 extern int		onexit(void(*)(void));
44 
45 int
46 atexit(void (*func)(void))
47 {
48 	return(onexit(func));
49 }
50 
51 #else
52 
53 struct list
54 {
55 	struct list*	next;
56 	void		(*func)(void);
57 };
58 
59 static struct list*	funclist;
60 
61 extern void		_exit(int);
62 
63 int
64 atexit(void (*func)(void))
65 {
66 	register struct list*	p;
67 
68 	if (!(p = newof(0, struct list, 1, 0))) return(-1);
69 	p->func = func;
70 	p->next = funclist;
71 	funclist = p;
72 	return(0);
73 }
74 
75 void
76 _ast_atexit(void)
77 {
78 	register struct list*	p;
79 
80 	while (p = funclist)
81 	{
82 		funclist = p->next;
83 		(*p->func)();
84 	}
85 }
86 
87 #if _std_cleanup
88 
89 #if _lib__cleanup
90 extern void		_cleanup(void);
91 #endif
92 
93 void
94 exit(int code)
95 {
96 	_ast_atexit();
97 #if _lib__cleanup
98 	_cleanup();
99 #endif
100 	_exit(code);
101 }
102 
103 #else
104 
105 void
106 _cleanup(void)
107 {
108 	_ast_atexit();
109 }
110 
111 #endif
112 
113 #endif
114 
115 #endif
116