view src/ploki/atechit.c @ 8203:b0fcb29e2ce7

<b_jonas> learn Silent initial consonants in English are: write, wrong, wrist, wrap, wreck, wrest, wrestle, wriggle, wrinkle, wrath, wraith, wreath, wrangle, wren, wright, wrought, wring, writ, writhe, wry; know, knife, knock, knee, knight, knob, knot, knit, knack, knout, knuckle; psychology, psalm, pseudo, psychic, psionics; gnaw, gnash, gnome, gnu, gnat; pneumatic; mnemonic; chthonic; phthisis; bdellium.
author HackBot
date Sun, 29 May 2016 20:52:46 +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);
	}
}