xref: /csrg-svn/usr.bin/time/time.c (revision 9228)
1*9228Ssam static	char *sccsid = "@(#)time.c	4.3 (Berkeley) 11/14/82";
21127Sbill 
3*9228Ssam /*
4*9228Ssam  * time
5*9228Ssam  */
61127Sbill #include <stdio.h>
71127Sbill #include <signal.h>
81127Sbill #include <sys/types.h>
9*9228Ssam #include <time.h>
10*9228Ssam #include <resource.h>
111127Sbill 
121127Sbill main(argc, argv)
13*9228Ssam 	int argc;
14*9228Ssam 	char **argv;
151127Sbill {
161127Sbill 	int status;
17*9228Ssam 	register int p;
18*9228Ssam 	struct timeval before, after;
19*9228Ssam 	struct rusage ru;
201127Sbill 
21*9228Ssam 	if (argc<=1)
221127Sbill 		exit(0);
23*9228Ssam 	gettimeofday(&before, 0);
241127Sbill 	p = fork();
25*9228Ssam 	if (p < 0) {
26*9228Ssam 		perror("time");
271127Sbill 		exit(1);
281127Sbill 	}
29*9228Ssam 	if (p == 0) {
301127Sbill 		execvp(argv[1], &argv[1]);
31*9228Ssam 		perror(argv[1]);
321127Sbill 		exit(1);
331127Sbill 	}
341127Sbill 	signal(SIGINT, SIG_IGN);
351127Sbill 	signal(SIGQUIT, SIG_IGN);
36*9228Ssam 	while (wait3(&status, 0, &ru) != p)
37*9228Ssam 		;
38*9228Ssam 	gettimeofday(&after, 0);
39*9228Ssam 	if ((status&0377) != 0)
40*9228Ssam 		fprintf(stderr, "Command terminated abnormally.\n");
41*9228Ssam 	after.tv_sec -= before.tv_sec;
42*9228Ssam 	after.tv_usec -= before.tv_usec;
43*9228Ssam 	if (after.tv_usec < 0)
44*9228Ssam 		after.tv_sec--, after.tv_usec += 1000000;
45*9228Ssam 	printt("real", &after);
46*9228Ssam 	printt("user", &ru.ru_utime);
47*9228Ssam 	printt("sys ", &ru.ru_stime);
481370Sbill 	fprintf(stderr, "\n");
49*9228Ssam 	exit (status>>8);
501127Sbill }
511127Sbill 
52*9228Ssam printt(s, tv)
53*9228Ssam 	char *s;
54*9228Ssam 	struct timeval *tv;
551127Sbill {
561127Sbill 
57*9228Ssam 	fprintf(stderr, "%9d.%01d %s ", tv->tv_sec, tv->tv_usec/100000, s);
581127Sbill }
59