xref: /csrg-svn/usr.bin/time/time.c (revision 12900)
1*12900Ssam #ifndef lint
2*12900Ssam static	char *sccsid = "@(#)time.c	4.4 (Berkeley) 06/02/83";
3*12900Ssam #endif
41127Sbill 
59228Ssam /*
69228Ssam  * time
79228Ssam  */
81127Sbill #include <stdio.h>
91127Sbill #include <signal.h>
101127Sbill #include <sys/types.h>
119228Ssam #include <time.h>
129228Ssam #include <resource.h>
131127Sbill 
141127Sbill main(argc, argv)
159228Ssam 	int argc;
169228Ssam 	char **argv;
171127Sbill {
181127Sbill 	int status;
199228Ssam 	register int p;
209228Ssam 	struct timeval before, after;
219228Ssam 	struct rusage ru;
221127Sbill 
239228Ssam 	if (argc<=1)
241127Sbill 		exit(0);
259228Ssam 	gettimeofday(&before, 0);
261127Sbill 	p = fork();
279228Ssam 	if (p < 0) {
289228Ssam 		perror("time");
291127Sbill 		exit(1);
301127Sbill 	}
319228Ssam 	if (p == 0) {
321127Sbill 		execvp(argv[1], &argv[1]);
339228Ssam 		perror(argv[1]);
341127Sbill 		exit(1);
351127Sbill 	}
361127Sbill 	signal(SIGINT, SIG_IGN);
371127Sbill 	signal(SIGQUIT, SIG_IGN);
389228Ssam 	while (wait3(&status, 0, &ru) != p)
399228Ssam 		;
409228Ssam 	gettimeofday(&after, 0);
419228Ssam 	if ((status&0377) != 0)
429228Ssam 		fprintf(stderr, "Command terminated abnormally.\n");
439228Ssam 	after.tv_sec -= before.tv_sec;
449228Ssam 	after.tv_usec -= before.tv_usec;
459228Ssam 	if (after.tv_usec < 0)
469228Ssam 		after.tv_sec--, after.tv_usec += 1000000;
479228Ssam 	printt("real", &after);
489228Ssam 	printt("user", &ru.ru_utime);
499228Ssam 	printt("sys ", &ru.ru_stime);
501370Sbill 	fprintf(stderr, "\n");
519228Ssam 	exit (status>>8);
521127Sbill }
531127Sbill 
549228Ssam printt(s, tv)
559228Ssam 	char *s;
569228Ssam 	struct timeval *tv;
571127Sbill {
581127Sbill 
599228Ssam 	fprintf(stderr, "%9d.%01d %s ", tv->tv_sec, tv->tv_usec/100000, s);
601127Sbill }
61