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