lastcomm.c revision 1.1.1.2 1 1.1 cgd /*
2 1.1.1.1 jtc * Copyright (c) 1980, 1993
3 1.1.1.1 jtc * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1.1.1 jtc static char copyright[] =
36 1.1.1.1 jtc "@(#) Copyright (c) 1980, 1993\n\
37 1.1.1.1 jtc The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1.1.2 jtc static char sccsid[] = "@(#)lastcomm.c 8.2 (Berkeley) 4/29/95";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.1 cgd #include <sys/stat.h>
46 1.1.1.1 jtc #include <sys/acct.h>
47 1.1.1.1 jtc
48 1.1 cgd #include <ctype.h>
49 1.1.1.1 jtc #include <err.h>
50 1.1.1.1 jtc #include <fcntl.h>
51 1.1 cgd #include <stdio.h>
52 1.1.1.1 jtc #include <stdlib.h>
53 1.1.1.1 jtc #include <string.h>
54 1.1.1.1 jtc #include <struct.h>
55 1.1.1.1 jtc #include <unistd.h>
56 1.1.1.1 jtc #include <utmp.h>
57 1.1 cgd #include "pathnames.h"
58 1.1 cgd
59 1.1.1.1 jtc time_t expand __P((u_int));
60 1.1.1.1 jtc char *flagbits __P((int));
61 1.1.1.1 jtc char *getdev __P((dev_t));
62 1.1.1.1 jtc int requested __P((char *[], struct acct *));
63 1.1.1.1 jtc void usage __P((void));
64 1.1.1.1 jtc char *user_from_uid();
65 1.1 cgd
66 1.1.1.1 jtc int
67 1.1 cgd main(argc, argv)
68 1.1 cgd int argc;
69 1.1 cgd char *argv[];
70 1.1 cgd {
71 1.1.1.1 jtc register char *p;
72 1.1.1.1 jtc struct acct ab;
73 1.1 cgd struct stat sb;
74 1.1.1.1 jtc FILE *fp;
75 1.1.1.1 jtc off_t size;
76 1.1.1.1 jtc time_t t;
77 1.1.1.1 jtc int ch;
78 1.1.1.1 jtc char *acctfile;
79 1.1 cgd
80 1.1 cgd acctfile = _PATH_ACCT;
81 1.1 cgd while ((ch = getopt(argc, argv, "f:")) != EOF)
82 1.1 cgd switch((char)ch) {
83 1.1 cgd case 'f':
84 1.1 cgd acctfile = optarg;
85 1.1 cgd break;
86 1.1 cgd case '?':
87 1.1 cgd default:
88 1.1.1.1 jtc usage();
89 1.1 cgd }
90 1.1.1.1 jtc argc -= optind;
91 1.1 cgd argv += optind;
92 1.1 cgd
93 1.1.1.1 jtc /* Open the file. */
94 1.1.1.1 jtc if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
95 1.1.1.1 jtc err(1, "%s", acctfile);
96 1.1.1.1 jtc
97 1.1.1.1 jtc /*
98 1.1.1.1 jtc * Round off to integral number of accounting records, probably
99 1.1.1.1 jtc * not necessary, but it doesn't hurt.
100 1.1.1.1 jtc */
101 1.1.1.1 jtc size = sb.st_size - sb.st_size % sizeof(struct acct);
102 1.1.1.1 jtc
103 1.1.1.1 jtc /* Check if any records to display. */
104 1.1.1.1 jtc if (size < sizeof(struct acct))
105 1.1.1.1 jtc exit(0);
106 1.1.1.1 jtc
107 1.1.1.1 jtc /*
108 1.1.1.1 jtc * Seek to before the last entry in the file; use lseek(2) in case
109 1.1.1.1 jtc * the file is bigger than a "long".
110 1.1.1.1 jtc */
111 1.1.1.1 jtc size -= sizeof(struct acct);
112 1.1.1.1 jtc if (lseek(fileno(fp), size, SEEK_SET) == -1)
113 1.1.1.1 jtc err(1, "%s", acctfile);
114 1.1.1.1 jtc
115 1.1.1.1 jtc for (;;) {
116 1.1.1.1 jtc if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
117 1.1.1.1 jtc err(1, "%s", acctfile);
118 1.1.1.1 jtc
119 1.1.1.1 jtc if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
120 1.1.1.1 jtc err(1, "%s", acctfile);
121 1.1.1.1 jtc
122 1.1.1.1 jtc if (size == 0)
123 1.1 cgd break;
124 1.1.1.1 jtc size -= sizeof(struct acct);
125 1.1.1.1 jtc
126 1.1.1.1 jtc if (ab.ac_comm[0] == '\0') {
127 1.1.1.1 jtc ab.ac_comm[0] = '?';
128 1.1.1.1 jtc ab.ac_comm[1] = '\0';
129 1.1.1.1 jtc } else
130 1.1.1.1 jtc for (p = &ab.ac_comm[0];
131 1.1.1.1 jtc p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
132 1.1.1.1 jtc if (!isprint(*p))
133 1.1.1.1 jtc *p = '?';
134 1.1.1.1 jtc if (*argv && !requested(argv, &ab))
135 1.1.1.1 jtc continue;
136 1.1.1.1 jtc
137 1.1.1.1 jtc t = expand(ab.ac_utime) + expand(ab.ac_stime);
138 1.1.1.1 jtc (void)printf("%-*s %-7s %-*s %-*s %6.2f secs %.16s\n",
139 1.1.1.1 jtc fldsiz(acct, ac_comm), ab.ac_comm, flagbits(ab.ac_flag),
140 1.1.1.1 jtc UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
141 1.1.1.1 jtc UT_LINESIZE, getdev(ab.ac_tty),
142 1.1.1.1 jtc t / (double)AHZ, ctime(&ab.ac_btime));
143 1.1 cgd }
144 1.1.1.1 jtc exit(0);
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd time_t
148 1.1.1.1 jtc expand(t)
149 1.1.1.1 jtc u_int t;
150 1.1 cgd {
151 1.1 cgd register time_t nt;
152 1.1 cgd
153 1.1 cgd nt = t & 017777;
154 1.1 cgd t >>= 13;
155 1.1 cgd while (t) {
156 1.1 cgd t--;
157 1.1 cgd nt <<= 3;
158 1.1 cgd }
159 1.1 cgd return (nt);
160 1.1 cgd }
161 1.1 cgd
162 1.1 cgd char *
163 1.1 cgd flagbits(f)
164 1.1 cgd register int f;
165 1.1 cgd {
166 1.1.1.1 jtc static char flags[20] = "-";
167 1.1.1.1 jtc char *p;
168 1.1 cgd
169 1.1.1.1 jtc #define BIT(flag, ch) if (f & flag) *p++ = ch
170 1.1.1.1 jtc
171 1.1.1.1 jtc p = flags + 1;
172 1.1 cgd BIT(ASU, 'S');
173 1.1 cgd BIT(AFORK, 'F');
174 1.1 cgd BIT(ACOMPAT, 'C');
175 1.1 cgd BIT(ACORE, 'D');
176 1.1 cgd BIT(AXSIG, 'X');
177 1.1.1.1 jtc *p = '\0';
178 1.1 cgd return (flags);
179 1.1 cgd }
180 1.1 cgd
181 1.1.1.1 jtc int
182 1.1.1.1 jtc requested(argv, acp)
183 1.1 cgd register char *argv[];
184 1.1 cgd register struct acct *acp;
185 1.1 cgd {
186 1.1 cgd do {
187 1.1.1.2 jtc if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
188 1.1.1.1 jtc return (1);
189 1.1.1.2 jtc if (!strcmp(getdev(acp->ac_tty), *argv))
190 1.1.1.1 jtc return (1);
191 1.1 cgd if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
192 1.1.1.1 jtc return (1);
193 1.1 cgd } while (*++argv);
194 1.1.1.1 jtc return (0);
195 1.1 cgd }
196 1.1 cgd
197 1.1 cgd char *
198 1.1 cgd getdev(dev)
199 1.1 cgd dev_t dev;
200 1.1 cgd {
201 1.1.1.1 jtc static dev_t lastdev = (dev_t)-1;
202 1.1 cgd static char *lastname;
203 1.1 cgd
204 1.1.1.1 jtc if (dev == NODEV) /* Special case. */
205 1.1 cgd return ("__");
206 1.1.1.1 jtc if (dev == lastdev) /* One-element cache. */
207 1.1 cgd return (lastname);
208 1.1.1.1 jtc lastdev = dev;
209 1.1.1.2 jtc if ((lastname = devname(dev, S_IFCHR)) == NULL)
210 1.1.1.2 jtc lastname = "??";
211 1.1.1.1 jtc return (lastname);
212 1.1.1.1 jtc }
213 1.1.1.1 jtc
214 1.1.1.1 jtc void
215 1.1.1.1 jtc usage()
216 1.1.1.1 jtc {
217 1.1.1.1 jtc (void)fprintf(stderr,
218 1.1.1.1 jtc "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
219 1.1.1.1 jtc exit(1);
220 1.1 cgd }
221