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