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