Annotation of src/usr.bin/time/time.c, Revision 1.3
1.3 ! deraadt 1: /* $OpenBSD: time.c,v 1.2 1996/06/26 05:40:39 deraadt 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.3 ! deraadt 47: static char rcsid[] = "$OpenBSD: time.c,v 1.2 1996/06/26 05:40:39 deraadt 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;
73:
74: lflag = 0;
75: while ((ch = getopt(argc, argv, "lp")) != EOF)
76: switch((char)ch) {
77: case 'p':
78: portableflag = 1;
79: break;
80: case 'l':
81: lflag = 1;
82: break;
83: case '?':
84: default:
85: fprintf(stderr, "usage: time [-lp] command.\n");
86: exit(1);
87: }
88:
89: if (!(argc -= optind))
90: exit(0);
91: argv += optind;
92:
93: gettimeofday(&before, (struct timezone *)NULL);
94: switch(pid = vfork()) {
95: case -1: /* error */
96: perror("time");
97: exit(1);
98: /* NOTREACHED */
99: case 0: /* child */
100: execvp(*argv, argv);
101: perror(*argv);
102: _exit((errno == ENOENT) ? 127 : 126);
103: /* NOTREACHED */
104: }
105:
106: /* parent */
107: (void)signal(SIGINT, SIG_IGN);
108: (void)signal(SIGQUIT, SIG_IGN);
1.3 ! deraadt 109: while (wait3(&status, 0, &ru) != pid)
! 110: ;
1.1 deraadt 111: gettimeofday(&after, (struct timezone *)NULL);
112: if (!WIFEXITED(status))
113: fprintf(stderr, "Command terminated abnormally.\n");
114: timersub(&after, &before, &after);
115:
116: if (portableflag) {
1.3 ! deraadt 117: fprintf(stderr, "real %9ld.%02ld\n",
1.1 deraadt 118: after.tv_sec, after.tv_usec/10000);
1.3 ! deraadt 119: fprintf(stderr, "user %9ld.%02ld\n",
1.1 deraadt 120: ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
1.3 ! deraadt 121: fprintf(stderr, "sys %9ld.%02ld\n",
1.1 deraadt 122: ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
123: } else {
124:
125: fprintf(stderr, "%9ld.%02ld real ",
126: after.tv_sec, after.tv_usec/10000);
127: fprintf(stderr, "%9ld.%02ld user ",
128: ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
129: fprintf(stderr, "%9ld.%02ld sys\n",
130: ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
131: }
132:
133: if (lflag) {
134: int hz = 100; /* XXX */
135: long ticks;
136:
137: ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
138: hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
139:
140: fprintf(stderr, "%10ld %s\n",
141: ru.ru_maxrss, "maximum resident set size");
142: fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0,
143: "average shared memory size");
144: fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0,
145: "average unshared data size");
146: fprintf(stderr, "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0,
147: "average unshared stack size");
148: fprintf(stderr, "%10ld %s\n",
149: ru.ru_minflt, "page reclaims");
150: fprintf(stderr, "%10ld %s\n",
151: ru.ru_majflt, "page faults");
152: fprintf(stderr, "%10ld %s\n",
153: ru.ru_nswap, "swaps");
154: fprintf(stderr, "%10ld %s\n",
155: ru.ru_inblock, "block input operations");
156: fprintf(stderr, "%10ld %s\n",
157: ru.ru_oublock, "block output operations");
158: fprintf(stderr, "%10ld %s\n",
159: ru.ru_msgsnd, "messages sent");
160: fprintf(stderr, "%10ld %s\n",
161: ru.ru_msgrcv, "messages received");
162: fprintf(stderr, "%10ld %s\n",
163: ru.ru_nsignals, "signals received");
164: fprintf(stderr, "%10ld %s\n",
165: ru.ru_nvcsw, "voluntary context switches");
166: fprintf(stderr, "%10ld %s\n",
167: ru.ru_nivcsw, "involuntary context switches");
168: }
169:
1.3 ! deraadt 170: exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
1.1 deraadt 171: }