lastcomm.c revision 1.1
1/*	$NetBSD: lastcomm.c,v 1.7.2.1 1995/10/12 06:55:13 jtc Exp $	*/
2
3/*
4 * Copyright (c) 1980, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37static char copyright[] =
38"@(#) Copyright (c) 1980, 1993\n\
39	The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43#if 0
44static char sccsid[] = "@(#)lastcomm.c	8.2 (Berkeley) 4/29/95";
45#endif
46static char rcsid[] = "$NetBSD: lastcomm.c,v 1.7.2.1 1995/10/12 06:55:13 jtc Exp $";
47#endif /* not lint */
48
49#include <sys/param.h>
50#include <sys/stat.h>
51#include <sys/acct.h>
52
53#include <ctype.h>
54#include <err.h>
55#include <fcntl.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <struct.h>
60#include <unistd.h>
61#include <utmp.h>
62#include "pathnames.h"
63
64time_t	 expand __P((u_int));
65char	*flagbits __P((int));
66char	*getdev __P((dev_t));
67int	 requested __P((char *[], struct acct *));
68void	 usage __P((void));
69char	*user_from_uid();
70
71int
72main(argc, argv)
73	int argc;
74	char *argv[];
75{
76	register char *p;
77	struct acct ab;
78	struct stat sb;
79	FILE *fp;
80	off_t size;
81	time_t t;
82	int ch;
83	char *acctfile;
84
85	acctfile = _PATH_ACCT;
86	while ((ch = getopt(argc, argv, "f:")) != EOF)
87		switch((char)ch) {
88		case 'f':
89			acctfile = optarg;
90			break;
91		case '?':
92		default:
93			usage();
94		}
95	argc -= optind;
96	argv += optind;
97
98	/* Open the file. */
99	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
100		err(1, "%s", acctfile);
101
102	/*
103	 * Round off to integral number of accounting records, probably
104	 * not necessary, but it doesn't hurt.
105	 */
106	size = sb.st_size - sb.st_size % sizeof(struct acct);
107
108	/* Check if any records to display. */
109	if (size < sizeof(struct acct))
110		exit(0);
111
112	/*
113	 * Seek to before the last entry in the file; use lseek(2) in case
114	 * the file is bigger than a "long".
115	 */
116	size -= sizeof(struct acct);
117	if (lseek(fileno(fp), size, SEEK_SET) == -1)
118		err(1, "%s", acctfile);
119
120	for (;;) {
121		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
122			err(1, "%s", acctfile);
123
124		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
125			err(1, "%s", acctfile);
126
127		if (size == 0)
128			break;
129		size -= sizeof(struct acct);
130
131		if (ab.ac_comm[0] == '\0') {
132			ab.ac_comm[0] = '?';
133			ab.ac_comm[1] = '\0';
134		} else
135			for (p = &ab.ac_comm[0];
136			    p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
137				if (!isprint(*p))
138					*p = '?';
139		if (*argv && !requested(argv, &ab))
140			continue;
141
142		t = expand(ab.ac_utime) + expand(ab.ac_stime);
143		(void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s\n",
144		    fldsiz(acct, ac_comm), fldsiz(acct, ac_comm), ab.ac_comm,
145		    flagbits(ab.ac_flag), UT_NAMESIZE, UT_NAMESIZE,
146		    user_from_uid(ab.ac_uid, 0), UT_LINESIZE, UT_LINESIZE,
147		    getdev(ab.ac_tty), t / (double)AHZ, ctime(&ab.ac_btime));
148	}
149	exit(0);
150}
151
152time_t
153expand(t)
154	u_int t;
155{
156	register time_t nt;
157
158	nt = t & 017777;
159	t >>= 13;
160	while (t) {
161		t--;
162		nt <<= 3;
163	}
164	return (nt);
165}
166
167char *
168flagbits(f)
169	register int f;
170{
171	static char flags[20] = "-";
172	char *p;
173
174#define	BIT(flag, ch)	if (f & flag) *p++ = ch
175
176	p = flags + 1;
177	BIT(ASU, 'S');
178	BIT(AFORK, 'F');
179	BIT(ACOMPAT, 'C');
180	BIT(ACORE, 'D');
181	BIT(AXSIG, 'X');
182	*p = '\0';
183	return (flags);
184}
185
186int
187requested(argv, acp)
188	register char *argv[];
189	register struct acct *acp;
190{
191	do {
192		if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
193			return (1);
194		if (!strcmp(getdev(acp->ac_tty), *argv))
195			return (1);
196		if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
197			return (1);
198	} while (*++argv);
199	return (0);
200}
201
202char *
203getdev(dev)
204	dev_t dev;
205{
206	static dev_t lastdev = (dev_t)-1;
207	static char *lastname;
208
209	if (dev == NODEV)			/* Special case. */
210		return ("__");
211	if (dev == lastdev)			/* One-element cache. */
212		return (lastname);
213	lastdev = dev;
214	if ((lastname = devname(dev, S_IFCHR)) == NULL)
215		lastname = "??";
216	return (lastname);
217}
218
219void
220usage()
221{
222	(void)fprintf(stderr,
223	    "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
224	exit(1);
225}
226