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 2005 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 65 int 66 main(int argc, char **argv) 67 { 68 struct tms buffer; 69 pid_t p; 70 int status; 71 int pflag = 0; 72 int c; 73 int clock_tick = CLK_TCK; 74 clock_t before, after; 75 76 (void) setlocale(LC_ALL, ""); 77 #if !defined(TEXT_DOMAIN) 78 #define TEXT_DOMAIN "SYS_TEST" 79 #endif 80 (void) textdomain(TEXT_DOMAIN); 81 82 while ((c = getopt(argc, argv, "p")) != EOF) 83 switch (c) { 84 case 'p': 85 pflag++; 86 break; 87 case '?': 88 usage(); 89 } 90 91 argc -= optind; 92 argv += optind; 93 94 /* 95 * time(1) is only accurate to a tenth of a second. We need to 96 * determine the number of clock ticks in a tenth of a second in 97 * order to later divide away what we don't care about. 98 */ 99 quant[0] = clock_tick/10; 100 101 before = times(&buffer); 102 if (argc < 1) 103 usage(); 104 p = fork(); 105 if (p == (pid_t)-1) { 106 perror("time"); 107 exit(2); 108 } 109 if (p == (pid_t)0) { 110 (void) execvp(argv[0], &argv[0]); 111 perror(argv[0]); 112 if (errno == ENOENT) 113 exit(127); 114 else 115 exit(126); 116 } 117 (void) signal(SIGINT, SIG_IGN); 118 (void) signal(SIGQUIT, SIG_IGN); 119 while (wait(&status) != p); 120 if ((status & 0377) != '\0') 121 (void) fprintf(stderr, "time: %s\n", 122 gettext("command terminated abnormally.")); 123 after = times(&buffer); 124 (void) fprintf(stderr, "\n"); 125 if (pflag) 126 (void) fprintf(stderr, "real %.2f\nuser %.2f\nsys %.2f\n", 127 (double)(after-before)/clock_tick, 128 (double)buffer.tms_cutime/clock_tick, 129 (double)buffer.tms_cstime/clock_tick); 130 else { 131 printt("real", (after-before)); 132 printt("user", buffer.tms_cutime); 133 printt("sys ", buffer.tms_cstime); 134 } 135 136 return ((status & 0xff00) 137 ? (status >> 8) 138 : ((status & 0x00ff) ? ((status & ~WCOREFLG) | 0200) : 0)); 139 } 140 141 142 static void 143 printt(char *s, clock_t a) 144 { 145 int i; 146 char digit[9]; 147 char c; 148 int nonzero; 149 150 a /= quant[0]; /* Divide away the accuracy we don't care about */ 151 152 /* 153 * We now have the number of tenths of seconds elapsed in terms of 154 * ticks. Loop through to determine the actual digits. 155 */ 156 for (i = 1; i < 9; i++) { 157 digit[i] = a % quant[i]; 158 a /= quant[i]; 159 } 160 (void) fprintf(stderr, s); 161 nonzero = 0; 162 while (--i > 0) { 163 c = (digit[i] != 0) ? digit[i]+'0' : (nonzero ? '0': pad[i]); 164 if (c != '\0') 165 (void) putc(c, stderr); 166 nonzero |= digit[i]; 167 c = nonzero?sep[i]:nsep[i]; 168 if (c != '\0') 169 (void) putc(c, stderr); 170 } 171 (void) fprintf(stderr, "\n"); 172 } 173 174 static void 175 usage(void) 176 { 177 (void) fprintf(stderr, 178 gettext("usage: time [-p] utility [argument...]\n")); 179 exit(1); 180 } 181