tools/llvm: Do not build with symbols
[minix3.git] / minix / commands / time / time.c
blob3fb975c22050bc3ff5f211617f8a04f412f85d4c
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 <stdio.h>
16 /* -DNEW prints time to 0.01 sec. */
17 #ifdef NEW
18 #define HUNDREDTHS 1
19 #endif
21 char **args;
22 char *name;
24 int digit_seen;
25 char a[] = " . \0";
27 int main(int argc, char **argv);
28 void print_time(clock_t t);
29 void twin(int n, char *p);
30 void execute(void);
32 int main(argc, argv)
33 int argc;
34 char *argv[];
36 int cycles = 0;
37 struct tms pre_buf, post_buf;
38 int status, pid;
39 struct tms dummy;
40 int start_time, end_time;
41 u64_t start_tsc, end_tsc, spent_tsc;
42 clock_t real_time;
43 int c;
45 if (argc == 1) exit(0);
47 while((c=getopt(argc, argv, "C")) != EOF) {
48 switch(c) {
49 case 'C':
50 cycles = 1;
51 break;
52 default:
53 fprintf(stderr, "usage: time [-C] <command>\n");
54 exit(1);
58 argv += optind;
59 argc -= optind;
61 args = &argv[0];
62 name = argv[0];
64 /* Get real time at start of run. */
65 start_time = times(&dummy);
66 read_tsc_64(&start_tsc);
68 /* Fork off child. */
69 if ((pid = fork()) < 0) {
70 std_err("Cannot fork\n");
71 exit(1);
73 if (pid == 0) execute();
75 /* Parent is the time program. Disable interrupts and wait. */
76 signal(SIGINT, SIG_IGN);
77 signal(SIGQUIT, SIG_IGN);
79 do {
80 times(&pre_buf);
81 } while (wait(&status) != pid);
82 read_tsc_64(&end_tsc);
83 spent_tsc = end_tsc - start_tsc;
84 end_time = times(&dummy);
85 real_time = (end_time - start_time);
87 if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
88 times(&post_buf);
90 if(cycles) {
91 fprintf(stderr, "%qd tsc ", spent_tsc);
93 /* Print results. -DNEW enables time on one line to 0.01 sec */
94 #ifndef NEW
95 std_err("real ");
96 print_time(real_time);
97 std_err("\nuser ");
98 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
99 std_err("\nsys ");
100 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
101 std_err("\n");
102 #else
103 print_time(real_time);
104 std_err(" real");
105 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
106 std_err(" user");
107 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
108 std_err(" sys\n");
109 #endif
110 return((status & 0377) ? -1 : (status >> 8));
113 void print_time(t)
114 register clock_t t;
116 /* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
118 int hours, minutes, seconds, hundredths, i;
119 u32_t system_hz;
121 system_hz = (u32_t) sysconf(_SC_CLK_TCK);
123 digit_seen = 0;
124 for (i = 0; i < 8; i++) a[i] = ' ';
125 hours = (int) (t / ((clock_t) 3600 * system_hz));
126 t -= (clock_t) hours * 3600 * system_hz;
127 minutes = (int) (t / ((clock_t) 60 * system_hz));
128 t -= (clock_t) minutes * 60 * system_hz;
129 seconds = (int) (t / system_hz);
130 t -= (clock_t) seconds * system_hz;
131 hundredths = (int) (t * 100 / system_hz);
133 if (hours) {
134 twin(hours, &a[0]);
135 a[2] = ':';
137 if (minutes || digit_seen) {
138 twin(minutes, &a[3]);
139 a[5] = ':';
141 if (seconds || digit_seen)
142 twin(seconds, &a[6]);
143 else
144 a[7] = '0';
145 a[9] = hundredths / 10 + '0';
146 #ifdef HUNDREDTHS /* tenths used to be enough */
147 a[10] = hundredths % 10 + '0';
148 #endif
149 std_err(a);
152 void twin(n, p)
153 int n;
154 char *p;
156 char c1, c2;
157 c1 = (n / 10) + '0';
158 c2 = (n % 10) + '0';
159 if (digit_seen == 0 && c1 == '0') c1 = ' ';
160 *p++ = c1;
161 *p++ = c2;
162 if (n > 0) digit_seen = 1;
165 void execute()
167 execvp(name, args);
168 std_err("Cannot execute ");
169 std_err(name);
170 std_err("\n");
171 exit(-1);