view src/ploki/atechit.c @ 4225:4c30f3005bab

<oerjan> addquote <Taneb> Well, get him <Phantom_Hoover> her! <Taneb> Well, get her <Taneb> Hang on <Taneb> Since when is Liam Neeson a "her"/ <Phantom_Hoover> oh i thought you meant my dad\'s godmother
author HackBot
date Wed, 25 Dec 2013 13:31:15 +0000
parents ac0403686959
children
line wrap: on
line source

#include "zz.h"
#include "atechit.h"
#include "main.h"

#include <stdio.h>
#include <stdlib.h>

enum {MAGIC = 23};
static size_t Aused;
static void (*Afunc[MAGIC])(void);

static void bah(void) {
	for (; Aused; Afunc[--Aused]())
		;
}

void atechit(void (*f)(void)) {
	if (Aused >= sizeof Afunc / sizeof *Afunc) {
		NOTREACHED;
	}
	Afunc[Aused++] = f;
	if (Aused == 1 && atexit(bah)) {
		fprintf(stderr, "%s: atexit(): error message\n", Prog);
		bah();
		exit(EXIT_FAILURE);
	}
}