home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / snapshot / signals.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  1.3 KB  |  74 lines

  1. /*
  2.  * signals.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <signal.h>
  7. #include "snapshot.h"
  8.  
  9. #ifndef BADSIG
  10. #define BADSIG (int (*)()) -1
  11. #endif
  12.  
  13. /* pointers to functions returning int */
  14. static int (*oldint) ();    /* Old value of SIGINT. */
  15.  
  16. #ifdef SIGTTIN
  17. static int (*oldttin) ();    /* Old value of SIGTTIN. */
  18. static int (*oldttou) ();    /* Old value of SIGTTOU. */
  19. #endif
  20.  
  21. /*
  22.  * Interrupt signal handler.
  23.  */
  24.  
  25. static int
  26. int_handler()
  27. {
  28.     fprintf(stderr, "Interrupted\n");
  29.     longjmp(sjbuf, -1);
  30. }
  31.  
  32. /*
  33.  * Set up signal handlers.  Return 0 if ok, -1 if error.
  34.  */
  35. handlesigs()
  36. {
  37. #ifdef SIGTTIN
  38.     /*
  39.      * Ignore the signals sent if someone in our process group tries a
  40.      * background read from/write to the tty. 
  41.      */
  42.     if ((oldttin = signal(SIGTTIN, SIG_IGN)) == BADSIG ||
  43.     (oldttou = signal(SIGTTOU, SIG_IGN)) == BADSIG) {
  44.     return -1;
  45.     }
  46. #endif
  47.  
  48.     /* if interrupts were being ignored, don't catch them */
  49.     if ((oldint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
  50.     if (signal(SIGINT, int_handler) == BADSIG) {
  51.         return -1;
  52.     }
  53.     return 0;
  54. }
  55.  
  56. /*
  57.  * Restore original signal values.  Return 0 if ok, -1 if error.
  58.  */
  59. restoresigs()
  60. {
  61.     int status = 0;
  62.     
  63. #ifdef SIGTTIN
  64.     if (signal(SIGTTIN, oldttin) == BADSIG || signal(SIGTTOU, oldttou) ==
  65.     BADSIG) {
  66.     status = -1;
  67.     }
  68. #endif
  69.     if (signal(SIGINT, oldint) == BADSIG) {
  70.     status = -1;
  71.     }
  72.     return status;
  73. }
  74.