home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / datetime / navytime / atfinish.c next >
Encoding:
C/C++ Source or Header  |  1987-06-27  |  1.5 KB  |  56 lines

  1. /* atfinish -- register functions to be called on termination               */
  2. /* copyright 1987  Michael M Rubenstein                                     */
  3.  
  4. /* unlike atexit, atfinish functions will be run on any kind of termination */
  5.  
  6. #include <dos.h>
  7. #include "atfinish.h"
  8.  
  9. #define MAXFINISH       8
  10.  
  11. static void far         (*old_quit)(void) = (void far (*) ()) 0;
  12. static unsigned         holdss, holdsp;
  13. static void interrupt   quit();
  14. static void             (*func[MAXFINISH])(void);
  15. static int              nfunc = 0;
  16.  
  17. extern unsigned         _psp;
  18.  
  19. int atfinish(void (*f)())
  20. {
  21.   if (nfunc == MAXFINISH)
  22.     return 1;
  23.  
  24.   /* get old exit routine */
  25.   if (old_quit == (void far (*)()) 0)
  26.     old_quit = MK_FP(peek(_psp, 12), peek(_psp, 10));
  27.  
  28.   disable();
  29.   holdss = _SS;                 /* quit routine will need to use our stack */
  30.   holdsp = _SP;
  31.   poke(_psp, 10, FP_OFF(quit)); /* set our quit routine */
  32.   poke(_psp, 12, FP_SEG(quit));
  33.   enable();
  34.  
  35.   func[nfunc] = f;
  36.   ++nfunc;
  37.  
  38.   return 0;
  39. }
  40.  
  41. /* quit routine.  this routine is executed on any termination               */
  42. void interrupt quit(void)
  43. {
  44.   static int          i;
  45.  
  46.   disable();
  47.   _SP = holdsp;       /* set to our stack.  this is safe since we're not    */
  48.   _SS = holdss;       /* returning.                                         */
  49.   enable();
  50.  
  51.   for (i = 0; i < nfunc; ++i)
  52.     (*func[i])();
  53.  
  54.   (*old_quit)();      /* go to old termination address (never returns)      */
  55. }
  56.