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