#include "ansidecl.h"
#include "libiberty.h"
#include <stdio.h>
#ifdef __STDC__
#include <stddef.h>
#else
#define size_t unsigned long
#endif
#if VMS
#include <stdlib.h>
#include <unixlib.h>
#else
PTR malloc PARAMS ((size_t));
#endif
static void xatexit_cleanup PARAMS ((void));
extern void (*_xexit_cleanup) PARAMS ((void));
#define XATEXIT_SIZE 32
struct xatexit {
struct xatexit *next;
int ind;
void (*fns[XATEXIT_SIZE]) PARAMS ((void));
};
static struct xatexit xatexit_first;
static struct xatexit *xatexit_head = &xatexit_first;
int
xatexit (fn)
void (*fn) PARAMS ((void));
{
register struct xatexit *p;
if (!_xexit_cleanup)
_xexit_cleanup = xatexit_cleanup;
p = xatexit_head;
if (p->ind >= XATEXIT_SIZE)
{
if ((p = (struct xatexit *) malloc (sizeof *p)) == NULL)
return -1;
p->ind = 0;
p->next = xatexit_head;
xatexit_head = p;
}
p->fns[p->ind++] = fn;
return 0;
}
static void
xatexit_cleanup ()
{
register struct xatexit *p;
register int n;
for (p = xatexit_head; p; p = p->next)
for (n = p->ind; --n >= 0;)
(*p->fns[n]) ();
}