view src/ploki/atechit.c @ 5983:e9e46956a506

<oerjan> mk wisdom/welcome.sv//Hej och v\xc3\xa4lkommen till den internationella knutpunkten f\xc3\xb6r esoterisk programmeringsspr\xc3\xa5ksdesign och -drifts\xc3\xa4ttning! F\xc3\xb6r mer information, se v\xc3\xa5r wiki: <http://esolangs.org/>. (F\xc3\xb6r den andra sortens esoterism, pr\xc3\xb6va #esoteric p\xc3\xa5 EFnet eller DALnet.)
author HackBot
date Mon, 14 Sep 2015 17:49:06 +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);
	}
}