BSD 4 release
[unix-history] / usr / src / cmd / time.c
CommitLineData
31cef89c 1static char *sccsid = "@(#)time.c 4.2 (Berkeley) 10/9/80";
63a84c73
BJ
2/* time command */
3
4#include <stdio.h>
5#include <signal.h>
6#include <sys/types.h>
7#include <sys/times.h>
8
9extern int errno;
10extern char *sys_errlist[];
11
12main(argc, argv)
13char **argv;
14{
15 struct tms buffer, obuffer;
16 int status;
17 register p;
18 time_t before, after;
19
20 if(argc<=1)
21 exit(0);
22 time(&before);
23 p = fork();
24 if(p == -1) {
25 fprintf(stderr, "Try again.\n");
26 exit(1);
27 }
28 if(p == 0) {
29 execvp(argv[1], &argv[1]);
30 fprintf(stderr, "%s: %s\n", argv[1], sys_errlist[errno]);
31 exit(1);
32 }
33 signal(SIGINT, SIG_IGN);
34 signal(SIGQUIT, SIG_IGN);
35 times(&obuffer);
36 while(wait(&status) != p)
37 times(&obuffer);
38 time(&after);
39 if((status&0377) != 0)
40 fprintf(stderr,"Command terminated abnormally.\n");
41 times(&buffer);
63a84c73
BJ
42 printt("real", (after-before) * 60);
43 printt("user", buffer.tms_cutime - obuffer.tms_cutime);
44 printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
064b89c9 45 fprintf(stderr, "\n");
63a84c73
BJ
46 exit(status>>8);
47}
48
49char quant[] = { 6, 10, 10, 6, 10, 6, 10, 10, 10 };
50char *pad = "000 ";
51char *sep = "\0\0.\0:\0:\0\0";
52char *nsep = "\0\0.\0 \0 \0\0";
53
54printt(s, a)
55char *s;
56long a;
57{
58 char digit[9];
59 register i;
60 char c;
61 int nonzero;
62
63 for(i=0; i<9; i++) {
64 digit[i] = a % quant[i];
65 a /= quant[i];
66 }
63a84c73
BJ
67 nonzero = 0;
68 while(--i>0) {
69 c = digit[i]!=0 ? digit[i]+'0':
70 nonzero ? '0':
71 pad[i];
72 if (c)
73 fprintf(stderr,"%c",c);
74 nonzero |= digit[i];
75 c = nonzero?sep[i]:nsep[i];
76 if (c)
77 fprintf(stderr,"%c",c);
78 }
064b89c9 79 fprintf(stderr," %s ",s);
63a84c73 80}