; /*
sc time.c ignore=73
slink from lib:c.o time.o to Time lib lib:sc.lib lib:amiga.lib sc sd nd
quit

  Time v1.2 Copyright © 1994 by Stefano Reksten of 3AM - The Three Amigos!!!
  This piece of code is FREEWARE. Do anything with it but don't gain money
  for my work! And please spread exe + source!

  What does it do? Simply tells you how much time did a command take to
  complete.

  rekststef@unisi.it

  Stefano Reksten c/o Naimi,
  v.le Cavour 40,
  53100 Siena
  Italy

*/

#include <exec/types.h>
#include <exec/io.h>
#include <devices/timer.h>

#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/timer_protos.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>

struct timeval start_time, end_time;
struct Library *TimerBase;
struct timerequest *tIO;
struct MsgPort *mport;
struct Message *msg;
char *cmd_line;
/*ULONG res_secs, res_micro;*/
BPTR output;

#define RES_SECS	0
#define RES_MICRO	1

ULONG	res[2];

char *template = "Command/A/F";
char*ver = "$VER: Time v1.2 (31.12.94)";

void _main( char *line )
{
struct RDArgs *args;

if ( args = ReadArgs( template, (LONG *)&cmd_line, NULL ) )
	{
	if ( mport = CreateMsgPort() )
		{
		if ( tIO = (struct timerequest *)CreateExtIO(mport,sizeof(struct timerequest)) )
			{
			if ( !OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest*)tIO,0L) )
				{
				TimerBase = (struct Library *)tIO->tr_node.io_Device;

				output = Output();
				VFPrintf( output, "\2333mTime\2330m v1.2 by Stefano Reksten\n", NULL );
				GetSysTime( &start_time );

				system( cmd_line );

				GetSysTime( &end_time );

				if ( end_time.tv_micro < start_time.tv_micro )
					{
					start_time.tv_secs--;
					res[RES_MICRO] = start_time.tv_micro - end_time.tv_micro;
					}
				else res[RES_MICRO] = end_time.tv_micro - start_time.tv_micro;

				res[RES_SECS] = end_time.tv_secs - start_time.tv_secs;

				VFPrintf( output, "Command took %ld secs, %ld micros to complete.\n", (LONG *)res );
				CloseDevice((struct IORequest *)tIO);
				}
			DeleteExtIO((struct IORequest *)tIO);
			}
		DeleteMsgPort(mport);
		}
	FreeArgs( args );
	}
else PrintFault( IoErr(), NULL );
}
