Merge commit '1d9a8ab82e5abe86cb1e43c502f88c7c655658fd'
[unleashed.git] / bin / time / time.c
blobe6dd3d5820f827ca18098222547113ee63b3ec40
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
34 * Time a command
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>
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.
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.
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";
62 static void usage(void);
63 static void printt(char *, clock_t);
65 int
66 main(int argc, char **argv)
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;
76 (void) setlocale(LC_ALL, "");
77 #if !defined(TEXT_DOMAIN)
78 #define TEXT_DOMAIN "SYS_TEST"
79 #endif
80 (void) textdomain(TEXT_DOMAIN);
82 while ((c = getopt(argc, argv, "p")) != EOF)
83 switch (c) {
84 case 'p':
85 pflag++;
86 break;
87 case '?':
88 usage();
91 argc -= optind;
92 argv += optind;
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.
99 quant[0] = clock_tick/10;
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);
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);
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 if (pflag)
125 (void) fprintf(stderr, "real %.2f\nuser %.2f\nsys %.2f\n",
126 (double)(after-before)/clock_tick,
127 (double)buffer.tms_cutime/clock_tick,
128 (double)buffer.tms_cstime/clock_tick);
129 else {
130 printt("real", (after-before));
131 printt("user", buffer.tms_cutime);
132 printt("sys ", buffer.tms_cstime);
135 return ((status & 0xff00)
136 ? (status >> 8)
137 : ((status & 0x00ff) ? ((status & ~WCOREFLG) | 0200) : 0));
141 static void
142 printt(char *s, clock_t a)
144 int i;
145 char digit[9];
146 char c;
147 int nonzero;
149 a /= quant[0]; /* Divide away the accuracy we don't care about */
152 * We now have the number of tenths of seconds elapsed in terms of
153 * ticks. Loop through to determine the actual digits.
155 for (i = 1; i < 9; i++) {
156 digit[i] = a % quant[i];
157 a /= quant[i];
159 (void) fprintf(stderr, s);
160 nonzero = 0;
161 while (--i > 0) {
162 c = (digit[i] != 0) ? digit[i]+'0' : (nonzero ? '0': pad[i]);
163 if (c != '\0')
164 (void) putc(c, stderr);
165 nonzero |= digit[i];
166 c = nonzero?sep[i]:nsep[i];
167 if (c != '\0')
168 (void) putc(c, stderr);
170 (void) fprintf(stderr, "\n");
173 static void
174 usage(void)
176 (void) fprintf(stderr,
177 gettext("usage: time [-p] utility [argument...]\n"));
178 exit(1);