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