home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / larn / nap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-05  |  2.3 KB  |  117 lines

  1. /* nap.c         Larn is copyrighted 1986 by Noah Morgan. */
  2. #include <signal.h>
  3. #include <sys/types.h>
  4. #ifdef SYSV
  5. #include <sys/times.h>
  6. #else
  7. #ifdef BSD
  8. #include <sys/timeb.h>
  9. #endif BSD
  10. #endif SYSV
  11.  
  12. /*
  13.  *    routine to take a nap for n milliseconds
  14.  */
  15. nap(x)
  16.     register int x;
  17.     {
  18.     if (x<=0) return; /* eliminate chance for infinite loop */
  19.     lflush();
  20.     if (x > 999) sleep(x/1000); else napms(x);
  21.     }
  22.  
  23. #ifdef NONAP
  24. napms(x)    /* do nothing */
  25.     int x;
  26.     {
  27.     }
  28. #else NONAP
  29. #ifdef SYSV
  30. /*    napms - sleep for time milliseconds - uses times() */
  31. /* this assumes that times returns a relative time in 60ths of a second */
  32. /* this will do horrible things if your times() returns seconds! */
  33. napms(time)
  34.     int time;
  35.     {
  36.     long matchclock, times();
  37.     struct tms stats;
  38.  
  39.     if (time<=0) time=1; /* eliminate chance for infinite loop */
  40.     if ((matchclock = times(&stats)) == -1 || matchclock == 0)
  41.         return;    /* error, or BSD style times() */
  42.     matchclock += (time / 17);        /*17 ms/tic is 1000 ms/sec / 60 tics/sec */
  43.  
  44.     while(matchclock < times(&stats))
  45.         ;
  46.     }
  47.  
  48. #else not SYSV
  49. #ifdef BSD
  50. #ifdef SIGVTALRM
  51. /* This must be BSD 4.2!  */
  52. #include <sys/time.h>
  53. #define bit(_a) (1<<((_a)-1))
  54.  
  55. static  nullf()
  56.     {
  57.     }
  58.  
  59. /*    napms - sleep for time milliseconds - uses setitimer() */
  60. napms(time)
  61.     int time;
  62.     {
  63.     struct itimerval    timeout;
  64.     int     (*oldhandler) ();
  65.     int     oldsig;
  66.  
  67.     if (time <= 0) return;
  68.  
  69.     timerclear(&timeout.it_interval);
  70.     timeout.it_value.tv_sec = time / 1000;
  71.     timeout.it_value.tv_usec = (time % 1000) * 1000;
  72.  
  73.     oldsig = sigblock(bit(SIGALRM));
  74.     setitimer(ITIMER_REAL, &timeout, (struct itimerval *)0);
  75.     oldhandler = signal(SIGALRM, nullf);
  76.     sigpause(oldsig);
  77.     signal(SIGALRM, oldhandler);
  78.     sigsetmask(oldsig);
  79.     }
  80.  
  81. #else
  82. /*    napms - sleep for time milliseconds - uses ftime() */
  83.  
  84. static napms(time)
  85.     int time;
  86.     {
  87.     /* assumed to be BSD UNIX */
  88.     struct timeb _gtime;
  89.     time_t matchtime;
  90.     unsigned short matchmilli;
  91.     register struct timeb *tp = & _gtime;
  92.  
  93.     if (time <= 0) return;
  94.     ftime(tp);
  95.     matchmilli = tp->millitm + time;
  96.     matchtime  = tp->time;
  97.     while (matchmilli >= 1000)
  98.         {
  99.         ++matchtime;
  100.         matchmilli -= 1000;
  101.         }
  102.  
  103.     while(1)
  104.         {
  105.         ftime(tp);
  106.         if ((tp->time > matchtime) ||
  107.             ((tp->time == matchtime) && (tp->millitm >= matchmilli)))
  108.             break;
  109.         }
  110.     }
  111. #endif
  112. #else not BSD
  113. static napms(time) int time; {}    /* do nothing, forget it */
  114. #endif BSD
  115. #endif SYSV
  116. #endif NONAP
  117.