sleep.c 694 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* $Id$ */
  2. #include <signal.h>
  3. #include <setjmp.h>
  4. static jmp_buf setjmpbuf;
  5. static void
  6. alfun(){
  7. longjmp(setjmpbuf, 1);
  8. } /* used with sleep() below */
  9. sleep(n)
  10. int n;
  11. {
  12. /* sleep(n) pauses for 'n' seconds by scheduling an alarm interrupt. */
  13. unsigned oldalarm;
  14. void (*oldsig)();
  15. if (n <= 0) return;
  16. if (setjmp(setjmpbuf)) {
  17. signal(SIGALRM, oldsig);
  18. alarm(oldalarm);
  19. return;
  20. }
  21. oldalarm = alarm(5000); /* Who cares how long, as long as it is long
  22. enough
  23. */
  24. if (oldalarm > n) oldalarm -= n;
  25. else if (oldalarm) {
  26. n = oldalarm;
  27. oldalarm = 1;
  28. }
  29. oldsig = signal(SIGALRM, alfun);
  30. alarm(n);
  31. for (;;) {
  32. /* allow for other handlers ... */
  33. pause();
  34. }
  35. }