xref: /original-bsd/lib/libc/stdlib/atexit.c (revision c3e32dec)
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.1 (Berkeley) 06/04/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include "atexit.h"
18 
19 /*
20  * Register a function to be performed at exit.
21  */
22 int
23 atexit(fn)
24 	void (*fn)();
25 {
26 	static struct atexit __atexit0;	/* one guaranteed table */
27 	register struct atexit *p;
28 
29 	if ((p = __atexit) == NULL)
30 		__atexit = p = &__atexit0;
31 	else if (p->ind >= ATEXIT_SIZE) {
32 		if ((p = malloc(sizeof(*p))) == NULL)
33 			return (-1);
34 		p->ind = 0;
35 		p->next = __atexit;
36 		__atexit = p;
37 	}
38 	p->fns[p->ind++] = fn;
39 	return (0);
40 }
41