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