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