
/* atexit.c */

#include <stdlib.h>

#include "atexit.h"

int
atexit (void (*func) (void))
{
  if (__atexit_root == NULL || __atexit_root->next_slot >= ATEXIT_NUM_SLOTS)
    {
      struct atexit_block *new_block;

      new_block = malloc (sizeof (struct atexit_block));
      if (new_block == NULL)
	return -1;
      new_block->next_slot = 0;
      new_block->next = __atexit_root;
      __atexit_root = new_block;
    }

  __atexit_root->functions[__atexit_root->next_slot++] = func;

  return 0;
}
