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