summaryrefslogtreecommitdiffstats
path: root/larn/nap.c
diff options
context:
space:
mode:
authorcgd <cgd@NetBSD.org>1993-03-21 09:45:37 +0000
committercgd <cgd@NetBSD.org>1993-03-21 09:45:37 +0000
commit77e3814f0c0e3dea4d0032e25666f77e6f83bfff (patch)
tree7eddfcbf3dd12089e71dc3fafb0a106c5c5766c7 /larn/nap.c
parente81d63576b2e46ab90da7d75fa155ea57ee4d32e (diff)
downloadbsdgames-darwin-77e3814f0c0e3dea4d0032e25666f77e6f83bfff.tar.gz
bsdgames-darwin-77e3814f0c0e3dea4d0032e25666f77e6f83bfff.tar.zst
bsdgames-darwin-77e3814f0c0e3dea4d0032e25666f77e6f83bfff.zip
initial import of 386bsd-0.1 sources
Diffstat (limited to 'larn/nap.c')
-rw-r--r--larn/nap.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/larn/nap.c b/larn/nap.c
new file mode 100644
index 00000000..b7a877d1
--- /dev/null
+++ b/larn/nap.c
@@ -0,0 +1,116 @@
+/* nap.c Larn is copyrighted 1986 by Noah Morgan. */
+#include <signal.h>
+#include <sys/types.h>
+#ifdef SYSV
+#include <sys/times.h>
+#else
+#ifdef BSD
+#include <sys/timeb.h>
+#endif BSD
+#endif SYSV
+
+/*
+ * routine to take a nap for n milliseconds
+ */
+nap(x)
+ register int x;
+ {
+ if (x<=0) return; /* eliminate chance for infinite loop */
+ lflush();
+ if (x > 999) sleep(x/1000); else napms(x);
+ }
+
+#ifdef NONAP
+napms(x) /* do nothing */
+ int x;
+ {
+ }
+#else NONAP
+#ifdef SYSV
+/* napms - sleep for time milliseconds - uses times() */
+/* this assumes that times returns a relative time in 60ths of a second */
+/* this will do horrible things if your times() returns seconds! */
+napms(time)
+ int time;
+ {
+ long matchclock, times();
+ struct tms stats;
+
+ if (time<=0) time=1; /* eliminate chance for infinite loop */
+ if ((matchclock = times(&stats)) == -1 || matchclock == 0)
+ return; /* error, or BSD style times() */
+ matchclock += (time / 17); /*17 ms/tic is 1000 ms/sec / 60 tics/sec */
+
+ while(matchclock < times(&stats))
+ ;
+ }
+
+#else not SYSV
+#ifdef BSD
+#ifdef SIGVTALRM
+/* This must be BSD 4.2! */
+#include <sys/time.h>
+#define bit(_a) (1<<((_a)-1))
+
+static nullf()
+ {
+ }
+
+/* napms - sleep for time milliseconds - uses setitimer() */
+napms(time)
+ int time;
+ {
+ struct itimerval timeout;
+ int (*oldhandler) ();
+ int oldsig;
+
+ if (time <= 0) return;
+
+ timerclear(&timeout.it_interval);
+ timeout.it_value.tv_sec = time / 1000;
+ timeout.it_value.tv_usec = (time % 1000) * 1000;
+
+ oldsig = sigblock(bit(SIGALRM));
+ setitimer(ITIMER_REAL, &timeout, (struct itimerval *)0);
+ oldhandler = signal(SIGALRM, nullf);
+ sigpause(oldsig);
+ signal(SIGALRM, oldhandler);
+ sigsetmask(oldsig);
+ }
+
+#else
+/* napms - sleep for time milliseconds - uses ftime() */
+
+static napms(time)
+ int time;
+ {
+ /* assumed to be BSD UNIX */
+ struct timeb _gtime;
+ time_t matchtime;
+ unsigned short matchmilli;
+ register struct timeb *tp = & _gtime;
+
+ if (time <= 0) return;
+ ftime(tp);
+ matchmilli = tp->millitm + time;
+ matchtime = tp->time;
+ while (matchmilli >= 1000)
+ {
+ ++matchtime;
+ matchmilli -= 1000;
+ }
+
+ while(1)
+ {
+ ftime(tp);
+ if ((tp->time > matchtime) ||
+ ((tp->time == matchtime) && (tp->millitm >= matchmilli)))
+ break;
+ }
+ }
+#endif
+#else not BSD
+static napms(time) int time; {} /* do nothing, forget it */
+#endif BSD
+#endif SYSV
+#endif NONAP