BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / lastcomm / lastcomm.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
a22c9f20
KB
3 * All rights reserved.
4 *
af359dea
C
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.
22e155fc
DF
32 */
33
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
a22c9f20 38#endif /* not lint */
22e155fc 39
7f055d32 40#ifndef lint
1c15e888 41static char sccsid[] = "@(#)lastcomm.c 5.11 (Berkeley) 6/1/90";
a22c9f20 42#endif /* not lint */
c7eeef87
BJ
43
44/*
45 * last command
46 */
7f055d32
SL
47#include <sys/param.h>
48#include <sys/acct.h>
cb88b4d5 49#include <sys/file.h>
40d69e19 50#include <sys/stat.h>
7f055d32
SL
51#include <utmp.h>
52#include <struct.h>
53#include <ctype.h>
a22c9f20 54#include <stdio.h>
0092fa3d 55#include "pathnames.h"
7f055d32 56
cb88b4d5 57struct acct buf[DEV_BSIZE / sizeof (struct acct)];
c7eeef87 58
cb88b4d5 59time_t expand();
7f055d32 60char *flagbits();
cb88b4d5 61char *getdev();
c7eeef87 62
7f055d32 63main(argc, argv)
956f1949 64 int argc;
7f055d32 65 char *argv[];
c7eeef87 66{
956f1949
KB
67 extern int optind;
68 extern char *optarg;
7f055d32 69 register struct acct *acp;
956f1949 70 register int bn, cc;
cb88b4d5 71 struct stat sb;
956f1949 72 int ch, fd;
ad8ec6f7 73 char *acctfile, *ctime(), *strcpy(), *user_from_uid();
a41e3d24 74 long lseek();
c7eeef87 75
0092fa3d 76 acctfile = _PATH_ACCT;
956f1949
KB
77 while ((ch = getopt(argc, argv, "f:")) != EOF)
78 switch((char)ch) {
79 case 'f':
80 acctfile = optarg;
81 break;
82 case '?':
83 default:
84 fputs("lastcomm [ -f file ]\n", stderr);
85 exit(1);
86 }
87 argv += optind;
0092fa3d 88
956f1949 89 fd = open(acctfile, O_RDONLY);
cb88b4d5 90 if (fd < 0) {
956f1949 91 perror(acctfile);
7f055d32 92 exit(1);
c7eeef87 93 }
956f1949 94 (void)fstat(fd, &sb);
9d7a479b 95 setpassent(1);
3b26b9c3 96 for (bn = btodb(sb.st_size); bn >= 0; bn--) {
a41e3d24 97 (void)lseek(fd, (off_t)dbtob(bn), L_SET);
cb88b4d5
SL
98 cc = read(fd, buf, DEV_BSIZE);
99 if (cc < 0) {
100 perror("read");
101 break;
102 }
103 acp = buf + (cc / sizeof (buf[0])) - 1;
104 for (; acp >= buf; acp--) {
105 register char *cp;
8dc8d395 106 time_t x;
cb88b4d5 107
a4e47cfd 108 if (acp->ac_comm[0] == '\0')
956f1949 109 (void)strcpy(acp->ac_comm, "?");
a4e47cfd
RC
110 for (cp = &acp->ac_comm[0];
111 cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
112 cp++)
113 if (!isascii(*cp) || iscntrl(*cp))
cb88b4d5 114 *cp = '?';
956f1949 115 if (*argv && !ok(argv, acp))
7f055d32 116 continue;
8dc8d395 117 x = expand(acp->ac_utime) + expand(acp->ac_stime);
4bf06df2 118 printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
ad8ec6f7 119 fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
a41e3d24 120 acp->ac_comm, flagbits(acp->ac_flag),
2f478083 121 UT_NAMESIZE, user_from_uid(acp->ac_uid, 0),
ad8ec6f7 122 UT_LINESIZE, getdev(acp->ac_tty),
891ad5df 123 x / (double)AHZ, ctime(&acp->ac_btime));
c7eeef87
BJ
124 }
125 }
126}
127
128time_t
129expand (t)
7f055d32 130 unsigned t;
c7eeef87
BJ
131{
132 register time_t nt;
133
134 nt = t & 017777;
135 t >>= 13;
7f055d32 136 while (t) {
c7eeef87
BJ
137 t--;
138 nt <<= 3;
139 }
140 return (nt);
141}
142
583da166
RE
143char *
144flagbits(f)
7f055d32 145 register int f;
c7eeef87 146{
583da166 147 static char flags[20];
a22c9f20 148 char *p, *strcpy();
583da166 149
a22c9f20
KB
150#define BIT(flag, ch) if (f & flag) *p++ = ch;
151 p = strcpy(flags, "- ");
7f055d32
SL
152 BIT(ASU, 'S');
153 BIT(AFORK, 'F');
154 BIT(ACOMPAT, 'C');
155 BIT(ACORE, 'D');
156 BIT(AXSIG, 'X');
7f055d32 157 return (flags);
583da166
RE
158}
159
956f1949 160ok(argv, acp)
cb88b4d5
SL
161 register char *argv[];
162 register struct acct *acp;
163{
a41e3d24 164 register char *cp;
ad8ec6f7 165 char *user_from_uid();
cb88b4d5 166
956f1949 167 do {
2f478083 168 cp = user_from_uid(acp->ac_uid, 0);
ad8ec6f7
KB
169 if (!strcmp(cp, *argv))
170 return(1);
171 if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
172 return(1);
173 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
956f1949
KB
174 return(1);
175 } while (*++argv);
176 return(0);
cb88b4d5
SL
177}
178
cb88b4d5
SL
179#include <sys/dir.h>
180
181#define N_DEVS 43 /* hash value for device names */
182#define NDEVS 500 /* max number of file names in /dev */
183
184struct devhash {
185 dev_t dev_dev;
ad8ec6f7 186 char dev_name [UT_LINESIZE + 1];
cb88b4d5
SL
187 struct devhash * dev_nxt;
188};
189struct devhash *dev_hash[N_DEVS];
190struct devhash *dev_chain;
191#define HASH(d) (((int) d) % N_DEVS)
192
583da166
RE
193setupdevs()
194{
195 register DIR * fd;
196 register struct devhash * hashtab;
197 register ndevs = NDEVS;
198 struct direct * dp;
a41e3d24 199 char *malloc();
583da166 200
a41e3d24 201 /*NOSTRICT*/
cb88b4d5
SL
202 hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
203 if (hashtab == (struct devhash *)0) {
a41e3d24
KB
204 fputs("No mem for dev table\n", stderr);
205 return;
206 }
0092fa3d
KB
207 if ((fd = opendir(_PATH_DEV)) == NULL) {
208 perror(_PATH_DEV);
583da166
RE
209 return;
210 }
583da166
RE
211 while (dp = readdir(fd)) {
212 if (dp->d_ino == 0)
213 continue;
583da166
RE
214 if (dp->d_name[0] != 't' && strcmp(dp->d_name, "console"))
215 continue;
ad8ec6f7
KB
216 (void)strncpy(hashtab->dev_name, dp->d_name, UT_LINESIZE);
217 hashtab->dev_name[UT_LINESIZE] = 0;
583da166
RE
218 hashtab->dev_nxt = dev_chain;
219 dev_chain = hashtab;
220 hashtab++;
221 if (--ndevs <= 0)
222 break;
223 }
224 closedir(fd);
225}
226
227char *
cb88b4d5 228getdev(dev)
7f055d32 229 dev_t dev;
583da166
RE
230{
231 register struct devhash *hp, *nhp;
232 struct stat statb;
7f055d32 233 char name[fldsiz(devhash, dev_name) + 6];
583da166
RE
234 static dev_t lastdev = (dev_t) -1;
235 static char *lastname;
f4e5f824 236 static int init = 0;
a41e3d24 237 char *strcpy(), *strcat();
583da166
RE
238
239 if (dev == NODEV)
7f055d32 240 return ("__");
583da166 241 if (dev == lastdev)
7f055d32 242 return (lastname);
cb88b4d5
SL
243 if (!init) {
244 setupdevs();
245 init++;
246 }
583da166
RE
247 for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
248 if (hp->dev_dev == dev) {
249 lastdev = dev;
7f055d32 250 return (lastname = hp->dev_name);
583da166 251 }
583da166
RE
252 for (hp = dev_chain; hp; hp = nhp) {
253 nhp = hp->dev_nxt;
ad8ec6f7 254 (void)strcpy(name, _PATH_DEV);
583da166
RE
255 strcat(name, hp->dev_name);
256 if (stat(name, &statb) < 0) /* name truncated usually */
257 continue;
258 if ((statb.st_mode & S_IFMT) != S_IFCHR)
259 continue;
260 hp->dev_dev = statb.st_rdev;
261 hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
262 dev_hash[HASH(hp->dev_dev)] = hp;
263 if (hp->dev_dev == dev) {
264 dev_chain = nhp;
265 lastdev = dev;
7f055d32 266 return (lastname = hp->dev_name);
583da166 267 }
c7eeef87 268 }
583da166 269 dev_chain = (struct devhash *) 0;
7f055d32
SL
270 return ("??");
271}