home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / larn.t.Z / larn.t / nap.c < prev    next >
Text File  |  1988-11-13  |  3KB  |  148 lines

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