source: trunk/minix/commands/simple/time.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.4 KB
Line 
1/* time - time a command Authors: Andy Tanenbaum & Michiel Huisjes */
2
3#define NEW 1
4
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>
15
16/* -DNEW prints time to 0.01 sec. */
17#ifdef NEW
18#define HUNDREDTHS 1
19#endif
20
21char **args;
22char *name;
23
24int digit_seen;
25char a[] = " . \0";
26
27_PROTOTYPE(int main, (int argc, char **argv));
28_PROTOTYPE(void print_time, (clock_t t));
29_PROTOTYPE(void twin, (int n, char *p));
30_PROTOTYPE(void execute, (void));
31
32int main(argc, argv)
33int argc;
34char *argv[];
35{
36
37 struct tms pre_buf, post_buf;
38 int status, pid;
39#if _VMD_EXT
40 struct timeval start_time, end_time;
41#else
42 struct tms dummy;
43 int start_time, end_time;
44#endif
45 clock_t real_time;
46
47 if (argc == 1) exit(0);
48
49 args = &argv[1];
50 name = argv[1];
51
52 /* Get real time at start of run. */
53#if _VMD_EXT
54 (void) sysutime(UTIME_TIMEOFDAY, &start_time);
55#else
56 start_time = times(&dummy);
57#endif
58
59 /* Fork off child. */
60 if ((pid = fork()) < 0) {
61 std_err("Cannot fork\n");
62 exit(1);
63 }
64 if (pid == 0) execute();
65
66 /* Parent is the time program. Disable interrupts and wait. */
67 signal(SIGINT, SIG_IGN);
68 signal(SIGQUIT, SIG_IGN);
69
70 do {
71 times(&pre_buf);
72 } while (wait(&status) != pid);
73#if _VMD_EXT
74 (void) sysutime(UTIME_TIMEOFDAY, &end_time);
75 real_time = (end_time.tv_sec - start_time.tv_sec) * CLOCKS_PER_SEC
76 + (end_time.tv_usec - start_time.tv_usec) * CLOCKS_PER_SEC / 1000000;
77#else
78 end_time = times(&dummy);
79 real_time = (end_time - start_time);
80#endif
81
82 if ((status & 0377) != 0) std_err("Command terminated abnormally.\n");
83 times(&post_buf);
84
85 /* Print results. -DNEW enables time on one line to 0.01 sec */
86#ifndef NEW
87 std_err("real ");
88 print_time(real_time);
89 std_err("\nuser ");
90 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
91 std_err("\nsys ");
92 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
93 std_err("\n");
94#else
95 print_time(real_time);
96 std_err(" real");
97 print_time(post_buf.tms_cutime - pre_buf.tms_cutime);
98 std_err(" user");
99 print_time(post_buf.tms_cstime - pre_buf.tms_cstime);
100 std_err(" sys\n");
101#endif
102 return((status & 0377) ? -1 : (status >> 8));
103}
104
105void print_time(t)
106register clock_t t;
107{
108/* Print the time 't' in hours: minutes: seconds. 't' is in ticks. */
109
110 int hours, minutes, seconds, hundredths, i;
111
112 digit_seen = 0;
113 for (i = 0; i < 8; i++) a[i] = ' ';
114 hours = (int) (t / ((clock_t) 3600 * CLOCKS_PER_SEC));
115 t -= (clock_t) hours * 3600 * CLOCKS_PER_SEC;
116 minutes = (int) (t / ((clock_t) 60 * CLOCKS_PER_SEC));
117 t -= (clock_t) minutes * 60 * CLOCKS_PER_SEC;
118 seconds = (int) (t / CLOCKS_PER_SEC);
119 t -= (clock_t) seconds * CLOCKS_PER_SEC;
120 hundredths = (int) (t * 100 / CLOCKS_PER_SEC);
121
122 if (hours) {
123 twin(hours, &a[0]);
124 a[2] = ':';
125 }
126 if (minutes || digit_seen) {
127 twin(minutes, &a[3]);
128 a[5] = ':';
129 }
130 if (seconds || digit_seen)
131 twin(seconds, &a[6]);
132 else
133 a[7] = '0';
134 a[9] = hundredths / 10 + '0';
135#ifdef HUNDREDTHS /* tenths used to be enough */
136 a[10] = hundredths % 10 + '0';
137#endif
138 std_err(a);
139}
140
141void twin(n, p)
142int n;
143char *p;
144{
145 char c1, c2;
146 c1 = (n / 10) + '0';
147 c2 = (n % 10) + '0';
148 if (digit_seen == 0 && c1 == '0') c1 = ' ';
149 *p++ = c1;
150 *p++ = c2;
151 if (n > 0) digit_seen = 1;
152}
153
154void execute()
155{
156 execvp(name, args);
157 std_err("Cannot execute ");
158 std_err(name);
159 std_err("\n");
160 exit(-1);
161}
Note: See TracBrowser for help on using the repository browser.