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