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