xref: /freebsd/usr.bin/lastcomm/lastcomm.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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 <utmp.h>
63 #include "pathnames.h"
64 
65 /*XXX*/#include <inttypes.h>
66 
67 time_t	 expand(u_int);
68 char	*flagbits(int);
69 const	 char *getdev(dev_t);
70 int	 readrec_forward(FILE *f, struct acctv2 *av2);
71 int	 readrec_backward(FILE *f, struct acctv2 *av2);
72 int	 requested(char *[], struct acctv2 *);
73 static	 void usage(void);
74 
75 #define AC_UTIME 1 /* user */
76 #define AC_STIME 2 /* system */
77 #define AC_ETIME 4 /* elapsed */
78 #define AC_CTIME 8 /* user + system time, default */
79 
80 #define AC_BTIME 16 /* starting time */
81 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
82 
83 int
84 main(int argc, char *argv[])
85 {
86 	struct acctv2 ab;
87 	char *p;
88 	FILE *fp;
89 	int (*readrec)(FILE *f, struct acctv2 *av2);
90 	time_t t;
91 	int ch, rv;
92 	const char *acctfile;
93 	int flags = 0;
94 
95 	acctfile = _PATH_ACCT;
96 	while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
97 		switch((char)ch) {
98 		case 'f':
99 			acctfile = optarg;
100 			break;
101 
102 		case 'u':
103 			flags |= AC_UTIME; /* user time */
104 			break;
105 		case 's':
106 			flags |= AC_STIME; /* system time */
107 			break;
108 		case 'e':
109 			flags |= AC_ETIME; /* elapsed time */
110 			break;
111         	case 'c':
112                         flags |= AC_CTIME; /* user + system time */
113 			break;
114 
115         	case 'S':
116                         flags |= AC_BTIME; /* starting time */
117 			break;
118         	case 'E':
119 			/* exit time (starting time + elapsed time )*/
120                         flags |= AC_FTIME;
121 			break;
122 
123 		case '?':
124 		default:
125 			usage();
126 		}
127 
128 	/* default user + system time and starting time */
129 	if (!flags) {
130 	    flags = AC_CTIME | AC_BTIME;
131 	}
132 
133 	argc -= optind;
134 	argv += optind;
135 
136 	if (strcmp(acctfile, "-") == 0) {
137 		fp = stdin;
138 		readrec = readrec_forward;
139 	} else {
140 		/* Open the file. */
141 		if ((fp = fopen(acctfile, "r")) == NULL)
142 			err(1, "could not open %s", acctfile);
143 		if (fseek(fp, 0l, SEEK_END) == -1)
144 			err(1, "seek to end of %s failed", acctfile);
145 		readrec = readrec_backward;
146 	}
147 
148 	while ((rv = readrec(fp, &ab)) == 1) {
149 		for (p = &ab.ac_comm[0];
150 		    p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
151 			if (!isprint(*p))
152 				*p = '?';
153 
154 		if (*argv && !requested(argv, &ab))
155 			continue;
156 
157 		(void)printf("%-*.*s %-7s %-*s %-*s",
158 			     AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
159 			     flagbits(ab.ac_flagx),
160 			     UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
161 			     UT_LINESIZE, getdev(ab.ac_tty));
162 
163 
164 		/* user + system time */
165 		if (flags & AC_CTIME) {
166 			(void)printf(" %6.3f secs",
167 			    (ab.ac_utime + ab.ac_stime) / 1000000);
168 		}
169 
170 		/* usr time */
171 		if (flags & AC_UTIME) {
172 			(void)printf(" %6.3f us", ab.ac_utime / 1000000);
173 		}
174 
175 		/* system time */
176 		if (flags & AC_STIME) {
177 			(void)printf(" %6.3f sy", ab.ac_stime / 1000000);
178 		}
179 
180 		/* elapsed time */
181 		if (flags & AC_ETIME) {
182 			(void)printf(" %8.3f es", ab.ac_etime / 1000000);
183 		}
184 
185 		/* starting time */
186 		if (flags & AC_BTIME) {
187 			(void)printf(" %.16s", ctime(&ab.ac_btime));
188 		}
189 
190 		/* exit time (starting time + elapsed time )*/
191 		if (flags & AC_FTIME) {
192 			t = ab.ac_btime;
193 			t += (time_t)(ab.ac_etime / 1000000);
194 			(void)printf(" %.16s", 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 
206 char *
207 flagbits(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 
224 int
225 requested(char *argv[], struct acctv2 *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 
241 const char *
242 getdev(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 
256 static void
257 usage(void)
258 {
259 	(void)fprintf(stderr,
260 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");
261 	exit(1);
262 }
263