1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #pragma ident "%Z%%M% %I% %E% SMI" 32 33 /* 34 * Time a command 35 */ 36 37 #include <stdio.h> 38 #include <signal.h> 39 #include <errno.h> 40 #include <stdlib.h> 41 #include <unistd.h> 42 #include <libintl.h> 43 #include <locale.h> 44 #include <limits.h> 45 #include <sys/types.h> 46 #include <sys/times.h> 47 #include <sys/wait.h> 48 49 /* 50 * The following use of HZ/10 will work correctly only if HZ is a multiple 51 * of 10. However the only values for HZ now in use are 100 for the 3B 52 * and 60 for other machines. 53 * 54 * The first value was HZ/10. Since HZ should be gotten from sysconf() 55 * it is dynamically initialized at entry to the main program. 56 */ 57 static clock_t quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 }; 58 static char *pad = "000 "; 59 static char *sep = "\0\0.\0:\0:\0\0"; 60 static char *nsep = "\0\0.\0 \0 \0\0"; 61 62 static void usage(void); 63 static void printt(char *, clock_t); 64 extern char *sys_errlist[]; 65 66 int 67 main(int argc, char **argv) 68 { 69 struct tms buffer; 70 pid_t p; 71 extern int errno; 72 int status; 73 int pflag = 0; 74 int c; 75 int clock_tick = CLK_TCK; 76 clock_t before, after; 77 78 (void) setlocale(LC_ALL, ""); 79 #if !defined(TEXT_DOMAIN) 80 #define TEXT_DOMAIN "SYS_TEST" 81 #endif 82 (void) textdomain(TEXT_DOMAIN); 83 84 while ((c = getopt(argc, argv, "p")) != EOF) 85 switch (c) { 86 case 'p': 87 pflag++; 88 break; 89 case '?': 90 usage(); 91 } 92 93 argc -= optind; 94 argv += optind; 95 96 /* 97 * time(1) is only accurate to a tenth of a second. We need to 98 * determine the number of clock ticks in a tenth of a second in 99 * order to later divide away what we don't care about. 100 */ 101 quant[0] = clock_tick/10; 102 103 before = times(&buffer); 104 if (argc < 1) 105 usage(); 106 p = fork(); 107 if (p == (pid_t)-1) { 108 perror("time"); 109 exit(2); 110 } 111 if (p == (pid_t)0) { 112 (void) execvp(argv[0], &argv[0]); 113 perror(argv[0]); 114 if (errno == ENOENT) 115 exit(127); 116 else 117 exit(126); 118 } 119 (void) signal(SIGINT, SIG_IGN); 120 (void) signal(SIGQUIT, SIG_IGN); 121 while (wait(&status) != p); 122 if ((status & 0377) != '\0') 123 (void) fprintf(stderr, "time: %s\n", 124 gettext("command terminated abnormally.")); 125 after = times(&buffer); 126 (void) fprintf(stderr, "\n"); 127 if (pflag) 128 (void) fprintf(stderr, "real %.2f\nuser %.2f\nsys %.2f\n", 129 (double)(after-before)/clock_tick, 130 (double)buffer.tms_cutime/clock_tick, 131 (double)buffer.tms_cstime/clock_tick); 132 else { 133 printt("real", (after-before)); 134 printt("user", buffer.tms_cutime); 135 printt("sys ", buffer.tms_cstime); 136 } 137 138 return ((status & 0xff00) 139 ? (status >> 8) 140 : ((status & 0x00ff) ? ((status & ~WCOREFLG) | 0200) : 0)); 141 } 142 143 144 static void 145 printt(char *s, clock_t a) 146 { 147 int i; 148 char digit[9]; 149 char c; 150 int nonzero; 151 152 a /= quant[0]; /* Divide away the accuracy we don't care about */ 153 154 /* 155 * We now have the number of tenths of seconds elapsed in terms of 156 * ticks. Loop through to determine the actual digits. 157 */ 158 for (i = 1; i < 9; i++) { 159 digit[i] = a % quant[i]; 160 a /= quant[i]; 161 } 162 (void) fprintf(stderr, s); 163 nonzero = 0; 164 while (--i > 0) { 165 c = (digit[i] != 0) ? digit[i]+'0' : (nonzero ? '0': pad[i]); 166 if (c != '\0') 167 (void) putc(c, stderr); 168 nonzero |= digit[i]; 169 c = nonzero?sep[i]:nsep[i]; 170 if (c != '\0') 171 (void) putc(c, stderr); 172 } 173 (void) fprintf(stderr, "\n"); 174 } 175 176 static void 177 usage(void) 178 { 179 (void) fprintf(stderr, 180 gettext("usage: time [-p] utility [argument...]\n")); 181 exit(1); 182 } 183