xref: /original-bsd/usr.bin/time/time.c (revision 18f6d767)
1 #ifndef lint
2 static	char *sccsid = "@(#)time.c	4.5 (Berkeley) 07/01/83";
3 #endif
4 
5 /*
6  * time
7  */
8 #include <stdio.h>
9 #include <signal.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13 
14 main(argc, argv)
15 	int argc;
16 	char **argv;
17 {
18 	int status;
19 	register int p;
20 	struct timeval before, after;
21 	struct rusage ru;
22 
23 	if (argc<=1)
24 		exit(0);
25 	gettimeofday(&before, 0);
26 	p = fork();
27 	if (p < 0) {
28 		perror("time");
29 		exit(1);
30 	}
31 	if (p == 0) {
32 		execvp(argv[1], &argv[1]);
33 		perror(argv[1]);
34 		exit(1);
35 	}
36 	signal(SIGINT, SIG_IGN);
37 	signal(SIGQUIT, SIG_IGN);
38 	while (wait3(&status, 0, &ru) != p)
39 		;
40 	gettimeofday(&after, 0);
41 	if ((status&0377) != 0)
42 		fprintf(stderr, "Command terminated abnormally.\n");
43 	after.tv_sec -= before.tv_sec;
44 	after.tv_usec -= before.tv_usec;
45 	if (after.tv_usec < 0)
46 		after.tv_sec--, after.tv_usec += 1000000;
47 	printt("real", &after);
48 	printt("user", &ru.ru_utime);
49 	printt("sys ", &ru.ru_stime);
50 	fprintf(stderr, "\n");
51 	exit (status>>8);
52 }
53 
54 printt(s, tv)
55 	char *s;
56 	struct timeval *tv;
57 {
58 
59 	fprintf(stderr, "%9d.%01d %s ", tv->tv_sec, tv->tv_usec/100000, s);
60 }
61