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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.bin/lastcomm/lastcomm.c,v 1.10.2.3 2001/10/01 12:51:15 dd Exp $";
48 #include <sys/param.h>
60 #include "pathnames.h"
62 time_t expand __P((u_int));
63 char *flagbits __P((int));
64 const char *getdev __P((dev_t));
65 int requested __P((char *[], struct acct *));
66 static void usage __P((void));
68 #define AC_UTIME 1 /* user */
69 #define AC_STIME 2 /* system */
70 #define AC_ETIME 4 /* elapsed */
71 #define AC_CTIME 8 /* user + system time, default */
73 #define AC_BTIME 16 /* starting time */
74 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
76 #define AC_HZ ((double)AHZ)
93 acctfile = _PATH_ACCT;
94 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
101 flags |= AC_UTIME; /* user time */
104 flags |= AC_STIME; /* system time */
107 flags |= AC_ETIME; /* elapsed time */
110 flags |= AC_CTIME; /* user + system time */
114 flags |= AC_BTIME; /* starting time */
117 /* exit time (starting time + elapsed time )*/
126 /* default user + system time and starting time */
128 flags = AC_CTIME | AC_BTIME;
135 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
136 err(1, "%s", acctfile);
139 * Round off to integral number of accounting records, probably
140 * not necessary, but it doesn't hurt.
142 size = sb.st_size - sb.st_size % sizeof(struct acct);
144 /* Check if any records to display. */
145 if ((unsigned)size < sizeof(struct acct))
149 * Seek to before the last entry in the file; use lseek(2) in case
150 * the file is bigger than a "long".
152 size -= sizeof(struct acct);
153 if (lseek(fileno(fp), size, SEEK_SET) == -1)
154 err(1, "%s", acctfile);
157 if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
158 err(1, "%s", acctfile);
160 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
161 err(1, "%s", acctfile);
165 size -= sizeof(struct acct);
167 if (ab.ac_comm[0] == '\0') {
169 ab.ac_comm[1] = '\0';
171 for (p = &ab.ac_comm[0];
172 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
175 if (*argv && !requested(argv, &ab))
178 (void)printf("%-*.*s %-7s %-*s %-*s",
179 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
180 flagbits(ab.ac_flag),
181 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
182 UT_LINESIZE, getdev(ab.ac_tty));
185 /* user + system time */
186 if (flags & AC_CTIME) {
187 (void)printf(" %6.2f secs",
188 (expand(ab.ac_utime) +
189 expand(ab.ac_stime))/AC_HZ);
193 if (flags & AC_UTIME) {
194 (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
198 if (flags & AC_STIME) {
199 (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
203 if (flags & AC_ETIME) {
204 (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
208 if (flags & AC_BTIME) {
209 (void)printf(" %.16s", ctime(&ab.ac_btime));
212 /* exit time (starting time + elapsed time )*/
213 if (flags & AC_FTIME) {
215 t += (time_t)(expand(ab.ac_etime)/AC_HZ);
216 (void)printf(" %.16s", ctime(&t));
242 static char flags[20] = "-";
245 #define BIT(flag, ch) if (f & flag) *p++ = ch
265 p = user_from_uid(acp->ac_uid, 0);
266 if (!strcmp(p, *argv))
268 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
270 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
280 static dev_t lastdev = (dev_t)-1;
281 static const char *lastname;
283 if (dev == NODEV) /* Special case. */
285 if (dev == lastdev) /* One-element cache. */
288 lastname = devname(dev, S_IFCHR);
295 (void)fprintf(stderr,
296 "usage: lastcomm [-EScesu] [ -f file ] [command ...] [user ...] [tty ...]\n");