update from Mike Karels at BSDI
[unix-history] / usr / src / lib / libc / stdlib / atexit.c
CommitLineData
b2c7b7fc
KB
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)
6d0ee557 12static char sccsid[] = "@(#)atexit.c 5.2 (Berkeley) %G%";
b2c7b7fc
KB
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 */
22int
23atexit(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;
6d0ee557 31 else if (p->ind >= ATEXIT_SIZE) {
b2c7b7fc
KB
32 if ((p = malloc(sizeof(*p))) == NULL)
33 return (-1);
6d0ee557
KB
34 p->ind = 0;
35 p->next = __atexit;
b2c7b7fc
KB
36 __atexit = p;
37 }
38 p->fns[p->ind++] = fn;
39 return (0);
40}