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