1 /* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
13 #include <minix/minlib.h>
14 #include <minix/sysinfo.h>
17 /* -DNEW prints time to 0.01 sec. */
28 _PROTOTYPE(int main
, (int argc
, char **argv
));
29 _PROTOTYPE(void print_time
, (clock_t t
));
30 _PROTOTYPE(void twin
, (int n
, char *p
));
31 _PROTOTYPE(void execute
, (void));
38 struct tms pre_buf
, post_buf
;
41 struct timeval start_time
, end_time
;
44 int start_time
, end_time
;
48 if (argc
== 1) exit(0);
53 /* Get real time at start of run. */
55 (void) sysutime(UTIME_TIMEOFDAY
, &start_time
);
57 start_time
= times(&dummy
);
61 if ((pid
= fork()) < 0) {
62 std_err("Cannot fork\n");
65 if (pid
== 0) execute();
67 /* Parent is the time program. Disable interrupts and wait. */
68 signal(SIGINT
, SIG_IGN
);
69 signal(SIGQUIT
, SIG_IGN
);
73 } while (wait(&status
) != pid
);
75 (void) sysutime(UTIME_TIMEOFDAY
, &end_time
);
76 real_time
= (end_time
.tv_sec
- start_time
.tv_sec
) * CLOCKS_PER_SEC
77 + (end_time
.tv_usec
- start_time
.tv_usec
) * CLOCKS_PER_SEC
/ 1000000;
79 end_time
= times(&dummy
);
80 real_time
= (end_time
- start_time
);
83 if ((status
& 0377) != 0) std_err("Command terminated abnormally.\n");
86 /* Print results. -DNEW enables time on one line to 0.01 sec */
89 print_time(real_time
);
91 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
93 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
96 print_time(real_time
);
98 print_time(post_buf
.tms_cutime
- pre_buf
.tms_cutime
);
100 print_time(post_buf
.tms_cstime
- pre_buf
.tms_cstime
);
103 return((status
& 0377) ? -1 : (status
>> 8));
109 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
111 int hours
, minutes
, seconds
, hundredths
, i
;
114 getsysinfo_up(PM_PROC_NR
, SIU_SYSTEMHZ
, sizeof(system_hz
), &system_hz
);
117 for (i
= 0; i
< 8; i
++) a
[i
] = ' ';
118 hours
= (int) (t
/ ((clock_t) 3600 * system_hz
));
119 t
-= (clock_t) hours
* 3600 * system_hz
;
120 minutes
= (int) (t
/ ((clock_t) 60 * system_hz
));
121 t
-= (clock_t) minutes
* 60 * system_hz
;
122 seconds
= (int) (t
/ system_hz
);
123 t
-= (clock_t) seconds
* system_hz
;
124 hundredths
= (int) (t
* 100 / system_hz
);
130 if (minutes
|| digit_seen
) {
131 twin(minutes
, &a
[3]);
134 if (seconds
|| digit_seen
)
135 twin(seconds
, &a
[6]);
138 a
[9] = hundredths
/ 10 + '0';
139 #ifdef HUNDREDTHS /* tenths used to be enough */
140 a
[10] = hundredths
% 10 + '0';
152 if (digit_seen
== 0 && c1
== '0') c1
= ' ';
155 if (n
> 0) digit_seen
= 1;
161 std_err("Cannot execute ");