time: Close an output file -o flag opened before execvp(3) in a child.
[dragonfly.git] / usr.bin / time / time.c
1 /*
2  * Copyright (c) 1987, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1987, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)time.c   8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/time/time.c,v 1.14.2.5 2002/06/28 08:35:15 tjr Exp $
32  * $DragonFly: src/usr.bin/time/time.c,v 1.11 2005/03/04 16:54:37 liamfoy Exp $
33  */
34
35 #include <sys/user.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/signal.h>
39 #include <sys/sysctl.h>
40 #include <sys/wait.h>
41
42 #include <err.h>
43 #include <errno.h>
44 #include <kinfo.h>
45 #include <locale.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 static void humantime(FILE *, long, long);
53 static void usage(void);
54
55 static char decimal_point;
56
57 int
58 main(int argc, char **argv)
59 {
60         pid_t pid;
61         int aflag, ch, hflag, lflag, status, pflag;
62         int exit_on_sig;
63         struct timeval before, after;
64         struct rusage ru;
65         FILE *out = stderr;
66         const char *ofn = NULL;
67
68         setlocale(LC_NUMERIC, "");
69         decimal_point = localeconv()->decimal_point[0];
70
71         aflag = hflag = lflag = pflag = 0;
72         while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
73                 switch (ch) {
74                 case 'a':
75                         aflag = 1;
76                         break;
77                 case 'h':
78                         hflag = 1;
79                         break;
80                 case 'l':
81                         lflag = 1;
82                         break;
83                 case 'o':
84                         ofn = optarg;
85                         break;
86                 case 'p':
87                         pflag = 1;
88                         break;
89                 default:
90                         usage();
91                 }
92
93         if (!(argc -= optind))
94                 exit(0);
95         argv += optind;
96
97         if (ofn) {
98                 if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
99                         err(1, "%s", ofn);
100                 setvbuf(out, NULL, _IONBF, (size_t)0);
101         }
102
103         if (gettimeofday(&before, NULL) == -1)
104                 err(1, "gettimeofday failed");
105         switch (pid = fork()) {
106         case -1:                        /* error */
107                 err(1, "could not fork");
108                 /* NOTREACHED */
109         case 0:                         /* child */
110                 if (ofn)
111                         fclose(out);
112                 execvp(*argv, argv);
113                 err(errno == ENOENT ? 127 : 126, "%s", *argv);
114                 /* NOTREACHED */
115         }
116         /* parent */
117         if (signal(SIGINT, SIG_IGN) == SIG_ERR)
118                 err(1, "signal failed");        
119         if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
120                 err(1, "signal failed");
121         while (wait4(pid, &status, 0, &ru) != pid)              /* XXX use waitpid */
122                 ;
123         if (gettimeofday(&after, NULL) == -1)
124                 err(1, "gettimeofday failed");
125         if (!WIFEXITED(status))
126                 warnx("command terminated abnormally");
127         exit_on_sig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
128         after.tv_sec -= before.tv_sec;
129         after.tv_usec -= before.tv_usec;
130         if (after.tv_usec < 0)
131                 after.tv_sec--, after.tv_usec += 1000000;
132         if (pflag) {
133                 /* POSIX wants output that must look like
134                 "real %f\nuser %f\nsys %f\n" and requires
135                 at least two digits after the radix. */
136                 fprintf(out, "real %ld%c%02ld\n",
137                         after.tv_sec, decimal_point,
138                         after.tv_usec / 10000);
139                 fprintf(out, "user %ld%c%02ld\n",
140                         ru.ru_utime.tv_sec, decimal_point,
141                         ru.ru_utime.tv_usec / 10000);
142                 fprintf(out, "sys %ld%c%02ld\n",
143                         ru.ru_stime.tv_sec, decimal_point,
144                         ru.ru_stime.tv_usec / 10000);
145         } else if (hflag) {
146                 humantime(out, after.tv_sec, after.tv_usec / 10000);
147                 fprintf(out, " real\t");
148                 humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
149                 fprintf(out, " user\t");
150                 humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000);
151                 fprintf(out, " sys\n");
152         } else {
153                 fprintf(out, "%9ld%c%02ld real ",
154                         after.tv_sec, decimal_point,
155                         after.tv_usec / 10000);
156                 fprintf(out, "%9ld%c%02ld user ",
157                         ru.ru_utime.tv_sec, decimal_point,
158                         ru.ru_utime.tv_usec / 10000);
159                 fprintf(out, "%9ld%c%02ld sys\n",
160                         ru.ru_stime.tv_sec, decimal_point,
161                         ru.ru_stime.tv_usec / 10000);
162         }
163         if (lflag) {
164                 int hz;
165                 u_long ticks;
166
167                 if (kinfo_get_sched_stathz(&hz))
168                         err(1, "kinfo_get_sched_stathz");
169                 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
170                      hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
171
172                 /*
173                  * If our round-off on the tick calculation still puts us at 0,
174                  * then always assume at least one tick.
175                  */
176                 if (ticks == 0)
177                         ticks = 1;
178
179                 fprintf(out, "%10ld  %s\n",
180                         ru.ru_maxrss, "maximum resident set size");
181                 fprintf(out, "%10ld  %s\n",
182                         ru.ru_ixrss / ticks, "average shared memory size");
183                 fprintf(out, "%10ld  %s\n",
184                         ru.ru_idrss / ticks, "average unshared data size");
185                 fprintf(out, "%10ld  %s\n",
186                         ru.ru_isrss / ticks, "average unshared stack size");
187                 fprintf(out, "%10ld  %s\n",
188                         ru.ru_minflt, "page reclaims");
189                 fprintf(out, "%10ld  %s\n",
190                         ru.ru_majflt, "page faults");
191                 fprintf(out, "%10ld  %s\n",
192                         ru.ru_nswap, "swaps");
193                 fprintf(out, "%10ld  %s\n",
194                         ru.ru_inblock, "block input operations");
195                 fprintf(out, "%10ld  %s\n",
196                         ru.ru_oublock, "block output operations");
197                 fprintf(out, "%10ld  %s\n",
198                         ru.ru_msgsnd, "messages sent");
199                 fprintf(out, "%10ld  %s\n",
200                         ru.ru_msgrcv, "messages received");
201                 fprintf(out, "%10ld  %s\n",
202                         ru.ru_nsignals, "signals received");
203                 fprintf(out, "%10ld  %s\n",
204                         ru.ru_nvcsw, "voluntary context switches");
205                 fprintf(out, "%10ld  %s\n",
206                         ru.ru_nivcsw, "involuntary context switches");
207         }
208         /*
209          * If the child has exited on a signal, exit on the same
210          * signal, too, in order to reproduce the child's exit
211          * status.  However, avoid actually dumping core from
212          * current process.
213          */
214         if (exit_on_sig) {
215                 if (signal(exit_on_sig, SIG_DFL) == SIG_ERR)
216                         warn("signal failed");
217                 else
218                         kill(getpid(), exit_on_sig);
219         }
220         exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
221 }
222
223 static void
224 usage(void)
225 {
226         fprintf(stderr,
227             "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
228         exit(1);
229 }
230
231 static void
232 humantime(FILE *out, long sec, long usec)
233 {
234         long days, hrs, mins;
235
236         days = sec / (60L * 60 * 24);
237         sec %= (60L * 60 * 24);
238         hrs = sec / (60L * 60);
239         sec %= (60L * 60);
240         mins = sec / 60;
241         sec %= 60;
242
243         fprintf(out, "\t");
244         if (days)
245                 fprintf(out, "%ldd", days);
246         if (hrs)
247                 fprintf(out, "%ldh", hrs);
248         if (mins)
249                 fprintf(out, "%ldm", mins);
250         fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
251 }