lastcomm.c revision 1.5 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)lastcomm.c 5.11 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: lastcomm.c,v 1.5 1994/03/23 04:37:40 cgd Exp $";
43 #endif /* not lint */
44
45 /*
46 * last command
47 */
48 #include <sys/param.h>
49 #include <sys/acct.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <utmp.h>
53 #include <struct.h>
54 #include <ctype.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include "pathnames.h"
58
59 time_t expand();
60 char *flagbits();
61 char *getdev();
62
63 extern char *user_from_uid __P((unsigned long, int));
64
65 main(argc, argv)
66 int argc;
67 char *argv[];
68 {
69 register char *p;
70 struct acct ab;
71 struct stat sb;
72 FILE *fp;
73 off_t size;
74 time_t x;
75 int ch;
76 char *acctfile;
77
78 acctfile = _PATH_ACCT;
79 while ((ch = getopt(argc, argv, "f:")) != EOF)
80 switch((char)ch) {
81 case 'f':
82 acctfile = optarg;
83 break;
84 case '?':
85 default:
86 fputs("lastcomm [ -f file ]\n", stderr);
87 exit(1);
88 }
89 argv += optind;
90
91 /* Open the file. */
92 if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
93 err(1, "%s", acctfile);
94
95 /*
96 * Round off to integral number of accounting records, probably
97 * not necessary, but it doesn't hurt.
98 */
99 size = sb.st_size - sb.st_size % sizeof(struct acct);
100
101 /* Check if any records to display. */
102 if (size < sizeof(struct acct))
103 exit(0);
104
105 /*
106 * Seek to before the last entry in the file; use lseek(2) in case
107 * the file is bigger than a "long".
108 */
109 size -= sizeof(struct acct);
110 if (lseek(fileno(fp), size, SEEK_SET) == -1)
111 err(1, "%s", acctfile);
112
113 for (;;) {
114 if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
115 err(1, "%s", acctfile);
116
117 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
118 err(1, "%s", acctfile);
119
120 if (size == 0)
121 break;
122 size -= sizeof(struct acct);
123
124 if (ab.ac_comm[0] == '\0') {
125 ab.ac_comm[0] = '?';
126 ab.ac_comm[1] = '\0';
127 } else
128 for (p = &ab.ac_comm[0];
129 p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
130 if (!isprint(*p))
131 *p = '?';
132 if (*argv && !ok(argv, &ab))
133 continue;
134
135 x = expand(ab.ac_utime) + expand(ab.ac_stime);
136 printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
137 fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
138 ab.ac_comm, flagbits(ab.ac_flag),
139 UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
140 UT_LINESIZE, getdev(ab.ac_tty),
141 x / (double)AHZ, ctime(&ab.ac_btime));
142 }
143 exit(0);
144 }
145
146 time_t
147 expand (t)
148 unsigned t;
149 {
150 register time_t nt;
151
152 nt = t & 017777;
153 t >>= 13;
154 while (t) {
155 t--;
156 nt <<= 3;
157 }
158 return (nt);
159 }
160
161 char *
162 flagbits(f)
163 register int f;
164 {
165 static char flags[20];
166 char *p, *strcpy();
167
168 #define BIT(flag, ch) if (f & flag) *p++ = ch;
169 p = strcpy(flags, "- ");
170 BIT(ASU, 'S');
171 BIT(AFORK, 'F');
172 BIT(ACOMPAT, 'C');
173 BIT(ACORE, 'D');
174 BIT(AXSIG, 'X');
175 return (flags);
176 }
177
178 ok(argv, acp)
179 register char *argv[];
180 register struct acct *acp;
181 {
182 register char *cp;
183
184 do {
185 cp = user_from_uid(acp->ac_uid, 0);
186 if (!strcmp(cp, *argv))
187 return(1);
188 if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
189 return(1);
190 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
191 return(1);
192 } while (*++argv);
193 return(0);
194 }
195
196 #include <dirent.h>
197
198 #define N_DEVS 43 /* hash value for device names */
199 #define NDEVS 500 /* max number of file names in /dev */
200
201 struct devhash {
202 dev_t dev_dev;
203 char dev_name [UT_LINESIZE + 1];
204 struct devhash * dev_nxt;
205 };
206 struct devhash *dev_hash[N_DEVS];
207 struct devhash *dev_chain;
208 #define HASH(d) (((int) d) % N_DEVS)
209
210 setupdevs()
211 {
212 register DIR * fd;
213 register struct devhash * hashtab;
214 register ndevs = NDEVS;
215 struct dirent * dp;
216
217 /*NOSTRICT*/
218 hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
219 if (hashtab == (struct devhash *)0) {
220 fputs("No mem for dev table\n", stderr);
221 return;
222 }
223 if ((fd = opendir(_PATH_DEV)) == NULL) {
224 perror(_PATH_DEV);
225 return;
226 }
227 while (dp = readdir(fd)) {
228 if (dp->d_ino == 0)
229 continue;
230 if (dp->d_name[0] != 't' &&
231 #warning WARNING! ugly pc 'vga' console hack!
232 (strcmp(dp->d_name, "console") && strcmp(dp->d_name, "vga")))
233 continue;
234 (void)strncpy(hashtab->dev_name, dp->d_name, UT_LINESIZE);
235 hashtab->dev_name[UT_LINESIZE] = 0;
236 hashtab->dev_nxt = dev_chain;
237 dev_chain = hashtab;
238 hashtab++;
239 if (--ndevs <= 0)
240 break;
241 }
242 closedir(fd);
243 }
244
245 char *
246 getdev(dev)
247 dev_t dev;
248 {
249 register struct devhash *hp, *nhp;
250 struct stat statb;
251 char name[fldsiz(devhash, dev_name) + 6];
252 static dev_t lastdev = (dev_t) -1;
253 static char *lastname;
254 static int init = 0;
255 char *strcpy(), *strcat();
256
257 if (dev == NODEV)
258 return ("__");
259 if (dev == lastdev)
260 return (lastname);
261 if (!init) {
262 setupdevs();
263 init++;
264 }
265 for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
266 if (hp->dev_dev == dev) {
267 lastdev = dev;
268 return (lastname = hp->dev_name);
269 }
270 for (hp = dev_chain; hp; hp = nhp) {
271 nhp = hp->dev_nxt;
272 (void)strcpy(name, _PATH_DEV);
273 strcat(name, hp->dev_name);
274 if (stat(name, &statb) < 0) /* name truncated usually */
275 continue;
276 if ((statb.st_mode & S_IFMT) != S_IFCHR)
277 continue;
278 hp->dev_dev = statb.st_rdev;
279 hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
280 dev_hash[HASH(hp->dev_dev)] = hp;
281 if (hp->dev_dev == dev) {
282 dev_chain = nhp;
283 lastdev = dev;
284 return (lastname = hp->dev_name);
285 }
286 }
287 dev_chain = (struct devhash *) 0;
288 return ("??");
289 }
290