date and time created 87/02/05 15:19:02 by slatteng
[unix-history] / usr / src / usr.bin / lastcomm / lastcomm.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
7f055d32 13#ifndef lint
4bf06df2 14static char sccsid[] = "@(#)lastcomm.c 5.2 (Berkeley) %G%";
22e155fc 15#endif not lint
c7eeef87
BJ
16
17/*
18 * last command
19 */
7f055d32
SL
20#include <sys/param.h>
21#include <sys/acct.h>
cb88b4d5
SL
22#include <sys/file.h>
23
24#include <stdio.h>
7f055d32 25#include <pwd.h>
40d69e19 26#include <sys/stat.h>
7f055d32
SL
27#include <utmp.h>
28#include <struct.h>
29#include <ctype.h>
30
cb88b4d5 31struct acct buf[DEV_BSIZE / sizeof (struct acct)];
c7eeef87 32
cb88b4d5 33time_t expand();
7f055d32 34char *flagbits();
cb88b4d5
SL
35char *getname();
36char *getdev();
c7eeef87 37
7f055d32
SL
38main(argc, argv)
39 char *argv[];
c7eeef87 40{
cb88b4d5 41 register int bn, cc;
7f055d32 42 register struct acct *acp;
cb88b4d5
SL
43 int fd;
44 struct stat sb;
c7eeef87 45
cb88b4d5
SL
46 fd = open("/usr/adm/acct", O_RDONLY);
47 if (fd < 0) {
7f055d32
SL
48 perror("/usr/adm/acct");
49 exit(1);
c7eeef87 50 }
cb88b4d5 51 fstat(fd, &sb);
3b26b9c3
RC
52 for (bn = btodb(sb.st_size); bn >= 0; bn--) {
53 lseek(fd, dbtob(bn), L_SET);
cb88b4d5
SL
54 cc = read(fd, buf, DEV_BSIZE);
55 if (cc < 0) {
56 perror("read");
57 break;
58 }
59 acp = buf + (cc / sizeof (buf[0])) - 1;
60 for (; acp >= buf; acp--) {
61 register char *cp;
8dc8d395 62 time_t x;
cb88b4d5 63
a4e47cfd 64 if (acp->ac_comm[0] == '\0')
7f055d32 65 strcpy(acp->ac_comm, "?");
a4e47cfd
RC
66 for (cp = &acp->ac_comm[0];
67 cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
68 cp++)
69 if (!isascii(*cp) || iscntrl(*cp))
cb88b4d5 70 *cp = '?';
8dc8d395 71 if (argc > 1 && !ok(argc, argv, acp))
7f055d32 72 continue;
8dc8d395 73 x = expand(acp->ac_utime) + expand(acp->ac_stime);
4bf06df2
JL
74 printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
75 fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
76 acp->ac_comm,
7f055d32 77 flagbits(acp->ac_flag),
cb88b4d5
SL
78 fldsiz(utmp, ut_name), getname(acp->ac_uid),
79 fldsiz(utmp, ut_line), getdev(acp->ac_tty),
891ad5df 80 x / (double)AHZ, ctime(&acp->ac_btime));
c7eeef87
BJ
81 }
82 }
83}
84
85time_t
86expand (t)
7f055d32 87 unsigned t;
c7eeef87
BJ
88{
89 register time_t nt;
90
91 nt = t & 017777;
92 t >>= 13;
7f055d32 93 while (t) {
c7eeef87
BJ
94 t--;
95 nt <<= 3;
96 }
97 return (nt);
98}
99
583da166
RE
100char *
101flagbits(f)
7f055d32 102 register int f;
c7eeef87 103{
583da166
RE
104 register int i = 0;
105 static char flags[20];
106
7f055d32
SL
107#define BIT(flag, ch) flags[i++] = (f & flag) ? ch : ' '
108 BIT(ASU, 'S');
109 BIT(AFORK, 'F');
110 BIT(ACOMPAT, 'C');
111 BIT(ACORE, 'D');
112 BIT(AXSIG, 'X');
583da166 113 flags[i] = '\0';
7f055d32 114 return (flags);
583da166
RE
115}
116
cb88b4d5
SL
117ok(argc, argv, acp)
118 register int argc;
119 register char *argv[];
120 register struct acct *acp;
121{
122 register int j;
123
124 for (j = 1; j < argc; j++)
125 if (strcmp(getname(acp->ac_uid), argv[j]) &&
126 strcmp(getdev(acp->ac_tty), argv[j]) &&
a4e47cfd 127 strncmp(acp->ac_comm, argv[j], fldsiz(acct, ac_comm)))
cb88b4d5
SL
128 break;
129 return (j == argc);
130}
131
132/* should be done with nameserver or database */
133
134struct utmp utmp;
135
136#define NUID 2048
137#define NMAX (sizeof (utmp.ut_name))
138
139char names[NUID][NMAX+1];
140char outrangename[NMAX+1];
141int outrangeuid = -1;
142
143char *
144getname(uid)
145{
146 register struct passwd *pw;
147 static init;
148 struct passwd *getpwent();
149
150 if (uid >= 0 && uid < NUID && names[uid][0])
151 return (&names[uid][0]);
152 if (uid >= 0 && uid == outrangeuid)
153 return (outrangename);
154 if (init == 2) {
155 if (uid < NUID)
156 return (0);
157 setpwent();
158 while (pw = getpwent()) {
159 if (pw->pw_uid != uid)
160 continue;
161 outrangeuid = pw->pw_uid;
93b9cc72 162 strncpy(outrangename, pw->pw_name, NMAX);
cb88b4d5
SL
163 endpwent();
164 return (outrangename);
165 }
166 endpwent();
167 return (0);
168 }
169 if (init == 0)
170 setpwent(), init = 1;
171 while (pw = getpwent()) {
172 if (pw->pw_uid < 0 || pw->pw_uid >= NUID) {
173 if (pw->pw_uid == uid) {
174 outrangeuid = pw->pw_uid;
93b9cc72 175 strncpy(outrangename, pw->pw_name, NMAX);
cb88b4d5
SL
176 return (outrangename);
177 }
178 continue;
179 }
180 if (names[pw->pw_uid][0])
181 continue;
182 strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
183 if (pw->pw_uid == uid)
184 return (&names[uid][0]);
185 }
186 init = 2;
187 endpwent();
188 return (0);
189}
190
191#include <sys/dir.h>
192
193#define N_DEVS 43 /* hash value for device names */
194#define NDEVS 500 /* max number of file names in /dev */
195
196struct devhash {
197 dev_t dev_dev;
198 char dev_name [fldsiz(utmp, ut_line) + 1];
199 struct devhash * dev_nxt;
200};
201struct devhash *dev_hash[N_DEVS];
202struct devhash *dev_chain;
203#define HASH(d) (((int) d) % N_DEVS)
204
583da166
RE
205setupdevs()
206{
207 register DIR * fd;
208 register struct devhash * hashtab;
209 register ndevs = NDEVS;
210 struct direct * dp;
211
212 if ((fd = opendir("/dev")) == NULL) {
213 perror("/dev");
214 return;
215 }
cb88b4d5
SL
216 hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
217 if (hashtab == (struct devhash *)0) {
583da166 218 fprintf(stderr, "No mem for dev table\n");
f4e5f824 219 closedir(fd);
583da166
RE
220 return;
221 }
583da166
RE
222 while (dp = readdir(fd)) {
223 if (dp->d_ino == 0)
224 continue;
583da166
RE
225 if (dp->d_name[0] != 't' && strcmp(dp->d_name, "console"))
226 continue;
583da166
RE
227 strncpy(hashtab->dev_name, dp->d_name, fldsiz(utmp, ut_line));
228 hashtab->dev_name[fldsiz(utmp, ut_line)] = 0;
229 hashtab->dev_nxt = dev_chain;
230 dev_chain = hashtab;
231 hashtab++;
232 if (--ndevs <= 0)
233 break;
234 }
235 closedir(fd);
236}
237
238char *
cb88b4d5 239getdev(dev)
7f055d32 240 dev_t dev;
583da166
RE
241{
242 register struct devhash *hp, *nhp;
243 struct stat statb;
7f055d32 244 char name[fldsiz(devhash, dev_name) + 6];
583da166
RE
245 static dev_t lastdev = (dev_t) -1;
246 static char *lastname;
f4e5f824 247 static int init = 0;
583da166
RE
248
249 if (dev == NODEV)
7f055d32 250 return ("__");
583da166 251 if (dev == lastdev)
7f055d32 252 return (lastname);
cb88b4d5
SL
253 if (!init) {
254 setupdevs();
255 init++;
256 }
583da166
RE
257 for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
258 if (hp->dev_dev == dev) {
259 lastdev = dev;
7f055d32 260 return (lastname = hp->dev_name);
583da166 261 }
583da166
RE
262 for (hp = dev_chain; hp; hp = nhp) {
263 nhp = hp->dev_nxt;
264 strcpy(name, "/dev/");
265 strcat(name, hp->dev_name);
266 if (stat(name, &statb) < 0) /* name truncated usually */
267 continue;
268 if ((statb.st_mode & S_IFMT) != S_IFCHR)
269 continue;
270 hp->dev_dev = statb.st_rdev;
271 hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
272 dev_hash[HASH(hp->dev_dev)] = hp;
273 if (hp->dev_dev == dev) {
274 dev_chain = nhp;
275 lastdev = dev;
7f055d32 276 return (lastname = hp->dev_name);
583da166 277 }
c7eeef87 278 }
583da166 279 dev_chain = (struct devhash *) 0;
7f055d32
SL
280 return ("??");
281}