1/*
2 * onexit.c
3 */
4
5#include <stdlib.h>
6#include <unistd.h>
7#include "atexit.h"
8
9static struct atexit *__atexit_list;
10
11static __noreturn on_exit_exit(int rv)
12{
13    struct atexit *ap;
14
15    for (ap = __atexit_list; ap; ap = ap->next) {
16	ap->fctn(rv, ap->arg);	/* This assumes extra args are harmless */
17    }
18
19    _exit(rv);
20}
21
22int on_exit(void (*fctn) (int, void *), void *arg)
23{
24    struct atexit *as = malloc(sizeof(struct atexit));
25
26    if (!as)
27	return -1;
28
29    as->fctn = fctn;
30    as->arg = arg;
31
32    as->next = __atexit_list;
33    __atexit_list = as;
34
35    return 0;
36}
37