xref: /freebsd/usr.bin/lastcomm/lastcomm.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
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 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
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 <fcntl.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <utmp.h>
60 #include "pathnames.h"
61 
62 time_t	 expand __P((u_int));
63 char	*flagbits __P((int));
64 const	 char *getdev __P((dev_t));
65 int	 requested __P((char *[], struct acct *));
66 static	 void usage __P((void));
67 
68 #define AC_UTIME 1 /* user */
69 #define AC_STIME 2 /* system */
70 #define AC_ETIME 4 /* elapsed */
71 #define AC_CTIME 8 /* user + system time, default */
72 
73 #define AC_BTIME 16 /* starting time */
74 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
75 
76 #define AC_HZ ((double)AHZ)
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char *argv[];
82 {
83 	char *p;
84 	struct acct ab;
85 	struct stat sb;
86 	FILE *fp;
87 	off_t size;
88 	time_t t;
89 	int ch;
90 	const char *acctfile;
91 	int flags = 0;
92 
93 	acctfile = _PATH_ACCT;
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 	/* Open the file. */
135 	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
136 		err(1, "%s", acctfile);
137 
138 	/*
139 	 * Round off to integral number of accounting records, probably
140 	 * not necessary, but it doesn't hurt.
141 	 */
142 	size = sb.st_size - sb.st_size % sizeof(struct acct);
143 
144 	/* Check if any records to display. */
145 	if ((unsigned)size < sizeof(struct acct))
146 		exit(0);
147 
148 	/*
149 	 * Seek to before the last entry in the file; use lseek(2) in case
150 	 * the file is bigger than a "long".
151 	 */
152 	size -= sizeof(struct acct);
153 	if (lseek(fileno(fp), size, SEEK_SET) == -1)
154 		err(1, "%s", acctfile);
155 
156 	for (;;) {
157 		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
158 			err(1, "%s", acctfile);
159 
160 		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
161 			err(1, "%s", acctfile);
162 
163 		if (size == 0)
164 			break;
165 		size -= sizeof(struct acct);
166 
167 		if (ab.ac_comm[0] == '\0') {
168 			ab.ac_comm[0] = '?';
169 			ab.ac_comm[1] = '\0';
170 		} else
171 			for (p = &ab.ac_comm[0];
172 			    p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
173 				if (!isprint(*p))
174 					*p = '?';
175 		if (*argv && !requested(argv, &ab))
176 			continue;
177 
178 		(void)printf("%-*.*s %-7s %-*s %-*s",
179 			     AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
180 			     flagbits(ab.ac_flag),
181 			     UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
182 			     UT_LINESIZE, getdev(ab.ac_tty));
183 
184 
185 		/* user + system time */
186 		if (flags & AC_CTIME) {
187 			(void)printf(" %6.2f secs",
188 				     (expand(ab.ac_utime) +
189 				      expand(ab.ac_stime))/AC_HZ);
190 		}
191 
192 		/* usr time */
193 		if (flags & AC_UTIME) {
194 			(void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
195 		}
196 
197 		/* system time */
198 		if (flags & AC_STIME) {
199 			(void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
200 		}
201 
202 		/* elapsed time */
203 		if (flags & AC_ETIME) {
204 			(void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
205 		}
206 
207 		/* starting time */
208 		if (flags & AC_BTIME) {
209 			(void)printf(" %.16s", ctime(&ab.ac_btime));
210 		}
211 
212 		/* exit time (starting time + elapsed time )*/
213 		if (flags & AC_FTIME) {
214 			t = ab.ac_btime;
215 			t += (time_t)(expand(ab.ac_etime)/AC_HZ);
216 			(void)printf(" %.16s", ctime(&t));
217 		}
218 		printf("\n");
219  	}
220  	exit(0);
221 }
222 
223 time_t
224 expand(t)
225 	u_int t;
226 {
227 	time_t nt;
228 
229 	nt = t & 017777;
230 	t >>= 13;
231 	while (t) {
232 		t--;
233 		nt <<= 3;
234 	}
235 	return (nt);
236 }
237 
238 char *
239 flagbits(f)
240 	int f;
241 {
242 	static char flags[20] = "-";
243 	char *p;
244 
245 #define	BIT(flag, ch)	if (f & flag) *p++ = ch
246 
247 	p = flags + 1;
248 	BIT(ASU, 'S');
249 	BIT(AFORK, 'F');
250 	BIT(ACOMPAT, 'C');
251 	BIT(ACORE, 'D');
252 	BIT(AXSIG, 'X');
253 	*p = '\0';
254 	return (flags);
255 }
256 
257 int
258 requested(argv, acp)
259 	char *argv[];
260 	struct acct *acp;
261 {
262 	const char *p;
263 
264 	do {
265 		p = user_from_uid(acp->ac_uid, 0);
266 		if (!strcmp(p, *argv))
267 			return (1);
268 		if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
269 			return (1);
270 		if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
271 			return (1);
272 	} while (*++argv);
273 	return (0);
274 }
275 
276 const char *
277 getdev(dev)
278 	dev_t dev;
279 {
280 	static dev_t lastdev = (dev_t)-1;
281 	static const char *lastname;
282 
283 	if (dev == NODEV)			/* Special case. */
284 		return ("__");
285 	if (dev == lastdev)			/* One-element cache. */
286 		return (lastname);
287 	lastdev = dev;
288 	lastname = devname(dev, S_IFCHR);
289 	return (lastname);
290 }
291 
292 static void
293 usage()
294 {
295 	(void)fprintf(stderr,
296 "usage: lastcomm [-EScesu] [ -f file ] [command ...] [user ...] [tty ...]\n");
297 	exit(1);
298 }
299