1 #ifndef lint 2 static char rcsid[] = "$Id: nap.c,v 1.2 1993/08/02 17:20:15 mycroft Exp $"; 3 #endif /* not lint */ 4 5 /* nap.c Larn is copyrighted 1986 by Noah Morgan. */ 6 #include <signal.h> 7 #include <sys/types.h> 8 #ifdef SYSV 9 #include <sys/times.h> 10 #else 11 #ifdef BSD 12 #include <sys/timeb.h> 13 #endif BSD 14 #endif SYSV 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 static napms(time) int time; {} /* do nothing, forget it */ 118 #endif BSD 119 #endif SYSV 120 #endif NONAP 121