home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / amiga / normal / gettimeofday.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  2.2 KB  |  92 lines

  1. #include <exec/types.h>
  2. #include <exec/io.h>
  3. #include <exec/memory.h>
  4. #include <exec/ports.h>
  5.  
  6. #include <sys/time.h>
  7.  
  8. #define timeval _timeval
  9. #include <devices/timer.h>
  10. #undef timeval
  11.  
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14.  
  15. #include <clib/alib_protos.h>
  16.  
  17. #include <inline/stubs.h>
  18. #ifdef __OPTIMIZE__
  19. #include <inline/exec.h>
  20. #include <inline/dos.h>
  21. #else
  22. #include <clib/exec_protos.h>
  23. #include <clib/dos_protos.h>
  24. #endif
  25.  
  26. int gettimeofday(struct timeval *tp, struct timezone *tzp)
  27. {
  28.     int ok_return=-1;
  29.  
  30.     if (tzp)
  31.     {
  32.       /* timezones are obsolete, we always return
  33.        * GMT... (from DST_MET you can see, that this software has been
  34.        * written in Middle Europe :-))
  35.        */
  36.  
  37.       tzp->tz_minuteswest = 0;
  38.       tzp->tz_dsttime = DST_MET;
  39.     }
  40.  
  41.     if (tp)
  42.     {
  43.         struct timerequest *TimerIO;
  44.         struct MsgPort *TimerMP;
  45.  
  46.         if ( (TimerMP = CreateMsgPort()) )
  47.         {
  48.             if ( (TimerIO = (struct timerequest *)
  49.                 CreateExtIO(TimerMP,sizeof(struct timerequest))) )
  50.             {
  51.                 LONG error;
  52.                     /* Open with UNIT_VBLANK, but any unit can be used */
  53.  
  54.                 if (!(error=OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)TimerIO,0L)))
  55.                 {
  56.  
  57.                         /* Issue the command and wait for it to finish, then get the reply */
  58.  
  59.                        TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  60.                        DoIO((struct IORequest *) TimerIO);
  61.  
  62.                         /* Get the results and close the timer device */
  63.                     tp->tv_usec = TimerIO->tr_time.tv_micro;
  64.                     tp->tv_sec  = TimerIO->tr_time.tv_secs;
  65.  
  66.                         /* add the offset from unix to amigados time system */
  67.                     tp->tv_sec += (8*365+2) * 24 * 3600;
  68.  
  69.                         /* Close the timer device */
  70.                     CloseDevice((struct IORequest *) TimerIO);
  71.  
  72.                     ok_return=0;
  73.                 }
  74.                 /* else ; */
  75.                      /* Error: Could not open timer device */
  76.  
  77.                        /* Delete the I/O request structure */
  78.                    DeleteExtIO((struct IORequest *)TimerIO);
  79.             }
  80.             /* else ; */
  81.                 /* Error: Could not create I/O structure */
  82.  
  83.                 /* Delete the port */
  84.             DeleteMsgPort(TimerMP);
  85.         }
  86.         /* else ; */
  87.          /* Error: Could not create port */
  88.     }
  89.  
  90.      return ok_return;
  91. }
  92.