home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / mssup201.arc / SIGNAL.C < prev    next >
C/C++ Source or Header  |  1987-08-09  |  2KB  |  76 lines

  1. /*
  2. signal.c - a signal package for Turbo C 1.0.
  3.  
  4. This program and accompanying documentation, henceforth collectively known as
  5. "this software", are copyrighted thus:  (C) Copyright 1987 Rahul Dhesi, all
  6. rights reserved.  This software may be used and distributed in any way
  7. whatsoever, whether commercial or noncommercial, with the following
  8. exceptions:  (a) this paragraph and this copyright notice must be included
  9. unchanged;  (b) it is forbidden to copy this software for distribution as
  10. part of any collection over which a compilation copyright is claimed.
  11. Notwithstanding the above, there are no restrictions of any kind on the
  12. machine-readable object code produced by compiling this program with a C
  13. compiler.
  14.  
  15.                                    -- Rahul Dhesi 1987/08/08
  16. */
  17.  
  18. /*
  19. Note:  When assigning an address to the variable `handler', a race
  20. condition can theoretically occur if a user interrupt occurs during
  21. the assignment.  However, current versions of MS-DOS cause a user
  22. interrupt to take effect only during system calls, so in practice it
  23. won't happen.
  24. */
  25.  
  26. /* The following include files are already supplied with Turbo C */
  27. #include <signal.h>
  28. #include <errno.h>
  29.  
  30. /*
  31. NOTE:  For best results, edit your <signal.h> file, provided with
  32. Turbo C, to include the following declaration at the end:
  33.      int (*_Cdecl signal (int sig, int (*action)())) ();
  34. */
  35.  
  36. void ctrlbrk (int (*fptr)(void));
  37.  
  38. static int (*handler)() = SIG_DFL;
  39. int main_handler();
  40.  
  41. int (*_Cdecl signal (int sig, int (*action) ())) ()
  42. {
  43.     int (*retval) ();
  44.     static int installed = 0;
  45.     if (sig != SIGINT) {
  46.         errno = EINVAL;
  47.         return SIG_ERR;                 /* error return */
  48.     }
  49.     if (!installed) {
  50.         ctrlbrk (main_handler);         /* ctrlbrk() is in Turbo C library */
  51.         installed = 1;
  52.     }
  53.  
  54.     retval = handler;
  55.     handler = action;
  56.     return (retval);
  57. }
  58.  
  59. #define ABORT_PGM   0
  60. #define RESUME_PGM  1
  61.  
  62. /* Every keyboard interrupt is handled here first */
  63. int main_handler()
  64. {
  65.     int (*old_handler)(void);
  66.  
  67.     if (handler == SIG_IGN)
  68.         return (RESUME_PGM);
  69.     if (handler == SIG_DFL)
  70.         return (ABORT_PGM);
  71.     old_handler = handler;          /* Save user's handler address */
  72.     handler = SIG_DFL;              /* Reset handler, like System V does */
  73.     (*old_handler)();               /* call the user's handler */
  74.     return (RESUME_PGM);
  75. }
  76.