/* time command */

#include <stdio.h>
#include <signal.h>
#include <types.h>
#include <times.h>

extern int errno;
extern char *sys_errlist[];
extern char **environ;
extern char *etime();

char eltime[40], sytime[40], utime[40];

_main(argc, argv)
char **argv;
{
	struct tms buffer, obuffer;
	int status;
	register p;

	if(argc<=1)
		exit(0);
	times(&obuffer);
	p = fork();
	if(p == -1) {
		fprintf(stderr, "Try again.\n");
		exit(1);
	}
	if(p == 0) {
		execvp(argv[1], &argv[1]);
		fprintf(stderr, "%s: %s\n", argv[1], sys_errlist[errno]);
		exit(1);
	}
	signal(SIGINT, SIG_IGN);
	signal(SIGQUIT, SIG_IGN);
/*****
	times(&obuffer);
******/
	while(wait(&status) != p)
		times(&obuffer);
	if((status&0377) != 0)
		fprintf(stderr,"Command terminated abnormally.\n");
	times(&buffer);
	fprintf(stderr,"\n");
/**************
	printt("real", (after-before) * 60);
	printt("user", buffer.tms_cutime - obuffer.tms_cutime);
	printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
************/

	ediff(&buffer.tms_etime, &obuffer.tms_etime);
	strcpy(eltime, etime(&buffer.tms_etime));
	strcpy(utime, etime(&buffer.tms_cutime));
	strcpy(sytime, etime(&buffer.tms_cstime));

        fprintf(stderr,"\nreal %s\nuser %s\nsys %s\n",
		eltime, utime, sytime);
	exit(status>>8);
}

