[9] | 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 |
|
---|
| 21 | char **args;
|
---|
| 22 | char *name;
|
---|
| 23 |
|
---|
| 24 | int digit_seen;
|
---|
| 25 | char 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 |
|
---|
| 32 | int main(argc, argv)
|
---|
| 33 | int argc;
|
---|
| 34 | char *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 |
|
---|
| 105 | void print_time(t)
|
---|
| 106 | register 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 |
|
---|
| 141 | void twin(n, p)
|
---|
| 142 | int n;
|
---|
| 143 | char *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 |
|
---|
| 154 | void execute()
|
---|
| 155 | {
|
---|
| 156 | execvp(name, args);
|
---|
| 157 | std_err("Cannot execute ");
|
---|
| 158 | std_err(name);
|
---|
| 159 | std_err("\n");
|
---|
| 160 | exit(-1);
|
---|
| 161 | }
|
---|