xref: /freebsd/usr.bin/lastcomm/lastcomm.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)lastcomm.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/acct.h>
51 
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <grp.h>
57 #include <pwd.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include "pathnames.h"
63 
64 /*XXX*/#include <inttypes.h>
65 
66 time_t	 expand(u_int);
67 char	*flagbits(int);
68 const	 char *getdev(dev_t);
69 int	 readrec_forward(FILE *f, struct acctv2 *av2);
70 int	 readrec_backward(FILE *f, struct acctv2 *av2);
71 int	 requested(char *[], struct acctv2 *);
72 static	 void usage(void);
73 
74 #define AC_UTIME 1 /* user */
75 #define AC_STIME 2 /* system */
76 #define AC_ETIME 4 /* elapsed */
77 #define AC_CTIME 8 /* user + system time, default */
78 
79 #define AC_BTIME 16 /* starting time */
80 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
81 
82 int
83 main(int argc, char *argv[])
84 {
85 	struct acctv2 ab;
86 	char *p;
87 	FILE *fp;
88 	int (*readrec)(FILE *f, struct acctv2 *av2);
89 	time_t t;
90 	int ch, rv;
91 	const char *acctfile;
92 	int flags = 0;
93 
94 	acctfile = _PATH_ACCT;
95 	while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
96 		switch((char)ch) {
97 		case 'f':
98 			acctfile = optarg;
99 			break;
100 
101 		case 'u':
102 			flags |= AC_UTIME; /* user time */
103 			break;
104 		case 's':
105 			flags |= AC_STIME; /* system time */
106 			break;
107 		case 'e':
108 			flags |= AC_ETIME; /* elapsed time */
109 			break;
110         	case 'c':
111                         flags |= AC_CTIME; /* user + system time */
112 			break;
113 
114         	case 'S':
115                         flags |= AC_BTIME; /* starting time */
116 			break;
117         	case 'E':
118 			/* exit time (starting time + elapsed time )*/
119                         flags |= AC_FTIME;
120 			break;
121 
122 		case '?':
123 		default:
124 			usage();
125 		}
126 
127 	/* default user + system time and starting time */
128 	if (!flags) {
129 	    flags = AC_CTIME | AC_BTIME;
130 	}
131 
132 	argc -= optind;
133 	argv += optind;
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 			(void)printf(" %.16s", ctime(&ab.ac_btime));
187 		}
188 
189 		/* exit time (starting time + elapsed time )*/
190 		if (flags & AC_FTIME) {
191 			t = ab.ac_btime;
192 			t += (time_t)(ab.ac_etime / 1000000);
193 			(void)printf(" %.16s", ctime(&t));
194 		}
195 		printf("\n");
196  	}
197 	if (rv == EOF)
198 		err(1, "read record from %s failed", acctfile);
199 
200 	if (fflush(stdout))
201 		err(1, "stdout");
202  	exit(0);
203 }
204 
205 char *
206 flagbits(int f)
207 {
208 	static char flags[20] = "-";
209 	char *p;
210 
211 #define	BIT(flag, ch)	if (f & flag) *p++ = ch
212 
213 	p = flags + 1;
214 	BIT(ASU, 'S');
215 	BIT(AFORK, 'F');
216 	BIT(ACOMPAT, 'C');
217 	BIT(ACORE, 'D');
218 	BIT(AXSIG, 'X');
219 	*p = '\0';
220 	return (flags);
221 }
222 
223 int
224 requested(char *argv[], struct acctv2 *acp)
225 {
226 	const char *p;
227 
228 	do {
229 		p = user_from_uid(acp->ac_uid, 0);
230 		if (!strcmp(p, *argv))
231 			return (1);
232 		if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
233 			return (1);
234 		if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
235 			return (1);
236 	} while (*++argv);
237 	return (0);
238 }
239 
240 const char *
241 getdev(dev_t dev)
242 {
243 	static dev_t lastdev = (dev_t)-1;
244 	static const char *lastname;
245 
246 	if (dev == NODEV)			/* Special case. */
247 		return ("__");
248 	if (dev == lastdev)			/* One-element cache. */
249 		return (lastname);
250 	lastdev = dev;
251 	lastname = devname(dev, S_IFCHR);
252 	return (lastname);
253 }
254 
255 static void
256 usage(void)
257 {
258 	(void)fprintf(stderr,
259 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");
260 	exit(1);
261 }
262