xref: /csrg-svn/lib/libc/stdlib/atexit.c (revision 42097)
1*42097Sbostic /*-
2*42097Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*42097Sbostic  * All rights reserved.
4*42097Sbostic  *
5*42097Sbostic  * This code is derived from software contributed to Berkeley by
6*42097Sbostic  * Chris Torek.
7*42097Sbostic  *
8*42097Sbostic  * %sccs.include.redist.c%
9*42097Sbostic  */
10*42097Sbostic 
11*42097Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*42097Sbostic static char sccsid[] = "@(#)atexit.c	5.1 (Berkeley) 05/15/90";
13*42097Sbostic #endif /* LIBC_SCCS and not lint */
14*42097Sbostic 
15*42097Sbostic #include <stddef.h>
16*42097Sbostic #include <stdlib.h>
17*42097Sbostic #include "atexit.h"
18*42097Sbostic 
19*42097Sbostic /*
20*42097Sbostic  * Register a function to be performed at exit.
21*42097Sbostic  */
22*42097Sbostic int
23*42097Sbostic atexit(fn)
24*42097Sbostic 	void (*fn)();
25*42097Sbostic {
26*42097Sbostic 	static struct atexit __atexit0;	/* one guaranteed table */
27*42097Sbostic 	register struct atexit *p;
28*42097Sbostic 
29*42097Sbostic 	if ((p = __atexit) == NULL)
30*42097Sbostic 		__atexit = p = &__atexit0;
31*42097Sbostic 	if (p->ind >= ATEXIT_SIZE) {
32*42097Sbostic 		if ((p = malloc(sizeof(*p))) == NULL)
33*42097Sbostic 			return (-1);
34*42097Sbostic 		__atexit->next = p;
35*42097Sbostic 		__atexit = p;
36*42097Sbostic 	}
37*42097Sbostic 	p->fns[p->ind++] = fn;
38*42097Sbostic 	return (0);
39*42097Sbostic }
40