/* Timeget Version 2.01 */
/* Copyright (c) Ｎａｍｙ 1994,1995. All rights reserved. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <time.h>
#include "softtime.h"

typedef unsigned int uint;
typedef unsigned long ulong;

char sf=0;
ulong callpersec,call;
uint starttime,endtime;

void main ( int argc,char *argv[] )
{
	int i,t;
	ulong clockt;
	uint sec,msec;

	if(argc>=2)
	if((*argv[1]=='-' || *argv[1]=='/') && strnicmp(argv[1]+1,"ST",2)==0)
	{
		sf=1;
		if(*(argv[1]+3)==0)
		{
			printf("ソフトウェアタイマ：計測中．");
			callpersec=ST_getcallpersec();	// 基準値を得る
			printf("\x1b[8D%lu 回／秒\n\n",callpersec);
		}
		else callpersec=atol(argv[1]+3);
	}

	if( argc < (3+sf) )
	{ printf("Usage : timeget (/ST(<n>)) <n> <command> ...\n");exit(0); }

	t=atoi(argv[1+sf]);
	printf("%d 回実行します．\n",t);

	if(sf) starttime=ST_time0wait();	// 秒が変わるのを待つ
	else clockt=clock();

	for(i=0;i<t;i++)
		spawnvp(P_WAIT,argv[2+sf],&argv[2+sf]);

	if(sf)
	{
		endtime=ST_getnext0time(&call);	// 終了時刻を得る
		if(endtime < starttime) endtime+=60*60;
			// 分と秒しか見ていないので、追い越した＝時が変わった時の補正
	/*	printf("starttime=%u endtime=%u call=%lu\n",starttime,endtime,call);*/
		sec=endtime-starttime-1;
		msec=((callpersec-call)*100/callpersec);
	}
	else
	{
		clockt=clock()-clockt;
		sec=clockt/CLOCKS_PER_SEC;
		msec=(clockt%CLOCKS_PER_SEC)/(CLOCKS_PER_SEC/100);
	}

	printf("所要時間 %u.%02u 秒\n",sec,msec);
	exit(0);
}
