xref: /onnv-gate/usr/src/cmd/time/time.c (revision 634:263e46dc023d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*634Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  *	Time a command
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include	<stdio.h>
380Sstevel@tonic-gate #include	<signal.h>
390Sstevel@tonic-gate #include	<errno.h>
400Sstevel@tonic-gate #include	<stdlib.h>
410Sstevel@tonic-gate #include	<unistd.h>
420Sstevel@tonic-gate #include	<libintl.h>
430Sstevel@tonic-gate #include	<locale.h>
440Sstevel@tonic-gate #include	<limits.h>
450Sstevel@tonic-gate #include	<sys/types.h>
460Sstevel@tonic-gate #include	<sys/times.h>
470Sstevel@tonic-gate #include	<sys/wait.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * The following use of HZ/10 will work correctly only if HZ is a multiple
510Sstevel@tonic-gate  * of 10.  However the only values for HZ now in use are 100 for the 3B
520Sstevel@tonic-gate  * and 60 for other machines.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * The first value was HZ/10. Since HZ should be gotten from sysconf()
550Sstevel@tonic-gate  * it is dynamically initialized at entry to the main program.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static clock_t quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 };
580Sstevel@tonic-gate static char *pad  = "000      ";
590Sstevel@tonic-gate static char *sep  = "\0\0.\0:\0:\0\0";
600Sstevel@tonic-gate static char *nsep = "\0\0.\0 \0 \0\0";
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static void usage(void);
630Sstevel@tonic-gate static void printt(char *, clock_t);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int
main(int argc,char ** argv)660Sstevel@tonic-gate main(int argc, char **argv)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	struct tms	buffer;
690Sstevel@tonic-gate 	pid_t		p;
700Sstevel@tonic-gate 	int		status;
710Sstevel@tonic-gate 	int		pflag		= 0;
720Sstevel@tonic-gate 	int		c;
730Sstevel@tonic-gate 	int		clock_tick	= CLK_TCK;
740Sstevel@tonic-gate 	clock_t		before, after;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
770Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
780Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
790Sstevel@tonic-gate #endif
800Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "p")) != EOF)
830Sstevel@tonic-gate 		switch (c) {
840Sstevel@tonic-gate 		case 'p':
850Sstevel@tonic-gate 			pflag++;
860Sstevel@tonic-gate 			break;
870Sstevel@tonic-gate 		case '?':
880Sstevel@tonic-gate 			usage();
890Sstevel@tonic-gate 		}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	argc -= optind;
920Sstevel@tonic-gate 	argv += optind;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * time(1) is only accurate to a tenth of a second.  We need to
960Sstevel@tonic-gate 	 * determine the number of clock ticks in a tenth of a second in
970Sstevel@tonic-gate 	 * order to later divide away what we don't care about.
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	quant[0] = clock_tick/10;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	before = times(&buffer);
1020Sstevel@tonic-gate 	if (argc < 1)
1030Sstevel@tonic-gate 		usage();
1040Sstevel@tonic-gate 	p = fork();
1050Sstevel@tonic-gate 	if (p == (pid_t)-1) {
1060Sstevel@tonic-gate 		perror("time");
1070Sstevel@tonic-gate 		exit(2);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	if (p == (pid_t)0) {
1100Sstevel@tonic-gate 		(void) execvp(argv[0], &argv[0]);
1110Sstevel@tonic-gate 		perror(argv[0]);
1120Sstevel@tonic-gate 		if (errno == ENOENT)
1130Sstevel@tonic-gate 			exit(127);
1140Sstevel@tonic-gate 		else
1150Sstevel@tonic-gate 			exit(126);
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
1180Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
1190Sstevel@tonic-gate 	while (wait(&status) != p);
1200Sstevel@tonic-gate 	if ((status & 0377) != '\0')
1210Sstevel@tonic-gate 		(void) fprintf(stderr, "time: %s\n",
1220Sstevel@tonic-gate 		    gettext("command terminated abnormally."));
1230Sstevel@tonic-gate 	after = times(&buffer);
1240Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
1250Sstevel@tonic-gate 	if (pflag)
1260Sstevel@tonic-gate 		(void) fprintf(stderr, "real %.2f\nuser %.2f\nsys %.2f\n",
1270Sstevel@tonic-gate 		    (double)(after-before)/clock_tick,
1280Sstevel@tonic-gate 		    (double)buffer.tms_cutime/clock_tick,
1290Sstevel@tonic-gate 		    (double)buffer.tms_cstime/clock_tick);
1300Sstevel@tonic-gate 	else {
1310Sstevel@tonic-gate 		printt("real", (after-before));
1320Sstevel@tonic-gate 		printt("user", buffer.tms_cutime);
1330Sstevel@tonic-gate 		printt("sys ", buffer.tms_cstime);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	return ((status & 0xff00)
1370Sstevel@tonic-gate 		? (status >> 8)
1380Sstevel@tonic-gate 		: ((status & 0x00ff) ? ((status & ~WCOREFLG) | 0200) : 0));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static void
printt(char * s,clock_t a)1430Sstevel@tonic-gate printt(char *s, clock_t a)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int i;
1460Sstevel@tonic-gate 	char digit[9];
1470Sstevel@tonic-gate 	char c;
1480Sstevel@tonic-gate 	int nonzero;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	a /= quant[0];	/* Divide away the accuracy we don't care about */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * We now have the number of tenths of seconds elapsed in terms of
1540Sstevel@tonic-gate 	 * ticks. Loop through to determine the actual digits.
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	for (i = 1; i < 9; i++) {
1570Sstevel@tonic-gate 		digit[i] = a % quant[i];
1580Sstevel@tonic-gate 		a /= quant[i];
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	(void) fprintf(stderr, s);
1610Sstevel@tonic-gate 	nonzero = 0;
1620Sstevel@tonic-gate 	while (--i > 0) {
1630Sstevel@tonic-gate 		c = (digit[i] != 0) ? digit[i]+'0' : (nonzero ? '0': pad[i]);
1640Sstevel@tonic-gate 		if (c != '\0')
1650Sstevel@tonic-gate 			(void) putc(c, stderr);
1660Sstevel@tonic-gate 		nonzero |= digit[i];
1670Sstevel@tonic-gate 		c = nonzero?sep[i]:nsep[i];
1680Sstevel@tonic-gate 		if (c != '\0')
1690Sstevel@tonic-gate 			(void) putc(c, stderr);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static void
usage(void)1750Sstevel@tonic-gate usage(void)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 	(void) fprintf(stderr,
1780Sstevel@tonic-gate 	    gettext("usage: time [-p] utility [argument...]\n"));
1790Sstevel@tonic-gate 	exit(1);
1800Sstevel@tonic-gate }
181