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