new flag to user_from_uid and group_from_gid
[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 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22e155fc
DF
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1980 Regents of the University of California.\n\
21 All rights reserved.\n";
a22c9f20 22#endif /* not lint */
22e155fc 23
7f055d32 24#ifndef lint
ad8ec6f7 25static char sccsid[] = "@(#)lastcomm.c 5.9 (Berkeley) %G%";
a22c9f20 26#endif /* not lint */
c7eeef87
BJ
27
28/*
29 * last command
30 */
7f055d32
SL
31#include <sys/param.h>
32#include <sys/acct.h>
cb88b4d5 33#include <sys/file.h>
40d69e19 34#include <sys/stat.h>
7f055d32
SL
35#include <utmp.h>
36#include <struct.h>
37#include <ctype.h>
a22c9f20 38#include <stdio.h>
0092fa3d 39#include "pathnames.h"
7f055d32 40
cb88b4d5 41struct acct buf[DEV_BSIZE / sizeof (struct acct)];
c7eeef87 42
cb88b4d5 43time_t expand();
7f055d32 44char *flagbits();
cb88b4d5 45char *getdev();
c7eeef87 46
7f055d32 47main(argc, argv)
956f1949 48 int argc;
7f055d32 49 char *argv[];
c7eeef87 50{
956f1949
KB
51 extern int optind;
52 extern char *optarg;
7f055d32 53 register struct acct *acp;
956f1949 54 register int bn, cc;
cb88b4d5 55 struct stat sb;
956f1949 56 int ch, fd;
ad8ec6f7 57 char *acctfile, *ctime(), *strcpy(), *user_from_uid();
a41e3d24 58 long lseek();
c7eeef87 59
0092fa3d 60 acctfile = _PATH_ACCT;
956f1949
KB
61 while ((ch = getopt(argc, argv, "f:")) != EOF)
62 switch((char)ch) {
63 case 'f':
64 acctfile = optarg;
65 break;
66 case '?':
67 default:
68 fputs("lastcomm [ -f file ]\n", stderr);
69 exit(1);
70 }
71 argv += optind;
0092fa3d 72
956f1949 73 fd = open(acctfile, O_RDONLY);
cb88b4d5 74 if (fd < 0) {
956f1949 75 perror(acctfile);
7f055d32 76 exit(1);
c7eeef87 77 }
956f1949 78 (void)fstat(fd, &sb);
9d7a479b 79 setpassent(1);
3b26b9c3 80 for (bn = btodb(sb.st_size); bn >= 0; bn--) {
a41e3d24 81 (void)lseek(fd, (off_t)dbtob(bn), L_SET);
cb88b4d5
SL
82 cc = read(fd, buf, DEV_BSIZE);
83 if (cc < 0) {
84 perror("read");
85 break;
86 }
87 acp = buf + (cc / sizeof (buf[0])) - 1;
88 for (; acp >= buf; acp--) {
89 register char *cp;
8dc8d395 90 time_t x;
cb88b4d5 91
a4e47cfd 92 if (acp->ac_comm[0] == '\0')
956f1949 93 (void)strcpy(acp->ac_comm, "?");
a4e47cfd
RC
94 for (cp = &acp->ac_comm[0];
95 cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
96 cp++)
97 if (!isascii(*cp) || iscntrl(*cp))
cb88b4d5 98 *cp = '?';
956f1949 99 if (*argv && !ok(argv, acp))
7f055d32 100 continue;
8dc8d395 101 x = expand(acp->ac_utime) + expand(acp->ac_stime);
4bf06df2 102 printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
ad8ec6f7 103 fldsiz(acct, ac_comm), fldsiz(acct, ac_comm),
a41e3d24 104 acp->ac_comm, flagbits(acp->ac_flag),
ad8ec6f7
KB
105 UT_NAMESIZE, user_from_uid(acp->ac_uid),
106 UT_LINESIZE, getdev(acp->ac_tty),
891ad5df 107 x / (double)AHZ, ctime(&acp->ac_btime));
c7eeef87
BJ
108 }
109 }
110}
111
112time_t
113expand (t)
7f055d32 114 unsigned t;
c7eeef87
BJ
115{
116 register time_t nt;
117
118 nt = t & 017777;
119 t >>= 13;
7f055d32 120 while (t) {
c7eeef87
BJ
121 t--;
122 nt <<= 3;
123 }
124 return (nt);
125}
126
583da166
RE
127char *
128flagbits(f)
7f055d32 129 register int f;
c7eeef87 130{
583da166 131 static char flags[20];
a22c9f20 132 char *p, *strcpy();
583da166 133
a22c9f20
KB
134#define BIT(flag, ch) if (f & flag) *p++ = ch;
135 p = strcpy(flags, "- ");
7f055d32
SL
136 BIT(ASU, 'S');
137 BIT(AFORK, 'F');
138 BIT(ACOMPAT, 'C');
139 BIT(ACORE, 'D');
140 BIT(AXSIG, 'X');
7f055d32 141 return (flags);
583da166
RE
142}
143
956f1949 144ok(argv, acp)
cb88b4d5
SL
145 register char *argv[];
146 register struct acct *acp;
147{
a41e3d24 148 register char *cp;
ad8ec6f7 149 char *user_from_uid();
cb88b4d5 150
956f1949 151 do {
ad8ec6f7
KB
152 cp = user_from_uid(acp->ac_uid);
153 if (!strcmp(cp, *argv))
154 return(1);
155 if ((cp = getdev(acp->ac_tty)) && !strcmp(cp, *argv))
156 return(1);
157 if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
956f1949
KB
158 return(1);
159 } while (*++argv);
160 return(0);
cb88b4d5
SL
161}
162
cb88b4d5
SL
163#include <sys/dir.h>
164
165#define N_DEVS 43 /* hash value for device names */
166#define NDEVS 500 /* max number of file names in /dev */
167
168struct devhash {
169 dev_t dev_dev;
ad8ec6f7 170 char dev_name [UT_LINESIZE + 1];
cb88b4d5
SL
171 struct devhash * dev_nxt;
172};
173struct devhash *dev_hash[N_DEVS];
174struct devhash *dev_chain;
175#define HASH(d) (((int) d) % N_DEVS)
176
583da166
RE
177setupdevs()
178{
179 register DIR * fd;
180 register struct devhash * hashtab;
181 register ndevs = NDEVS;
182 struct direct * dp;
a41e3d24 183 char *malloc();
583da166 184
a41e3d24 185 /*NOSTRICT*/
cb88b4d5
SL
186 hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
187 if (hashtab == (struct devhash *)0) {
a41e3d24
KB
188 fputs("No mem for dev table\n", stderr);
189 return;
190 }
0092fa3d
KB
191 if ((fd = opendir(_PATH_DEV)) == NULL) {
192 perror(_PATH_DEV);
583da166
RE
193 return;
194 }
583da166
RE
195 while (dp = readdir(fd)) {
196 if (dp->d_ino == 0)
197 continue;
583da166
RE
198 if (dp->d_name[0] != 't' && strcmp(dp->d_name, "console"))
199 continue;
ad8ec6f7
KB
200 (void)strncpy(hashtab->dev_name, dp->d_name, UT_LINESIZE);
201 hashtab->dev_name[UT_LINESIZE] = 0;
583da166
RE
202 hashtab->dev_nxt = dev_chain;
203 dev_chain = hashtab;
204 hashtab++;
205 if (--ndevs <= 0)
206 break;
207 }
208 closedir(fd);
209}
210
211char *
cb88b4d5 212getdev(dev)
7f055d32 213 dev_t dev;
583da166
RE
214{
215 register struct devhash *hp, *nhp;
216 struct stat statb;
7f055d32 217 char name[fldsiz(devhash, dev_name) + 6];
583da166
RE
218 static dev_t lastdev = (dev_t) -1;
219 static char *lastname;
f4e5f824 220 static int init = 0;
a41e3d24 221 char *strcpy(), *strcat();
583da166
RE
222
223 if (dev == NODEV)
7f055d32 224 return ("__");
583da166 225 if (dev == lastdev)
7f055d32 226 return (lastname);
cb88b4d5
SL
227 if (!init) {
228 setupdevs();
229 init++;
230 }
583da166
RE
231 for (hp = dev_hash[HASH(dev)]; hp; hp = hp->dev_nxt)
232 if (hp->dev_dev == dev) {
233 lastdev = dev;
7f055d32 234 return (lastname = hp->dev_name);
583da166 235 }
583da166
RE
236 for (hp = dev_chain; hp; hp = nhp) {
237 nhp = hp->dev_nxt;
ad8ec6f7 238 (void)strcpy(name, _PATH_DEV);
583da166
RE
239 strcat(name, hp->dev_name);
240 if (stat(name, &statb) < 0) /* name truncated usually */
241 continue;
242 if ((statb.st_mode & S_IFMT) != S_IFCHR)
243 continue;
244 hp->dev_dev = statb.st_rdev;
245 hp->dev_nxt = dev_hash[HASH(hp->dev_dev)];
246 dev_hash[HASH(hp->dev_dev)] = hp;
247 if (hp->dev_dev == dev) {
248 dev_chain = nhp;
249 lastdev = dev;
7f055d32 250 return (lastname = hp->dev_name);
583da166 251 }
c7eeef87 252 }
583da166 253 dev_chain = (struct devhash *) 0;
7f055d32
SL
254 return ("??");
255}