home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / EXIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.1 KB  |  163 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - exit.c
  3.  *
  4.  * function(s)
  5.  *      ___exit - handle the four flavors of exit
  6.  *      exit    - terminate program
  7.  *      _exit   - quick termination
  8.  *      _cexit  - perform exit cleanup without termination
  9.  *      _c_exit - perform quick exit cleanup without termination
  10.  *-----------------------------------------------------------------------*/
  11.  
  12. /*
  13.  *      C/C++ Run Time Library - Version 5.0
  14.  *
  15.  *      Copyright (c) 1987, 1992 by Borland International
  16.  *      All Rights Reserved.
  17.  *
  18.  */
  19.  
  20.  
  21. #include <stdlib.h>
  22.  
  23. extern  int             _atexitcnt;    /* count of atexit function pointers */
  24. extern  atexit_t        _atexittbl[];  /* array of atexit function pointers */
  25.  
  26. void _terminate( int );                 /* terminate program */
  27. void _cleanup( void );                  /* call #pragma exit routines */
  28. void _checknull( void );                /* check for null pointer usage */
  29. void _restorezero( void );              /* restore interrupt vectors */
  30.  
  31. static  void    dummy(void)
  32. {
  33. }
  34.  
  35. void    (*_exitbuf)(void)   = dummy;
  36. void    (*_exitfopen)(void) = dummy;
  37. void    (*_exitopen)(void)  = dummy;
  38.  
  39. /*---------------------------------------------------------------------------*
  40.  
  41. Name            ___exit - perform cleanup and optionally terminate the program
  42.  
  43. Usage           void ___exit(int quick, int dontexit, int errcode);
  44.  
  45. Prototype in    local
  46.  
  47. Description     ___exit is an internal routine used by the various flavors
  48.                 of exit.  If the "quick" flag is false, buffered output
  49.                 (waiting to be output) is written to files, any registered
  50.                 "exit functions"  (posted with atexit) are called, and
  51.                 #pragma exit functions are called.
  52.  
  53.                 In all cases, interrupt vectors taken by the RTL are
  54.                 restored, and in small and medium models, stray NULL pointers
  55.                 are checked for by seeing if the copyright message
  56.                 got zapped.
  57.  
  58.                 Finally, if the "dontexit" flag is false, all files
  59.                 are closed, and the program is terminated with the
  60.                 error code "errcode".
  61.  
  62. Return value    If dontexit is false, ___exit never returns; otherwise
  63.                 it returns no value.
  64.  
  65. *----------------------------------------------------------------------------*/
  66. static void pascal near ___exit(int quick, int dontexit, int errcode)
  67. {
  68.     if (!quick)
  69.     {
  70.         /* Execute "atexit" functions
  71.          */
  72.         while (_atexitcnt)
  73.             (*_atexittbl[--_atexitcnt])();
  74.  
  75.         /* Run #pragma exit routines
  76.          */
  77.         _cleanup();
  78.  
  79.         /* Flush files.
  80.          */
  81.         (*_exitbuf)();
  82.     }
  83.  
  84.  
  85.     /* Restore interrupt vectors.
  86.      */
  87.     _restorezero();
  88.  
  89.     /* Check for stray NULL pointers zapping the copyright message.
  90.      */
  91.     _checknull();
  92.  
  93.     if (!dontexit)
  94.     {
  95.         if (!quick)
  96.         {
  97.             (*_exitfopen)();    /* close stream files */
  98.             (*_exitopen)();     /* close handle files */
  99.         }
  100.         _terminate(errcode);    /* terminate program */
  101.     }
  102. }
  103.  
  104. /*---------------------------------------------------------------------------*
  105.  
  106. Name            exit - terminates program
  107.  
  108. Usage           void exit(int status);
  109.  
  110. Prototype in    process.h and stdlib.h
  111.  
  112. Related
  113. functions usage void _exit(int status);
  114.                 void _cexit(void);
  115.                 void _c_exit(void);
  116.  
  117. Description     exit  terminates the  calling process.  Before exiting, all
  118.                 files are closed, buffered output (waiting to be output) is
  119.                 written, interrupt vectors taken by the startup code are
  120.                 restored, and  any registered "exit functions"  (posted with
  121.                 atexit) are called.
  122.  
  123.                 status is provided for the  calling process as the the exit
  124.                 status of  the process. Typically a  value of 0 is  used to
  125.                 indicate a normal exit, and a non-zero value indicates some
  126.                 error.
  127.  
  128.                 _exit is "quick" version of exit.  It does everything
  129.                 that exit does, except that it doesn't flush files or
  130.                 call atexit or #pragma exit routines.
  131.  
  132.                 _cexit is a non-terminating version of exit().  It does
  133.                 everything that exit does, except that it doesn't
  134.                 close files or terminate the program.
  135.  
  136.                 _c_exit is a non-terminating version of _exit().  It
  137.                 restores interrupt vectors taken by the startup code, but
  138.                 does no other cleanup and does not terminate the program.
  139.  
  140. Return value    exit and _exit never return.  _cexit and _c_exit do
  141.                 not return a value.
  142.  
  143. *----------------------------------------------------------------------------*/
  144. void    exit(int c)
  145. {
  146.     ___exit(0,0,c);
  147. }
  148.  
  149. void    _exit(int c)
  150. {
  151.     ___exit(1,0,c);
  152. }
  153.  
  154. void    _cexit(void)
  155. {
  156.     ___exit(0,1,0);
  157. }
  158.  
  159. void    _c_exit(void)
  160. {
  161.     ___exit(1,1,0);
  162. }
  163.