atexit.c (fab94ac15b4c228ccbbb46f5454543078726f54e) atexit.c (662909a7800d5634772b89ca1509765dda837508)
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 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
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 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)atexit.c 8.1 (Berkeley) 6/4/93";
38static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
39#endif /* LIBC_SCCS and not lint */
40
41#include <stddef.h>
42#include <stdlib.h>
39#endif /* LIBC_SCCS and not lint */
40
41#include <stddef.h>
42#include <stdlib.h>
43#include <unistd.h>
44#include "atexit.h"
45
43#include "atexit.h"
44
45struct atexit *__atexit; /* points to head of LIFO stack */
46
46/*
47 * Register a function to be performed at exit.
48 */
49int
50atexit(fn)
51 void (*fn)();
52{
53 static struct atexit __atexit0; /* one guaranteed table */
54 register struct atexit *p;
55
56 if ((p = __atexit) == NULL)
57 __atexit = p = &__atexit0;
58 else if (p->ind >= ATEXIT_SIZE) {
47/*
48 * Register a function to be performed at exit.
49 */
50int
51atexit(fn)
52 void (*fn)();
53{
54 static struct atexit __atexit0; /* one guaranteed table */
55 register struct atexit *p;
56
57 if ((p = __atexit) == NULL)
58 __atexit = p = &__atexit0;
59 else if (p->ind >= ATEXIT_SIZE) {
59 if ((p = (struct atexit *)sbrk(sizeof(*p))) == (struct atexit *)-1)
60 if ((p = malloc(sizeof(*p))) == NULL)
60 return (-1);
61 p->ind = 0;
62 p->next = __atexit;
63 __atexit = p;
64 }
65 p->fns[p->ind++] = fn;
66 return (0);
67}
61 return (-1);
62 p->ind = 0;
63 p->next = __atexit;
64 __atexit = p;
65 }
66 p->fns[p->ind++] = fn;
67 return (0);
68}