Path: utzoo!attcan!uunet!mstan!frank From: frank@Morgan.COM (Frank Wortner) Newsgroups: comp.os.minix Subject: atexit() for Minix 1.3 Keywords: atexit(), Minix Message-ID: <114@mstan.Morgan.COM> Date: 15 Nov 88 01:33:45 GMT Reply-To: frank@Morgan.COM (Frank Wortner) Organization: Morgan Stanley and Co., NY, NY Lines: 69 I've written an atexit() function for use with the Minix 1.3 library. It's occasionally useful, and no one (that I can recall) has posted anything like it yet, so here it is. Enjoy! Frank ------------------------------------------------------------------------------ echo x - atexit.doc sed '/^X/s///' > atexit.doc << '/' Xint atexit(func) Xvoid (*func)(); X Xatexit() arranges for the subroutine whose address is func to be Xcalled upon normal program termination. atexit() returns zero Xif the registration cannot be made, non-zero otherwise. / echo x - atexit.c sed '/^X/s///' > atexit.c << '/' X/* Copyright 1988 Frank Wortner X** You may reproduce and use this software as long as you X** do not delete this notice from the source code. X*/ X X#define MAXFN 33 /* Maximum number of functions registerable X (includes 1 for __cleanup) */ X Xtypedef void (*PFN)(); /* Pointer to FuNction */ X Xextern PFN __cleanup; /* imported from exit.c */ X Xstatic PFN fn[MAXFN]; /* addresses of functions stored here */ Xstatic int nfn = -1; /* number of functions registered less one */ X Xvoid checkcleanup(), run_atexit(); X Xint atexit(fun) XPFN fun; X{ X if (nfn == -1) /* Do we need to check for stdio? */ X checkcleanup(); X if (nfn >= MAXFN) /* Run out of room yet? */ X return (-1); X fn[++nfn] = fun; /* Save the address of the subr. */ X return (0); X} X X/* Check to see if stdio is used. If yes register stdio's cleanup routine X** with the atexit() system and then force stdio to call run_atexit(). X*/ Xstatic void checkcleanup() X{ X if (__cleanup) /* stdio used ? */ X fn[++nfn] = __cleanup; X __cleanup = run_atexit; X} X X/* Run the requested subroutines */ Xstatic void run_atexit() X{ X while (nfn >= 0) { X (*fn)[nfn--](); X } X} / -- Frank "Computers are mistake amplifiers."