xref: /dragonfly/usr.bin/lastcomm/lastcomm.c (revision dca3c15d)
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  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)lastcomm.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/lastcomm/lastcomm.c,v 1.20.2.1 2007/04/18 05:53:50 dds Exp $
36  * $DragonFly: src/usr.bin/lastcomm/lastcomm.c,v 1.3 2003/10/04 20:36:47 hmp Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/acct.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <fcntl.h>
46 #include <pwd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <utmp.h>
52 #include "pathnames.h"
53 
54 time_t	 expand(u_int);
55 char	*flagbits(int);
56 const	 char *getdev(dev_t);
57 int	 requested(char *[], struct acct *);
58 static	 void usage(void);
59 
60 #define AC_UTIME 1 /* user */
61 #define AC_STIME 2 /* system */
62 #define AC_ETIME 4 /* elapsed */
63 #define AC_CTIME 8 /* user + system time, default */
64 
65 #define AC_BTIME 16 /* starting time */
66 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
67 
68 #define AC_HZ ((double)AHZ)
69 
70 int
71 main(int argc, char *argv[])
72 {
73 	char *p;
74 	struct acct ab;
75 	struct stat sb;
76 	FILE *fp;
77 	off_t size = 0;
78 	time_t t;
79 	int ch;
80 	const char *acctfile;
81 	int flags = 0;
82 
83 	acctfile = _PATH_ACCT;
84 	while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
85 		switch((char)ch) {
86 		case 'f':
87 			acctfile = optarg;
88 			break;
89 
90 		case 'u':
91 			flags |= AC_UTIME; /* user time */
92 			break;
93 		case 's':
94 			flags |= AC_STIME; /* system time */
95 			break;
96 		case 'e':
97 			flags |= AC_ETIME; /* elapsed time */
98 			break;
99 		case 'c':
100                         flags |= AC_CTIME; /* user + system time */
101 			break;
102 
103 		case 'S':
104                         flags |= AC_BTIME; /* starting time */
105 			break;
106 		case 'E':
107 			/* exit time (starting time + elapsed time )*/
108                         flags |= AC_FTIME;
109 			break;
110 
111 		case '?':
112 		default:
113 			usage();
114 		}
115 
116 	/* default user + system time and starting time */
117 	if (!flags) {
118 	    flags = AC_CTIME | AC_BTIME;
119 	}
120 
121 	argc -= optind;
122 	argv += optind;
123 
124 	if (strcmp(acctfile, "-") == 0)
125 		fp = stdin;
126 	else {
127 		/* Open the file. */
128 		if ((fp = fopen(acctfile, "r")) == NULL ||
129 		    fstat(fileno(fp), &sb))
130 			err(1, "could not open %s", acctfile);
131 
132 		/*
133 		 * Round off to integral number of accounting records,
134 		 * probably not necessary, but it doesn't hurt.
135 		 */
136 		size = sb.st_size - sb.st_size % sizeof(struct acct);
137 
138 		/* Check if any records to display. */
139 		if ((unsigned)size < sizeof(struct acct))
140 			exit(0);
141 	}
142 
143 	do {
144 		int rv;
145 
146 		if (fp != stdin) {
147 			size -= sizeof(struct acct);
148 			if (fseeko(fp, size, SEEK_SET) == -1)
149 				err(1, "seek %s failed", acctfile);
150 		}
151 
152 		if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) {
153 			if (feof(fp))
154 				break;
155 			else
156 				err(1, "read %s returned %d", acctfile, rv);
157 		}
158 
159 		if (ab.ac_comm[0] == '\0') {
160 			ab.ac_comm[0] = '?';
161 			ab.ac_comm[1] = '\0';
162 		} else
163 			for (p = &ab.ac_comm[0];
164 			    p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
165 				if (!isprint(*p))
166 					*p = '?';
167 		if (*argv && !requested(argv, &ab))
168 			continue;
169 
170 		(void)printf("%-*.*s %-7s %-*s %-*s",
171 			     AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
172 			     flagbits(ab.ac_flag),
173 			     UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
174 			     UT_LINESIZE, getdev(ab.ac_tty));
175 
176 
177 		/* user + system time */
178 		if (flags & AC_CTIME) {
179 			(void)printf(" %6.2f secs",
180 				     (expand(ab.ac_utime) +
181 				      expand(ab.ac_stime))/AC_HZ);
182 		}
183 
184 		/* usr time */
185 		if (flags & AC_UTIME) {
186 			(void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
187 		}
188 
189 		/* system time */
190 		if (flags & AC_STIME) {
191 			(void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
192 		}
193 
194 		/* elapsed time */
195 		if (flags & AC_ETIME) {
196 			(void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
197 		}
198 
199 		/* starting time */
200 		if (flags & AC_BTIME) {
201 			(void)printf(" %.16s", ctime(&ab.ac_btime));
202 		}
203 
204 		/* exit time (starting time + elapsed time )*/
205 		if (flags & AC_FTIME) {
206 			t = ab.ac_btime;
207 			t += (time_t)(expand(ab.ac_etime)/AC_HZ);
208 			(void)printf(" %.16s", ctime(&t));
209 		}
210 		printf("\n");
211 
212 	} while (size > 0);
213 	exit(0);
214 }
215 
216 time_t
217 expand(u_int t)
218 {
219 	time_t nt;
220 
221 	nt = t & 017777;
222 	t >>= 13;
223 	while (t) {
224 		t--;
225 		nt <<= 3;
226 	}
227 	return (nt);
228 }
229 
230 char *
231 flagbits(int f)
232 {
233 	static char flags[20] = "-";
234 	char *p;
235 
236 #define	BIT(flag, ch)	if (f & flag) *p++ = ch
237 
238 	p = flags + 1;
239 	BIT(ASU, 'S');
240 	BIT(AFORK, 'F');
241 	BIT(ACOMPAT, 'C');
242 	BIT(ACORE, 'D');
243 	BIT(AXSIG, 'X');
244 	*p = '\0';
245 	return (flags);
246 }
247 
248 int
249 requested(char *argv[], struct acct *acp)
250 {
251 	const char *p;
252 
253 	do {
254 		p = user_from_uid(acp->ac_uid, 0);
255 		if (!strcmp(p, *argv))
256 			return (1);
257 		if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
258 			return (1);
259 		if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
260 			return (1);
261 	} while (*++argv);
262 	return (0);
263 }
264 
265 const char *
266 getdev(dev_t dev)
267 {
268 	static dev_t lastdev = (dev_t)-1;
269 	static const char *lastname;
270 
271 	if (dev == NODEV)			/* Special case. */
272 		return ("__");
273 	if (dev == lastdev)			/* One-element cache. */
274 		return (lastname);
275 	lastdev = dev;
276 	lastname = devname(dev, S_IFCHR);
277 	return (lastname);
278 }
279 
280 static void
281 usage(void)
282 {
283 	(void)fprintf(stderr,
284 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");
285 	exit(1);
286 }
287