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