home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / signal.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  86 lines

  1. /* signal() for MiNT; written by ERS, placed in the public domain */
  2.  
  3. #include <compiler.h>
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <mintbind.h>
  7. #include <signal.h>
  8.  
  9. /* vector of signal handlers (for TOS, or for MiNT with -mshort) */
  10. extern __Sigfunc _sig_handler[NSIG];
  11.  
  12. /* vector giving which signals are currently blocked from delivery (for TOS) */
  13. extern long _sigmask;
  14.  
  15. /* vector giving an indication of which signals are currently pending (for TOS) */
  16. extern long _sigpending;
  17.  
  18.  
  19. #ifdef __MSHORT__
  20. /* trampoline code: for any caught signal, converts the 32 bit signal
  21.  * number MiNT passed us into a 16 bit one, and jumps to the handler
  22.  * we previously established
  23.  */
  24.  
  25. void __CDECL _trampoline __PROTO((long));
  26.  
  27. /* the argument is on the stack */
  28. void __CDECL _trampoline(sig)
  29.     long sig;
  30. {
  31.     __Sigfunc func;
  32.  
  33.     func = _sig_handler[sig];
  34.  
  35. /* note: func should never be SIG_IGN or SIG_DFL; if it is, something
  36.  * really bad happened and we want to crash anyway!
  37.  */
  38.     (*func)((short)sig);
  39. }
  40. #endif
  41.  
  42. __Sigfunc
  43. signal(sig, func)
  44.     int sig;
  45.     __Sigfunc func;
  46. {
  47.     long old;
  48.     extern int __mint;
  49.     __Sigfunc oldfunc;
  50.  
  51.     if (__mint == 0) {
  52.         if (sig < 0 || sig >= NSIG) {
  53.             errno = ERANGE;
  54.             return SIG_ERR;
  55.         }
  56.         oldfunc = _sig_handler[sig];
  57.         _sig_handler[sig] = func;
  58.         return oldfunc;
  59.     }
  60.  
  61. #ifdef __MSHORT__
  62. /* NOTE: MiNT passes 32 bit numbers for signals, so we want our
  63.  * own signal dispatcher to switch these to 16 bit ints
  64.  */
  65.     if (sig < 0 || sig >= NSIG) {
  66.         errno = ERANGE;
  67.         return SIG_ERR;
  68.     }
  69.     oldfunc = _sig_handler[sig];
  70.     _sig_handler[sig] = func;
  71.     if (func != SIG_DFL && func != SIG_IGN) {
  72.         func = (__Sigfunc) _trampoline;
  73.     }
  74. #endif
  75.     old = Psignal((short)sig, (long)func);
  76.     if (old < 0) {
  77.         errno = (int) -old;
  78.         return SIG_ERR;
  79.     }
  80.     func = (__Sigfunc) old;
  81. #ifdef __MSHORT__
  82.     if (func == (__Sigfunc) _trampoline) func = oldfunc;
  83. #endif
  84.     return func;
  85. }
  86.