home *** CD-ROM | disk | FTP | other *** search
- /*Program 6_3 - Delay Specified Number of Seconds
- by Stephen R. Davis, 1987
-
- Many programs must delay for short periods of time. Often this
- is done by executing an "empty" FOR loop. The Time-of-Day BIOS
- routine provides a more accurate means of delaying.
- */
-
- #include <stdio.h>
- #include <dos.h>
-
- /*prototype definitions*/
- void main (void);
- unsigned getval (char *);
- void wait (unsigned);
- unsigned getime (void);
-
- /*global data definitions*/
- union REGS reg;
- union {
- int stime [2];
- long int dtime;
- } both;
-
- /*Main - ask the user for length of time to delay (0 terminates)*/
- void main (void)
- {
- unsigned delay;
-
- printf ("This program simply delays the user specified number\n"
- "of seconds. Entering a zero terminates the program.\n"
- "\n"
- "Seconds are counted down to provide output so that\n"
- "the user can break out prematurely, if desired\n"
- "\n");
- for (;;) {
-
- /*get the specified delay and wait () that long*/
- if ((delay = getval ("Enter delay time [seconds]")) == 0)
- break;
-
- /*now call our wait procedure to perform the delay*/
- printf ("Start delay:\n");
- wait (delay);
- printf ("Finished\n");
- }
- }
-
- /*Getval - output a prompt and get an integer response*/
- unsigned getval (prompt)
- char *prompt;
- {
- unsigned retval;
-
- printf ("%s - ", prompt);
- scanf ("%d", &retval);
- return retval;
- }
-
-
-
- /*Wait - wait the specified length of time*/
- void wait (delay)
- unsigned delay;
- {
-
- unsigned previous, current;
-
- /*every time the time changes - decrement count*/
- previous = getime ();
- for (;;) {
- if (previous != (current = getime ())) {
- previous = current;
- if (!--delay)
- return;
-
- /*remove this print statement in actual use*/
- printf ("count - %d\n", delay);
- }
- }
- }
-
- /*Getime - return the current time in seconds since midnight*/
- unsigned getime (void)
- {
- /*use the BIOS Time-of-Day to read the current time*/
- reg.h.ah = 0;
- int86 (0x1a, ®, ®);
- both.stime [0] = reg.x.dx;
- both.stime [1] = reg.x.cx;
-
- /*now convert the clock ticks into seconds (fudge factor
- is the difference between 18.2 and 18*/
- return (unsigned)(both.dtime / 18) -
- (unsigned)(both.dtime / 1638);
- }