xref: /freebsd/usr.bin/lastcomm/lastcomm.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
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 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 static char sccsid[] = "@(#)lastcomm.c	8.1 (Berkeley) 6/6/93";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <sys/acct.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <struct.h>
55 #include <unistd.h>
56 #include <utmp.h>
57 #include "pathnames.h"
58 
59 time_t	 expand __P((u_int));
60 char	*flagbits __P((int));
61 char	*getdev __P((dev_t));
62 int	 requested __P((char *[], struct acct *));
63 void	 usage __P((void));
64 char	*user_from_uid();
65 
66 int
67 main(argc, argv)
68 	int argc;
69 	char *argv[];
70 {
71 	register char *p;
72 	struct acct ab;
73 	struct stat sb;
74 	FILE *fp;
75 	off_t size;
76 	time_t t;
77 	int ch;
78 	char *acctfile;
79 
80 	acctfile = _PATH_ACCT;
81 	while ((ch = getopt(argc, argv, "f:")) != EOF)
82 		switch((char)ch) {
83 		case 'f':
84 			acctfile = optarg;
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 	argc -= optind;
91 	argv += optind;
92 
93 	/* Open the file. */
94 	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
95 		err(1, "%s", acctfile);
96 
97 	/*
98 	 * Round off to integral number of accounting records, probably
99 	 * not necessary, but it doesn't hurt.
100 	 */
101 	size = sb.st_size - sb.st_size % sizeof(struct acct);
102 
103 	/* Check if any records to display. */
104 	if (size < sizeof(struct acct))
105 		exit(0);
106 
107 	/*
108 	 * Seek to before the last entry in the file; use lseek(2) in case
109 	 * the file is bigger than a "long".
110 	 */
111 	size -= sizeof(struct acct);
112 	if (lseek(fileno(fp), size, SEEK_SET) == -1)
113 		err(1, "%s", acctfile);
114 
115 	for (;;) {
116 		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
117 			err(1, "%s", acctfile);
118 
119 		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
120 			err(1, "%s", acctfile);
121 
122 		if (size == 0)
123 			break;
124 		size -= sizeof(struct acct);
125 
126 		if (ab.ac_comm[0] == '\0') {
127 			ab.ac_comm[0] = '?';
128 			ab.ac_comm[1] = '\0';
129 		} else
130 			for (p = &ab.ac_comm[0];
131 			    p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
132 				if (!isprint(*p))
133 					*p = '?';
134 		if (*argv && !requested(argv, &ab))
135 			continue;
136 
137 		t = expand(ab.ac_utime) + expand(ab.ac_stime);
138 		(void)printf("%-*.*s %-7s %-*s %-*s %6.2f secs %.16s\n",
139 			fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
140 			ab.ac_comm, flagbits(ab.ac_flag),
141 			UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
142 			UT_LINESIZE, getdev(ab.ac_tty),
143 			t / (double)AHZ, ctime(&ab.ac_btime));
144 	}
145 	exit(0);
146 }
147 
148 time_t
149 expand(t)
150 	u_int t;
151 {
152 	register time_t nt;
153 
154 	nt = t & 017777;
155 	t >>= 13;
156 	while (t) {
157 		t--;
158 		nt <<= 3;
159 	}
160 	return (nt);
161 }
162 
163 char *
164 flagbits(f)
165 	register int f;
166 {
167 	static char flags[20] = "-";
168 	char *p;
169 
170 #define	BIT(flag, ch)	if (f & flag) *p++ = ch
171 
172 	p = flags + 1;
173 	BIT(ASU, 'S');
174 	BIT(AFORK, 'F');
175 	BIT(ACOMPAT, 'C');
176 	BIT(ACORE, 'D');
177 	BIT(AXSIG, 'X');
178 	*p = '\0';
179 	return (flags);
180 }
181 
182 int
183 requested(argv, acp)
184 	register char *argv[];
185 	register struct acct *acp;
186 {
187 	register char *p;
188 
189 	do {
190 		p = user_from_uid(acp->ac_uid, 0);
191 		if (!strcmp(p, *argv))
192 			return (1);
193 		if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
194 			return (1);
195 		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
196 			return (1);
197 	} while (*++argv);
198 	return (0);
199 }
200 
201 char *
202 getdev(dev)
203 	dev_t dev;
204 {
205 	static dev_t lastdev = (dev_t)-1;
206 	static char *lastname;
207 
208 	if (dev == NODEV)			/* Special case. */
209 		return ("__");
210 	if (dev == lastdev)			/* One-element cache. */
211 		return (lastname);
212 	lastdev = dev;
213 	lastname = devname(dev, S_IFCHR);
214 	return (lastname);
215 }
216 
217 void
218 usage()
219 {
220 	(void)fprintf(stderr,
221 	    "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
222 	exit(1);
223 }
224