xref: /csrg-svn/lib/libc/stdlib/atexit.c (revision 45581)
142097Sbostic /*-
242097Sbostic  * Copyright (c) 1990 The Regents of the University of California.
342097Sbostic  * All rights reserved.
442097Sbostic  *
542097Sbostic  * This code is derived from software contributed to Berkeley by
642097Sbostic  * Chris Torek.
742097Sbostic  *
842097Sbostic  * %sccs.include.redist.c%
942097Sbostic  */
1042097Sbostic 
1142097Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*45581Sbostic static char sccsid[] = "@(#)atexit.c	5.2 (Berkeley) 11/14/90";
1342097Sbostic #endif /* LIBC_SCCS and not lint */
1442097Sbostic 
1542097Sbostic #include <stddef.h>
1642097Sbostic #include <stdlib.h>
1742097Sbostic #include "atexit.h"
1842097Sbostic 
1942097Sbostic /*
2042097Sbostic  * Register a function to be performed at exit.
2142097Sbostic  */
2242097Sbostic int
2342097Sbostic atexit(fn)
2442097Sbostic 	void (*fn)();
2542097Sbostic {
2642097Sbostic 	static struct atexit __atexit0;	/* one guaranteed table */
2742097Sbostic 	register struct atexit *p;
2842097Sbostic 
2942097Sbostic 	if ((p = __atexit) == NULL)
3042097Sbostic 		__atexit = p = &__atexit0;
31*45581Sbostic 	else if (p->ind >= ATEXIT_SIZE) {
3242097Sbostic 		if ((p = malloc(sizeof(*p))) == NULL)
3342097Sbostic 			return (-1);
34*45581Sbostic 		p->ind = 0;
35*45581Sbostic 		p->next = __atexit;
3642097Sbostic 		__atexit = p;
3742097Sbostic 	}
3842097Sbostic 	p->fns[p->ind++] = fn;
3942097Sbostic 	return (0);
4042097Sbostic }
41