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