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