2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
45 #include "pathnames.h"
47 /*XXX*/#include <inttypes.h>
51 const char *getdev(dev_t
);
52 int readrec_forward(FILE *f
, struct acctv3
*av3
);
53 int readrec_backward(FILE *f
, struct acctv3
*av3
);
54 int requested(char *[], struct acctv3
*);
55 static void usage(void);
57 #define AC_UTIME 1 /* user */
58 #define AC_STIME 2 /* system */
59 #define AC_ETIME 4 /* elapsed */
60 #define AC_CTIME 8 /* user + system time, default */
62 #define AC_BTIME 16 /* starting time */
63 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
66 main(int argc
, char *argv
[])
71 int (*readrec
)(FILE *f
, struct acctv3
*av3
);
74 const char *acctfile
, *format
;
78 acctfile
= _PATH_ACCT
;
80 while ((ch
= getopt(argc
, argv
, "f:usecSE")) != -1)
87 flags
|= AC_UTIME
; /* user time */
90 flags
|= AC_STIME
; /* system time */
93 flags
|= AC_ETIME
; /* elapsed time */
96 flags
|= AC_CTIME
; /* user + system time */
100 flags
|= AC_BTIME
; /* starting time */
103 /* exit time (starting time + elapsed time )*/
112 /* default user + system time and starting time */
114 flags
= AC_CTIME
| AC_BTIME
;
120 if (argc
> 0 && **argv
== '+') {
121 format
= *argv
+ 1; /* skip + */
126 if (strcmp(acctfile
, "-") == 0) {
128 readrec
= readrec_forward
;
131 if ((fp
= fopen(acctfile
, "r")) == NULL
)
132 err(1, "could not open %s", acctfile
);
133 if (fseek(fp
, 0l, SEEK_END
) == -1)
134 err(1, "seek to end of %s failed", acctfile
);
135 readrec
= readrec_backward
;
138 while ((rv
= readrec(fp
, &ab
)) == 1) {
139 for (p
= &ab
.ac_comm
[0];
140 p
< &ab
.ac_comm
[AC_COMM_LEN
] && *p
; ++p
)
144 if (*argv
&& !requested(argv
, &ab
))
147 (void)printf("%-*.*s %-7s %-*s %-8s",
148 AC_COMM_LEN
, AC_COMM_LEN
, ab
.ac_comm
,
149 flagbits(ab
.ac_flagx
),
150 MAXLOGNAME
- 1, user_from_uid(ab
.ac_uid
, 0),
154 /* user + system time */
155 if (flags
& AC_CTIME
) {
156 (void)printf(" %6.3f secs",
157 (ab
.ac_utime
+ ab
.ac_stime
) / 1000000);
161 if (flags
& AC_UTIME
) {
162 (void)printf(" %6.3f us", ab
.ac_utime
/ 1000000);
166 if (flags
& AC_STIME
) {
167 (void)printf(" %6.3f sy", ab
.ac_stime
/ 1000000);
171 if (flags
& AC_ETIME
) {
172 (void)printf(" %8.3f es", ab
.ac_etime
/ 1000000);
176 if (flags
& AC_BTIME
) {
177 if (format
!= NULL
) {
178 (void)strftime(buf
, sizeof(buf
), format
,
179 localtime(&ab
.ac_btime
));
180 (void)printf(" %s", buf
);
182 (void)printf(" %.19s", ctime(&ab
.ac_btime
));
185 /* exit time (starting time + elapsed time )*/
186 if (flags
& AC_FTIME
) {
188 t
+= (time_t)(ab
.ac_etime
/ 1000000);
189 if (format
!= NULL
) {
190 (void)strftime(buf
, sizeof(buf
), format
,
192 (void)printf(" %s", buf
);
194 (void)printf(" %.19s", ctime(&t
));
199 err(1, "read record from %s failed", acctfile
);
209 static char flags
[20] = "-";
212 #define BIT(flag, ch) if (f & flag) *p++ = ch
225 requested(char *argv
[], struct acctv3
*acp
)
230 p
= user_from_uid(acp
->ac_uid
, 0);
231 if (!strcmp(p
, *argv
))
233 if ((p
= getdev(acp
->ac_tty
)) && !strcmp(p
, *argv
))
235 if (!strncmp(acp
->ac_comm
, *argv
, AC_COMM_LEN
))
244 static dev_t lastdev
= (dev_t
)-1;
245 static const char *lastname
;
247 if (dev
== NODEV
) /* Special case. */
249 if (dev
== lastdev
) /* One-element cache. */
252 lastname
= devname(dev
, S_IFCHR
);
259 (void)fprintf(stderr
,
260 "usage: lastcomm [-EScesu] [-f file] [+format] [command ...] "
261 "[user ...] [terminal ...]\n");