xref: /netbsd-src/usr.bin/time/time.c (revision 9aa0541bdf64142d9a27c2cf274394d60182818f)
1 /*	$NetBSD: time.c,v 1.21 2011/08/31 16:24:58 plunky Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1988, 1993\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: time.c,v 1.21 2011/08/31 16:24:58 plunky Exp $");
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/time.h>
47 #include <sys/resource.h>
48 #include <sys/wait.h>
49 #include <errno.h>
50 #include <err.h>
51 #include <locale.h>
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 
57 #include "ext.h"
58 
59 __dead static void	usage(void);
60 static void	prl(long, const char *);
61 static void	prtv(const char *, const char *, const struct timeval *,
62     const char *);
63 
64 int
65 main(int argc, char ** volatile argv)
66 {
67 	int pid;
68 	int ch, status;
69 	int volatile portableflag;
70 	int volatile lflag;
71 	int volatile cshflag;
72 	const char *decpt;
73 	const struct lconv *lconv;
74 	struct timeval before, after;
75 	struct rusage ru;
76 
77 	(void)setlocale(LC_ALL, "");
78 
79 	cshflag = lflag = portableflag = 0;
80 	while ((ch = getopt(argc, argv, "clp")) != -1) {
81 		switch (ch) {
82 		case 'c':
83 			cshflag = 1;
84 			portableflag = 0;
85 			lflag = 0;
86 			break;
87 		case 'p':
88 			portableflag = 1;
89 			cshflag = 0;
90 			lflag = 0;
91 			break;
92 		case 'l':
93 			lflag = 1;
94 			portableflag = 0;
95 			cshflag = 0;
96 			break;
97 		case '?':
98 		default:
99 			usage();
100 		}
101 	}
102 	argc -= optind;
103 	argv += optind;
104 
105 	if (argc < 1)
106 		usage();
107 
108 	gettimeofday(&before, NULL);
109 	switch(pid = vfork()) {
110 	case -1:			/* error */
111 		err(EXIT_FAILURE, "Vfork failed");
112 		/* NOTREACHED */
113 	case 0:				/* child */
114 		/* LINTED will return only on failure */
115 		execvp(*argv, argv);
116 		err((errno == ENOENT) ? 127 : 126, "Can't exec `%s'", *argv);
117 		/* NOTREACHED */
118 	}
119 
120 	/* parent */
121 	(void)signal(SIGINT, SIG_IGN);
122 	(void)signal(SIGQUIT, SIG_IGN);
123 	if ((pid = wait4(pid, &status, 0, &ru)) == -1)
124 		err(EXIT_FAILURE, "wait4 %d failed", pid);
125 	(void)gettimeofday(&after, NULL);
126 	if (!WIFEXITED(status))
127 		warnx("Command terminated abnormally.");
128 	timersub(&after, &before, &after);
129 
130 	if ((lconv = localeconv()) == NULL ||
131 	    (decpt = lconv->decimal_point) == NULL)
132 		decpt = ".";
133 
134 	if (cshflag) {
135 		static struct rusage null_ru;
136 		before.tv_sec = 0;
137 		before.tv_usec = 0;
138 		prusage(stderr, &null_ru, &ru, &after, &before);
139 	} else if (portableflag) {
140 		prtv("real ", decpt, &after, "\n");
141 		prtv("user ", decpt, &ru.ru_utime, "\n");
142 		prtv("sys  ", decpt, &ru.ru_stime, "\n");
143 	} else {
144 		prtv("", decpt, &after, " real ");
145 		prtv("", decpt, &ru.ru_utime, " user ");
146 		prtv("", decpt, &ru.ru_stime, " sys\n");
147 	}
148 
149 	if (lflag) {
150 		int hz = (int)sysconf(_SC_CLK_TCK);
151 		unsigned long long ticks;
152 #define SCALE(x) (long)(ticks ? x / ticks : 0)
153 
154 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
155 		    hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
156 		prl(ru.ru_maxrss, "maximum resident set size");
157 		prl(SCALE(ru.ru_ixrss), "average shared memory size");
158 		prl(SCALE(ru.ru_idrss), "average unshared data size");
159 		prl(SCALE(ru.ru_isrss), "average unshared stack size");
160 		prl(ru.ru_minflt, "page reclaims");
161 		prl(ru.ru_majflt, "page faults");
162 		prl(ru.ru_nswap, "swaps");
163 		prl(ru.ru_inblock, "block input operations");
164 		prl(ru.ru_oublock, "block output operations");
165 		prl(ru.ru_msgsnd, "messages sent");
166 		prl(ru.ru_msgrcv, "messages received");
167 		prl(ru.ru_nsignals, "signals received");
168 		prl(ru.ru_nvcsw, "voluntary context switches");
169 		prl(ru.ru_nivcsw, "involuntary context switches");
170 	}
171 
172 	return (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
173 }
174 
175 static void
176 usage(void)
177 {
178 
179 	(void)fprintf(stderr, "usage: %s [-clp] utility [argument ...]\n",
180 	    getprogname());
181 	exit(EXIT_FAILURE);
182 }
183 
184 static void
185 prl(long val, const char *expn)
186 {
187 
188 	(void)fprintf(stderr, "%10ld  %s\n", val, expn);
189 }
190 
191 static void
192 prtv(const char *pre, const char *decpt, const struct timeval *tv,
193     const char *post)
194 {
195 
196 	(void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt,
197 	    (long)tv->tv_usec / 10000, post);
198 }
199