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