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