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