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