forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / simple / time.c
blob7860062b56f23c2e370594ab1fe6129140b9f021
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/sysinfo.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 _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));
33 int main(argc, argv)
34 int argc;
35 char *argv[];
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 clock_t real_time;
48 if (argc == 1) exit(0);
50 args = &argv[1];
51 name = argv[1];
53 /* Get real time at start of run. */
54 #if _VMD_EXT
55 (void) sysutime(UTIME_TIMEOFDAY, &start_time);
56 #else
57 start_time = times(&dummy);
58 #endif
60 /* Fork off child. */
61 if ((pid = fork()) < 0) {
62 std_err("Cannot fork\n");
63 exit(1);
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);
71 do {
72 times(&pre_buf);
73 } while (wait(&status) != pid);
74 #if _VMD_EXT
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;
78 #else
79 end_time = times(&dummy);
80 real_time = (end_time - start_time);
81 #endif
83 if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
84 times(&post_buf);
86 /* Print results. -DNEW enables time on one line to 0.01 sec */
87 #ifndef NEW
88 std_err("real ");
89 print_time(real_time);
90 std_err("\nuser ");
91 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
92 std_err("\nsys ");
93 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
94 std_err("\n");
95 #else
96 print_time(real_time);
97 std_err(" real");
98 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
99 std_err(" user");
100 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
101 std_err(" sys\n");
102 #endif
103 return((status & 0377) ? -1 : (status >> 8));
106 void print_time(t)
107 register clock_t t;
109 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
111 int hours, minutes, seconds, hundredths, i;
112 u32_t system_hz;
114 getsysinfo_up(PM_PROC_NR, SIU_SYSTEMHZ, sizeof(system_hz), &system_hz);
116 digit_seen = 0;
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);
126 if (hours) {
127 twin(hours, &a[0]);
128 a[2] = ':';
130 if (minutes || digit_seen) {
131 twin(minutes, &a[3]);
132 a[5] = ':';
134 if (seconds || digit_seen)
135 twin(seconds, &a[6]);
136 else
137 a[7] = '0';
138 a[9] = hundredths / 10 + '0';
139 #ifdef HUNDREDTHS /* tenths used to be enough */
140 a[10] = hundredths % 10 + '0';
141 #endif
142 std_err(a);
145 void twin(n, p)
146 int n;
147 char *p;
149 char c1, c2;
150 c1 = (n / 10) + '0';
151 c2 = (n % 10) + '0';
152 if (digit_seen == 0 && c1 == '0') c1 = ' ';
153 *p++ = c1;
154 *p++ = c2;
155 if (n > 0) digit_seen = 1;
158 void execute()
160 execvp(name, args);
161 std_err("Cannot execute ");
162 std_err(name);
163 std_err("\n");
164 exit(-1);