xref: /original-bsd/lib/libc/stdlib/atexit.c (revision 0edb85a7)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * 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	5.1 (Berkeley) 05/15/90";
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 	if (p->ind >= ATEXIT_SIZE) {
32 		if ((p = malloc(sizeof(*p))) == NULL)
33 			return (-1);
34 		__atexit->next = p;
35 		__atexit = p;
36 	}
37 	p->fns[p->ind++] = fn;
38 	return (0);
39 }
40