GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/lastcomm/lastcomm.c Lines: 68 84 81.0 %
Date: 2017-11-07 Branches: 48 64 75.0 %

Line Branch Exec Source
1
/*	$OpenBSD: lastcomm.c,v 1.26 2017/06/08 17:14:02 bluhm 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 <unistd.h>
45
#include <utmp.h>
46
#include <pwd.h>
47
#include "pathnames.h"
48
49
time_t	 expand(u_int);
50
char	*flagbits(int);
51
char	*getdev(dev_t);
52
int	 requested(char *[], struct acct *);
53
void	 usage(void);
54
55
#define SECSPERMIN	(60)
56
#define SECSPERHOUR	(60 * 60)
57
58
int
59
main(int argc, char *argv[])
60
{
61
	char *p;
62
110
	struct acct ab;
63
55
	struct stat sb;
64
	FILE *fp;
65
	off_t size;
66
	time_t t;
67
	double delta;
68
	int ch;
69
	char *acctfile;
70
71
55
	if (pledge("stdio rpath getpw flock cpath wpath", NULL) == -1)
72
		err(1, "pledge");
73
74
	acctfile = _PATH_ACCT;
75
111
	while ((ch = getopt(argc, argv, "f:")) != -1)
76
1
		switch(ch) {
77
		case 'f':
78
1
			acctfile = optarg;
79
1
			break;
80
		case '?':
81
		default:
82
			usage();
83
		}
84
55
	argc -= optind;
85
55
	argv += optind;
86
87
	/* Open the file. */
88

220
	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
89
		err(1, "%s", acctfile);
90
91
	/*
92
	 * Round off to integral number of accounting records, probably
93
	 * not necessary, but it doesn't hurt.
94
	 */
95
55
	size = sb.st_size - sb.st_size % sizeof(struct acct);
96
97
	/* Check if any records to display. */
98
55
	if (size < sizeof(struct acct))
99
		exit(0);
100
101
	/*
102
	 * Seek to before the last entry in the file; use lseek(2) in case
103
	 * the file is bigger than a "long".
104
	 */
105
55
	size -= sizeof(struct acct);
106

165
	if (lseek(fileno(fp), size, SEEK_SET) == -1)
107
		err(1, "%s", acctfile);
108
109
35458
	for (;;) {
110
35513
		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
111
			err(1, "%s", acctfile);
112
113
35513
		if (ab.ac_comm[0] == '\0') {
114
			ab.ac_comm[0] = '?';
115
			ab.ac_comm[1] = '\0';
116
		} else
117
330927
			for (p = &ab.ac_comm[0];
118
461056
			    p < &ab.ac_comm[sizeof ab.ac_comm] && *p; ++p)
119
130129
				if (!isprint((unsigned char)*p))
120
					*p = '?';
121

37973
		if (!*argv || requested(argv, &ab))
122
		{
123
33134
			t = expand(ab.ac_utime) + expand(ab.ac_stime);
124
33134
			(void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
125
			    (int)sizeof ab.ac_comm,
126
			    (int)sizeof ab.ac_comm,
127
33134
			    ab.ac_comm, flagbits(ab.ac_flag), UT_NAMESIZE,
128
33134
			    UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
129
33134
			    UT_LINESIZE, UT_LINESIZE, getdev(ab.ac_tty),
130
33134
			    t / (double)AHZ, ctime(&ab.ac_btime));
131
33134
			delta = expand(ab.ac_etime) / (double)AHZ;
132
33134
			printf(" (%1.0f:%02.0f:%05.2f)\n",
133
33134
			    delta / SECSPERHOUR,
134
33134
			    fmod(delta, (double)SECSPERHOUR) / SECSPERMIN,
135
33134
			    fmod(delta, (double)SECSPERMIN));
136
33134
		}
137
138
35513
		if (size == 0)
139
			break;
140
		/* seek to previous entry */
141
35458
		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
142
			err(1, "%s", acctfile);
143
35458
		size -= sizeof(struct acct);
144
	}
145
	exit(0);
146
}
147
148
time_t
149
expand(u_int t)
150
{
151
	time_t nt;
152
153
198804
	nt = t & 017777;
154
99402
	t >>= 13;
155
198840
	while (t) {
156
18
		t--;
157
18
		nt <<= 3;
158
	}
159
99402
	return (nt);
160
}
161
162
char *
163
flagbits(int f)
164
{
165
	static char flags[20] = "-";
166
	char *p;
167
168
#define	BIT(flag, ch)	if (f & flag) *p++ = ch
169
170
	p = flags + 1;
171
99196
	BIT(ASU, 'S');
172
37260
	BIT(AFORK, 'F');
173
33134
	BIT(ACOMPAT, 'C');
174
33326
	BIT(ACORE, 'D');
175
33371
	BIT(AXSIG, 'X');
176
33326
	BIT(APLEDGE, 'P');
177
33144
	BIT(ATRAP, 'T');
178
33134
	*p = '\0';
179
33134
	return (flags);
180
}
181
182
int
183
requested(char *argv[], struct acct *acp)
184
{
185
4920
	do {
186
2460
		if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
187
			return (1);
188
2460
		if (!strcmp(getdev(acp->ac_tty), *argv))
189
			return (1);
190
2460
		if (!strncmp(acp->ac_comm, *argv, sizeof acp->ac_comm))
191
81
			return (1);
192
2379
	} while (*++argv);
193
2379
	return (0);
194
2460
}
195
196
char *
197
getdev(dev_t dev)
198
{
199
	static dev_t lastdev = (dev_t)-1;
200
	static char *lastname;
201
202
71188
	if (dev == NODEV)			/* Special case. */
203
998
		return ("__");
204
34596
	if (dev == lastdev)			/* One-element cache. */
205
34535
		return (lastname);
206
61
	lastdev = dev;
207
61
	if ((lastname = devname(dev, S_IFCHR)) == NULL)
208
61
		lastname = "??";
209
61
	return (lastname);
210
35594
}
211
212
void
213
usage(void)
214
{
215
	(void)fprintf(stderr,
216
	    "usage: lastcomm [-f file] [command ...] [user ...] [terminal ...]\n");
217
	exit(1);
218
}