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