/*
 * atime.c: grapped and modified from gerlib:amiga/normal/getsystime.c and
 *				      gerlib:amiga/normal/time.c
 *
 * Created: Wed Feb 23 16:45:41 1994 too
 * Last modified: Wed Feb 23 17:01:15 1994 too
 *
 * HISTORY
 * $Log: $
 */

#include <exec/types.h>
#include <exec/io.h>
#include <exec/memory.h>
#include <exec/ports.h>
#include <devices/timer.h>
#include <dos/dos.h>
#include <dos/dosextens.h>

#include <inline/exec.h>
#include <inline/dos.h>

ULONG atime(ULONG * t)
{
  struct timerequest * TimerIO;
  struct MsgPort * TimerMP;

  ULONG secs = 0;
  
  if ( (TimerMP = CreateMsgPort()) )
    {
      if ( (TimerIO = (struct timerequest *)
	    CreateIORequest(TimerMP, sizeof(struct timerequest))) )
	{
	  LONG error;
	  /* Open with UNIT_VBLANK, but any unit can be used */
	  
	  if (!(error = OpenDevice(TIMERNAME, UNIT_VBLANK,
				   (struct IORequest *)TimerIO, 0L)))
	    {
	 /*
	  * Issue the command and wait for it to finish, then get the reply
	  */
	      TimerIO->tr_node.io_Command = TR_GETSYSTIME;
	      DoIO((struct IORequest *) TimerIO);

	      secs  = TimerIO->tr_time.tv_secs;
	      
	      CloseDevice((struct IORequest *) TimerIO);
	    }
	  DeleteIORequest((struct IORequest *)TimerIO);
	}
      DeleteMsgPort(TimerMP);
    }
  if (t)
    *t = secs;
  return secs;
}
