home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / pc_hw / timer / clock.c next >
Encoding:
C/C++ Source or Header  |  1996-01-25  |  932 b   |  45 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <time.h>
  4. #include <go32.h>
  5. #include <libc/farptrgs.h>
  6. #include <libc/bss.h>
  7.  
  8. static int clock_bss = -1;
  9.  
  10. clock_t
  11. clock(void)
  12. {
  13.   static clock_t base = 0;
  14.   static unsigned long last_tics = 0;
  15.   unsigned long tics, otics;
  16.   clock_t rv;
  17.  
  18.   if (clock_bss != __bss_count)
  19.   {
  20.     clock_bss = __bss_count;
  21.     last_tics = 0;
  22.     base = 0;
  23.   }
  24.  
  25.   /* Make sure the numbers we get are consistent */
  26.   do {
  27.     otics = _farpeekl(_dos_ds, 0x46c);
  28.     tics = _farpeekl(_dos_ds, 0x46c);
  29.   } while (otics != tics);
  30.  
  31.   rv = tics;
  32.  
  33.   if (base == 0L)
  34.     base = rv;
  35.  
  36.   if (last_tics > tics) /* midnight happened */
  37.     base -= 0x1800b0;
  38.  
  39.   last_tics = tics;
  40.  
  41.   /* return relative time */
  42.   /* The 5 matches the scale for CLOCKS_PER_SEC */
  43.   return (rv - base) * 5;
  44. }
  45.