2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
57 #include "pathnames.h"
59 /*XXX*/#include <inttypes.h>
63 const char *getdev(dev_t);
64 int readrec_forward(FILE *f, struct acctv2 *av2);
65 int readrec_backward(FILE *f, struct acctv2 *av2);
66 int requested(char *[], struct acctv2 *);
67 static void usage(void);
69 #define AC_UTIME 1 /* user */
70 #define AC_STIME 2 /* system */
71 #define AC_ETIME 4 /* elapsed */
72 #define AC_CTIME 8 /* user + system time, default */
74 #define AC_BTIME 16 /* starting time */
75 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
78 main(int argc, char *argv[])
83 int (*readrec)(FILE *f, struct acctv2 *av2);
86 const char *acctfile, *format;
90 acctfile = _PATH_ACCT;
92 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
99 flags |= AC_UTIME; /* user time */
102 flags |= AC_STIME; /* system time */
105 flags |= AC_ETIME; /* elapsed time */
108 flags |= AC_CTIME; /* user + system time */
112 flags |= AC_BTIME; /* starting time */
115 /* exit time (starting time + elapsed time )*/
124 /* default user + system time and starting time */
126 flags = AC_CTIME | AC_BTIME;
132 if (argc > 0 && **argv == '+') {
133 format = *argv + 1; /* skip + */
138 if (strcmp(acctfile, "-") == 0) {
140 readrec = readrec_forward;
143 if ((fp = fopen(acctfile, "r")) == NULL)
144 err(1, "could not open %s", acctfile);
145 if (fseek(fp, 0l, SEEK_END) == -1)
146 err(1, "seek to end of %s failed", acctfile);
147 readrec = readrec_backward;
150 while ((rv = readrec(fp, &ab)) == 1) {
151 for (p = &ab.ac_comm[0];
152 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
156 if (*argv && !requested(argv, &ab))
159 (void)printf("%-*.*s %-7s %-*s %-8s",
160 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
161 flagbits(ab.ac_flagx),
162 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0),
166 /* user + system time */
167 if (flags & AC_CTIME) {
168 (void)printf(" %6.3f secs",
169 (ab.ac_utime + ab.ac_stime) / 1000000);
173 if (flags & AC_UTIME) {
174 (void)printf(" %6.3f us", ab.ac_utime / 1000000);
178 if (flags & AC_STIME) {
179 (void)printf(" %6.3f sy", ab.ac_stime / 1000000);
183 if (flags & AC_ETIME) {
184 (void)printf(" %8.3f es", ab.ac_etime / 1000000);
188 if (flags & AC_BTIME) {
189 if (format != NULL) {
190 (void)strftime(buf, sizeof(buf), format,
191 localtime(&ab.ac_btime));
192 (void)printf(" %s", buf);
194 (void)printf(" %.16s", ctime(&ab.ac_btime));
197 /* exit time (starting time + elapsed time )*/
198 if (flags & AC_FTIME) {
200 t += (time_t)(ab.ac_etime / 1000000);
201 if (format != NULL) {
202 (void)strftime(buf, sizeof(buf), format,
204 (void)printf(" %s", buf);
206 (void)printf(" %.16s", ctime(&t));
211 err(1, "read record from %s failed", acctfile);
221 static char flags[20] = "-";
224 #define BIT(flag, ch) if (f & flag) *p++ = ch
237 requested(char *argv[], struct acctv2 *acp)
242 p = user_from_uid(acp->ac_uid, 0);
243 if (!strcmp(p, *argv))
245 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
247 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
256 static dev_t lastdev = (dev_t)-1;
257 static const char *lastname;
259 if (dev == NODEV) /* Special case. */
261 if (dev == lastdev) /* One-element cache. */
264 lastname = devname(dev, S_IFCHR);
271 (void)fprintf(stderr,
272 "usage: lastcomm [-EScesu] [-f file] [+format] [command ...] "
273 "[user ...] [terminal ...]\n");