opendir change: refinement
[minix.git] / commands / time / time.c
blob651d244c639a0dc076071590bf085f8cda33ebe3
1 /* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
3 #define NEW 1
5 #include <sys/types.h>
6 #include <sys/times.h>
7 #include <limits.h>
8 #include <time.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/wait.h>
13 #include <minix/minlib.h>
14 // #include <minix/u64.h>
15 #include <stdio.h>
17 /* -DNEW prints time to 0.01 sec. */
18 #ifdef NEW
19 #define HUNDREDTHS 1
20 #endif
22 char **args;
23 char *name;
25 int digit_seen;
26 char a[] = " . \0";
28 int main(int argc, char **argv);
29 void print_time(clock_t t);
30 void twin(int n, char *p);
31 void execute(void);
33 int main(argc, argv)
34 int argc;
35 char *argv[];
37 int cycles = 0;
38 struct tms pre_buf, post_buf;
39 int status, pid;
40 #if _VMD_EXT
41 struct timeval start_time, end_time;
42 #else
43 struct tms dummy;
44 int start_time, end_time;
45 #endif
46 u64_t start_tsc, end_tsc, spent_tsc;
47 clock_t real_time;
48 int c;
50 if (argc == 1) exit(0);
52 while((c=getopt(argc, argv, "C")) != EOF) {
53 switch(c) {
54 case 'C':
55 cycles = 1;
56 break;
57 default:
58 fprintf(stderr, "usage: time [-C] <command>\n");
59 exit(1);
63 argv += optind;
64 argc -= optind;
66 args = &argv[0];
67 name = argv[0];
69 /* Get real time at start of run. */
70 #if _VMD_EXT
71 (void) sysutime(UTIME_TIMEOFDAY, &start_time);
72 #else
73 start_time = times(&dummy);
74 #endif
75 read_tsc_64(&start_tsc);
77 /* Fork off child. */
78 if ((pid = fork()) < 0) {
79 std_err("Cannot fork\n");
80 exit(1);
82 if (pid == 0) execute();
84 /* Parent is the time program. Disable interrupts and wait. */
85 signal(SIGINT, SIG_IGN);
86 signal(SIGQUIT, SIG_IGN);
88 do {
89 times(&pre_buf);
90 } while (wait(&status) != pid);
91 read_tsc_64(&end_tsc);
92 spent_tsc = end_tsc - start_tsc;
93 #if _VMD_EXT
94 (void) sysutime(UTIME_TIMEOFDAY, &end_time);
95 real_time = (end_time.tv_sec - start_time.tv_sec) * CLOCKS_PER_SEC
96 + (end_time.tv_usec - start_time.tv_usec) * CLOCKS_PER_SEC / 1000000;
97 #else
98 end_time = times(&dummy);
99 real_time = (end_time - start_time);
100 #endif
102 if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
103 times(&post_buf);
105 if(cycles) {
106 fprintf(stderr, "%qd tsc ", spent_tsc);
108 /* Print results. -DNEW enables time on one line to 0.01 sec */
109 #ifndef NEW
110 std_err("real ");
111 print_time(real_time);
112 std_err("\nuser ");
113 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
114 std_err("\nsys ");
115 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
116 std_err("\n");
117 #else
118 print_time(real_time);
119 std_err(" real");
120 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
121 std_err(" user");
122 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
123 std_err(" sys\n");
124 #endif
125 return((status & 0377) ? -1 : (status >> 8));
128 void print_time(t)
129 register clock_t t;
131 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
133 int hours, minutes, seconds, hundredths, i;
134 u32_t system_hz;
136 system_hz = (u32_t) sysconf(_SC_CLK_TCK);
138 digit_seen = 0;
139 for (i = 0; i < 8; i++) a[i] = ' ';
140 hours = (int) (t / ((clock_t) 3600 * system_hz));
141 t -= (clock_t) hours * 3600 * system_hz;
142 minutes = (int) (t / ((clock_t) 60 * system_hz));
143 t -= (clock_t) minutes * 60 * system_hz;
144 seconds = (int) (t / system_hz);
145 t -= (clock_t) seconds * system_hz;
146 hundredths = (int) (t * 100 / system_hz);
148 if (hours) {
149 twin(hours, &a[0]);
150 a[2] = ':';
152 if (minutes || digit_seen) {
153 twin(minutes, &a[3]);
154 a[5] = ':';
156 if (seconds || digit_seen)
157 twin(seconds, &a[6]);
158 else
159 a[7] = '0';
160 a[9] = hundredths / 10 + '0';
161 #ifdef HUNDREDTHS /* tenths used to be enough */
162 a[10] = hundredths % 10 + '0';
163 #endif
164 std_err(a);
167 void twin(n, p)
168 int n;
169 char *p;
171 char c1, c2;
172 c1 = (n / 10) + '0';
173 c2 = (n % 10) + '0';
174 if (digit_seen == 0 && c1 == '0') c1 = ' ';
175 *p++ = c1;
176 *p++ = c2;
177 if (n > 0) digit_seen = 1;
180 void execute()
182 execvp(name, args);
183 std_err("Cannot execute ");
184 std_err(name);
185 std_err("\n");
186 exit(-1);