signal.c 817 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * PuTTY's wrapper on signal(2).
  3. *
  4. * Calling signal() is non-portable, as it varies in meaning between
  5. * platforms and depending on feature macros, and has stupid semantics
  6. * at least some of the time.
  7. *
  8. * This function provides the same interface as the libc function, but
  9. * provides consistent semantics. It assumes POSIX semantics for
  10. * sigaction() (so you might need to do some more work if you port to
  11. * something ancient like SunOS 4).
  12. */
  13. #include <signal.h>
  14. #include "defs.h"
  15. void (*putty_signal(int sig, void (*func)(int)))(int)
  16. {
  17. struct sigaction sa;
  18. struct sigaction old;
  19. sa.sa_handler = func;
  20. if (sigemptyset(&sa.sa_mask) < 0)
  21. return SIG_ERR;
  22. sa.sa_flags = SA_RESTART;
  23. if (sigaction(sig, &sa, &old) < 0)
  24. return SIG_ERR;
  25. return old.sa_handler;
  26. }