ANSI X3J11 onexit() implementation
Doug Gwyn
gwyn at brl-smoke.ARPA
Wed Jun 18 06:53:17 AEST 1986
/*
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 <stdlib.h> /* 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 <stdlib.h> 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*/
}
More information about the Comp.lang.c
mailing list