home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / on_exit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-27  |  2.1 KB  |  69 lines

  1. /* 
  2.  * on_exit.c --
  3.  *
  4.  *    This file contains the source code for the "on_exit" library
  5.  *    procedure.  "on_exit" is sort of just like "at_exit" except
  6.  *      you can pass an argument.
  7.  *
  8.  * Copyright 1988 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/on_exit.c,v 1.1 92/03/27 13:40:40 rab Exp Locker: rab $ SPRITE (Berkeley)";
  20. #endif /* not lint */
  21.  
  22. #include <stdlib.h>
  23.  
  24. /*
  25.  * Variables shared with exit.c:
  26.  */
  27.  
  28. extern void (*_exitHandlers[])();    /* Function table. */
  29. extern int _exitNumHandlers;        /* Number of functions currently
  30.                      * registered in table. */
  31. extern long _exitHandlerArgs[];        /* Arguments to pass to functions. */
  32. extern int _exitTableSize;        /* Number of entries in table. */
  33.  
  34. /*
  35.  *----------------------------------------------------------------------
  36.  *
  37.  * on_exit --
  38.  *
  39.  *    Register a function ("func") to be called as part of process
  40.  *    exiting.  One argment can be passed.
  41.  *
  42.  * Results:
  43.  *    The return value is 0 if the registration was successful,
  44.  *    and -1 if registration failed because the table was full.
  45.  *
  46.  * Side effects:
  47.  *    Information will be remembered so that when the process exits
  48.  *    (by calling the "exit" procedure), func will be called.  Func
  49.  *    takes no arguments and returns no result.  If the process
  50.  *    terminates in some way other than by calling exit, then func
  51.  *    will not be invoked.
  52.  *
  53.  *----------------------------------------------------------------------
  54.  */
  55.  
  56. int
  57. on_exit(func, arg)
  58.     void (*func)();            /* Function to call during exit. */
  59.     long arg;
  60. {
  61.     if (_exitNumHandlers >= _exitTableSize) {
  62.     return -1;
  63.     }
  64.     _exitHandlers[_exitNumHandlers] = func;
  65.     _exitHandlerArgs[_exitNumHandlers] = arg;
  66.     _exitNumHandlers += 1;
  67.     return 0;
  68. }
  69.