Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!seismo!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: ANSI X3J11 onexit() implementation Message-ID: <1436@brl-smoke.ARPA> Date: Tue, 17-Jun-86 16:53:17 EDT Article-I.D.: brl-smok.1436 Posted: Tue Jun 17 16:53:17 1986 Date-Received: Sat, 21-Jun-86 09:17:14 EDT Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 70 Keywords: X3J11 onexit freebie /* onexit, exit -- register function to be invoked at program termination last exit: 17-Jun-1986 D A Gwyn Portable version that invokes the previous implementation of exit(), now renamed __exit(), just to show how onexit() can be implemented. */ #if __STDC__ #include /* defines onexit_t */ #define VOID void /* empty argument list in prototype */ #define INT int /* int argument in prototype */ #else /* pre-X3J11 C */ #define VOID /* nothing -- prototypes not supported */ #define INT /* nothing -- prototypes not supported */ typedef void (*onexit_t)(VOID); /* should be general enough */ /* The above line really needs to be put into a header! */ #define NULL 0 #endif /* __STDC__ */ extern void __exit(INT); /* pre-onexit version of exit(); typically _cleanup() then _exit() */ /* functions are registered in a linear list with explicit count: */ #ifndef MAX_CALLS /* # functions that can be registered */ #define MAX_CALLS 32 /* minimum required by X3J11 */ #endif static onexit_t (*list[MAX_CALLS])(VOID); /* function registration list */ static int n_calls = 0; /* # functions registered so far */ onexit_t onexit( func ) onexit_t (*func)(VOID); /* -> function to be registered */ { if ( func == NULL /* safety net for incorrect usage */ || n_calls == MAX_CALLS ) return (onexit_t) NULL; /* registration failure */ else { list[n_calls++] = func; /* register the function */ return (onexit_t) func; /* non-NULL => success */ } } void exit( status ) int status; /* termination status code */ { /* execute functions in reverse order of registration */ while ( n_calls != 0 ) (void) (*list[--n_calls])(); __exit( status ); /* former version of exit() */ /*NOTREACHED*/ }