xref: /onnv-gate/usr/src/lib/libast/common/comp/atexit.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin /*
254887Schin  * ANSI C atexit()
264887Schin  * arrange for func to be called LIFO on exit()
274887Schin  */
284887Schin 
294887Schin #include <ast.h>
304887Schin 
314887Schin #if _lib_atexit
324887Schin 
334887Schin NoN(atexit)
344887Schin 
354887Schin #else
364887Schin 
374887Schin #if _lib_onexit || _lib_on_exit
384887Schin 
394887Schin #if !_lib_onexit
404887Schin #define onexit		on_exit
414887Schin #endif
424887Schin 
434887Schin extern int		onexit(void(*)(void));
444887Schin 
454887Schin int
464887Schin atexit(void (*func)(void))
474887Schin {
484887Schin 	return(onexit(func));
494887Schin }
504887Schin 
514887Schin #else
524887Schin 
534887Schin struct list
544887Schin {
554887Schin 	struct list*	next;
564887Schin 	void		(*func)(void);
574887Schin };
584887Schin 
594887Schin static struct list*	funclist;
604887Schin 
614887Schin extern void		_exit(int);
624887Schin 
634887Schin int
644887Schin atexit(void (*func)(void))
654887Schin {
664887Schin 	register struct list*	p;
674887Schin 
684887Schin 	if (!(p = newof(0, struct list, 1, 0))) return(-1);
694887Schin 	p->func = func;
704887Schin 	p->next = funclist;
714887Schin 	funclist = p;
724887Schin 	return(0);
734887Schin }
744887Schin 
754887Schin void
764887Schin _ast_atexit(void)
774887Schin {
784887Schin 	register struct list*	p;
794887Schin 
804887Schin 	while (p = funclist)
814887Schin 	{
824887Schin 		funclist = p->next;
834887Schin 		(*p->func)();
844887Schin 	}
854887Schin }
864887Schin 
874887Schin #if _std_cleanup
884887Schin 
894887Schin #if _lib__cleanup
904887Schin extern void		_cleanup(void);
914887Schin #endif
924887Schin 
934887Schin void
944887Schin exit(int code)
954887Schin {
964887Schin 	_ast_atexit();
974887Schin #if _lib__cleanup
984887Schin 	_cleanup();
994887Schin #endif
1004887Schin 	_exit(code);
1014887Schin }
1024887Schin 
1034887Schin #else
1044887Schin 
1054887Schin void
1064887Schin _cleanup(void)
1074887Schin {
1084887Schin 	_ast_atexit();
1094887Schin }
1104887Schin 
1114887Schin #endif
1124887Schin 
1134887Schin #endif
1144887Schin 
1154887Schin #endif
116