142097Sbostic /*- 261180Sbostic * Copyright (c) 1990, 1993 361180Sbostic * The Regents of the University of California. All rights reserved. 442097Sbostic * 542097Sbostic * This code is derived from software contributed to Berkeley by 642097Sbostic * Chris Torek. 742097Sbostic * 842097Sbostic * %sccs.include.redist.c% 942097Sbostic */ 1042097Sbostic 1142097Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*67487Smckusick static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 07/03/94"; 1342097Sbostic #endif /* LIBC_SCCS and not lint */ 1442097Sbostic 1542097Sbostic #include <stddef.h> 1642097Sbostic #include <stdlib.h> 1742097Sbostic #include "atexit.h" 1842097Sbostic 19*67487Smckusick struct atexit *__atexit; /* points to head of LIFO stack */ 20*67487Smckusick 2142097Sbostic /* 2242097Sbostic * Register a function to be performed at exit. 2342097Sbostic */ 2442097Sbostic int 2542097Sbostic atexit(fn) 2642097Sbostic void (*fn)(); 2742097Sbostic { 2842097Sbostic static struct atexit __atexit0; /* one guaranteed table */ 2942097Sbostic register struct atexit *p; 3042097Sbostic 3142097Sbostic if ((p = __atexit) == NULL) 3242097Sbostic __atexit = p = &__atexit0; 3345581Sbostic else if (p->ind >= ATEXIT_SIZE) { 3442097Sbostic if ((p = malloc(sizeof(*p))) == NULL) 3542097Sbostic return (-1); 3645581Sbostic p->ind = 0; 3745581Sbostic p->next = __atexit; 3842097Sbostic __atexit = p; 3942097Sbostic } 4042097Sbostic p->fns[p->ind++] = fn; 4142097Sbostic return (0); 4242097Sbostic } 43