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 * 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.
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
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)lastcomm.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/lastcomm/lastcomm.c,v 1.20.2.1 2007/04/18 05:53:50 dds Exp $
34 #include <sys/param.h>
46 #include "pathnames.h"
50 const char *getdev(dev_t);
51 int requested(char *[], struct acct *);
52 static void usage(void);
54 #define AC_UTIME 1 /* user */
55 #define AC_STIME 2 /* system */
56 #define AC_ETIME 4 /* elapsed */
57 #define AC_CTIME 8 /* user + system time, default */
59 #define AC_BTIME 16 /* starting time */
60 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
62 #define AC_HZ ((double)AHZ)
65 main(int argc, char *argv[])
77 acctfile = _PATH_ACCT;
78 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
85 flags |= AC_UTIME; /* user time */
88 flags |= AC_STIME; /* system time */
91 flags |= AC_ETIME; /* elapsed time */
94 flags |= AC_CTIME; /* user + system time */
98 flags |= AC_BTIME; /* starting time */
101 /* exit time (starting time + elapsed time )*/
110 /* default user + system time and starting time */
112 flags = AC_CTIME | AC_BTIME;
118 if (strcmp(acctfile, "-") == 0)
122 if ((fp = fopen(acctfile, "r")) == NULL ||
123 fstat(fileno(fp), &sb))
124 err(1, "could not open %s", acctfile);
127 * Round off to integral number of accounting records,
128 * probably not necessary, but it doesn't hurt.
130 size = sb.st_size - sb.st_size % sizeof(struct acct);
132 /* Check if any records to display. */
133 if ((unsigned)size < sizeof(struct acct))
141 size -= sizeof(struct acct);
142 if (fseeko(fp, size, SEEK_SET) == -1)
143 err(1, "seek %s failed", acctfile);
146 if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) {
150 err(1, "read %s returned %d", acctfile, rv);
153 if (ab.ac_comm[0] == '\0') {
155 ab.ac_comm[1] = '\0';
157 for (p = &ab.ac_comm[0];
158 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
161 if (*argv && !requested(argv, &ab))
164 (void)printf("%-*.*s %-7s %-*s %-8s",
165 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
166 flagbits(ab.ac_flag),
167 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0),
171 /* user + system time */
172 if (flags & AC_CTIME) {
173 (void)printf(" %6.2f secs",
174 (expand(ab.ac_utime) +
175 expand(ab.ac_stime))/AC_HZ);
179 if (flags & AC_UTIME) {
180 (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
184 if (flags & AC_STIME) {
185 (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
189 if (flags & AC_ETIME) {
190 (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
194 if (flags & AC_BTIME) {
195 (void)printf(" %.16s", ctime(&ab.ac_btime));
198 /* exit time (starting time + elapsed time )*/
199 if (flags & AC_FTIME) {
201 t += (time_t)(expand(ab.ac_etime)/AC_HZ);
202 (void)printf(" %.16s", ctime(&t));
227 static char flags[20] = "-";
230 #define BIT(flag, ch) if (f & flag) *p++ = ch
243 requested(char *argv[], struct acct *acp)
248 p = user_from_uid(acp->ac_uid, 0);
249 if (!strcmp(p, *argv))
251 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
253 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
262 static dev_t lastdev = (dev_t)-1;
263 static const char *lastname;
265 if (dev == NODEV) /* Special case. */
267 if (dev == lastdev) /* One-element cache. */
270 lastname = devname(dev, S_IFCHR);
277 (void)fprintf(stderr,
278 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");