1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 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. 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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 #endif /* not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/acct.h>
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include "pathnames.h"
60
61 /*XXX*/#include <inttypes.h>
62
63 time_t expand(u_int);
64 char *flagbits(int);
65 const char *getdev(dev_t);
66 int readrec_forward(FILE *f, struct acctv3 *av3);
67 int readrec_backward(FILE *f, struct acctv3 *av3);
68 int requested(char *[], struct acctv3 *);
69 static void usage(void);
70
71 #define AC_UTIME 1 /* user */
72 #define AC_STIME 2 /* system */
73 #define AC_ETIME 4 /* elapsed */
74 #define AC_CTIME 8 /* user + system time, default */
75
76 #define AC_BTIME 16 /* starting time */
77 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
78
79 int
main(int argc,char * argv[])80 main(int argc, char *argv[])
81 {
82 struct acctv3 ab;
83 char *p;
84 FILE *fp;
85 int (*readrec)(FILE *f, struct acctv3 *av3);
86 time_t t;
87 int ch, rv;
88 const char *acctfile, *format;
89 char buf[1024];
90 int flags = 0;
91
92 acctfile = _PATH_ACCT;
93 format = NULL;
94 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
95 switch((char)ch) {
96 case 'f':
97 acctfile = optarg;
98 break;
99
100 case 'u':
101 flags |= AC_UTIME; /* user time */
102 break;
103 case 's':
104 flags |= AC_STIME; /* system time */
105 break;
106 case 'e':
107 flags |= AC_ETIME; /* elapsed time */
108 break;
109 case 'c':
110 flags |= AC_CTIME; /* user + system time */
111 break;
112
113 case 'S':
114 flags |= AC_BTIME; /* starting time */
115 break;
116 case 'E':
117 /* exit time (starting time + elapsed time )*/
118 flags |= AC_FTIME;
119 break;
120
121 case '?':
122 default:
123 usage();
124 }
125
126 /* default user + system time and starting time */
127 if (!flags) {
128 flags = AC_CTIME | AC_BTIME;
129 }
130
131 argc -= optind;
132 argv += optind;
133
134 if (argc > 0 && **argv == '+') {
135 format = *argv + 1; /* skip + */
136 argc--;
137 argv++;
138 }
139
140 if (strcmp(acctfile, "-") == 0) {
141 fp = stdin;
142 readrec = readrec_forward;
143 } else {
144 /* Open the file. */
145 if ((fp = fopen(acctfile, "r")) == NULL)
146 err(1, "could not open %s", acctfile);
147 if (fseek(fp, 0l, SEEK_END) == -1)
148 err(1, "seek to end of %s failed", acctfile);
149 readrec = readrec_backward;
150 }
151
152 while ((rv = readrec(fp, &ab)) == 1) {
153 for (p = &ab.ac_comm[0];
154 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
155 if (!isprint(*p))
156 *p = '?';
157
158 if (*argv && !requested(argv, &ab))
159 continue;
160
161 (void)printf("%-*.*s %-7s %-*s %-8s",
162 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
163 flagbits(ab.ac_flagx),
164 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0),
165 getdev(ab.ac_tty));
166
167
168 /* user + system time */
169 if (flags & AC_CTIME) {
170 (void)printf(" %6.3f secs",
171 (ab.ac_utime + ab.ac_stime) / 1000000);
172 }
173
174 /* usr time */
175 if (flags & AC_UTIME) {
176 (void)printf(" %6.3f us", ab.ac_utime / 1000000);
177 }
178
179 /* system time */
180 if (flags & AC_STIME) {
181 (void)printf(" %6.3f sy", ab.ac_stime / 1000000);
182 }
183
184 /* elapsed time */
185 if (flags & AC_ETIME) {
186 (void)printf(" %8.3f es", ab.ac_etime / 1000000);
187 }
188
189 /* starting time */
190 if (flags & AC_BTIME) {
191 if (format != NULL) {
192 (void)strftime(buf, sizeof(buf), format,
193 localtime(&ab.ac_btime));
194 (void)printf(" %s", buf);
195 } else
196 (void)printf(" %.16s", ctime(&ab.ac_btime));
197 }
198
199 /* exit time (starting time + elapsed time )*/
200 if (flags & AC_FTIME) {
201 t = ab.ac_btime;
202 t += (time_t)(ab.ac_etime / 1000000);
203 if (format != NULL) {
204 (void)strftime(buf, sizeof(buf), format,
205 localtime(&t));
206 (void)printf(" %s", buf);
207 } else
208 (void)printf(" %.16s", ctime(&t));
209 }
210 printf("\n");
211 }
212 if (rv == EOF)
213 err(1, "read record from %s failed", acctfile);
214
215 if (fflush(stdout))
216 err(1, "stdout");
217 exit(0);
218 }
219
220 char *
flagbits(int f)221 flagbits(int f)
222 {
223 static char flags[20] = "-";
224 char *p;
225
226 #define BIT(flag, ch) if (f & flag) *p++ = ch
227
228 p = flags + 1;
229 BIT(ASU, 'S');
230 BIT(AFORK, 'F');
231 BIT(ACOMPAT, 'C');
232 BIT(ACORE, 'D');
233 BIT(AXSIG, 'X');
234 *p = '\0';
235 return (flags);
236 }
237
238 int
requested(char * argv[],struct acctv3 * acp)239 requested(char *argv[], struct acctv3 *acp)
240 {
241 const char *p;
242
243 do {
244 p = user_from_uid(acp->ac_uid, 0);
245 if (!strcmp(p, *argv))
246 return (1);
247 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
248 return (1);
249 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
250 return (1);
251 } while (*++argv);
252 return (0);
253 }
254
255 const char *
getdev(dev_t dev)256 getdev(dev_t dev)
257 {
258 static dev_t lastdev = (dev_t)-1;
259 static const char *lastname;
260
261 if (dev == NODEV) /* Special case. */
262 return ("__");
263 if (dev == lastdev) /* One-element cache. */
264 return (lastname);
265 lastdev = dev;
266 lastname = devname(dev, S_IFCHR);
267 return (lastname);
268 }
269
270 static void
usage(void)271 usage(void)
272 {
273 (void)fprintf(stderr,
274 "usage: lastcomm [-EScesu] [-f file] [+format] [command ...] "
275 "[user ...] [terminal ...]\n");
276 exit(1);
277 }
278