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