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