Annotation of src/usr.bin/lastcomm/lastcomm.c, Revision 1.2
1.2 ! deraadt 1: /* $NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $ */
1.1 deraadt 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: #ifndef lint
37: static char copyright[] =
38: "@(#) 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
1.2 ! deraadt 46: static char rcsid[] = "$NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $";
1.1 deraadt 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>
1.2 ! deraadt 56: #include <math.h>
1.1 deraadt 57: #include <stdio.h>
58: #include <stdlib.h>
59: #include <string.h>
60: #include <struct.h>
1.2 ! deraadt 61: #include <tzfile.h>
1.1 deraadt 62: #include <unistd.h>
63: #include <utmp.h>
64: #include "pathnames.h"
65:
66: time_t expand __P((u_int));
67: char *flagbits __P((int));
68: char *getdev __P((dev_t));
69: int requested __P((char *[], struct acct *));
70: void usage __P((void));
71: char *user_from_uid();
72:
73: int
74: main(argc, argv)
75: int argc;
76: char *argv[];
77: {
78: register char *p;
79: struct acct ab;
80: struct stat sb;
81: FILE *fp;
82: off_t size;
83: time_t t;
1.2 ! deraadt 84: double delta;
1.1 deraadt 85: int ch;
86: char *acctfile;
87:
88: acctfile = _PATH_ACCT;
89: while ((ch = getopt(argc, argv, "f:")) != EOF)
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. */
112: if (size < sizeof(struct acct))
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 (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
128: err(1, "%s", acctfile);
129:
130: if (size == 0)
131: break;
132: size -= sizeof(struct acct);
133:
134: if (ab.ac_comm[0] == '\0') {
135: ab.ac_comm[0] = '?';
136: ab.ac_comm[1] = '\0';
137: } else
138: for (p = &ab.ac_comm[0];
139: p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
140: if (!isprint(*p))
141: *p = '?';
142: if (*argv && !requested(argv, &ab))
143: continue;
144:
145: t = expand(ab.ac_utime) + expand(ab.ac_stime);
1.2 ! deraadt 146: (void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
1.1 deraadt 147: fldsiz(acct, ac_comm), fldsiz(acct, ac_comm), ab.ac_comm,
148: flagbits(ab.ac_flag), UT_NAMESIZE, UT_NAMESIZE,
149: user_from_uid(ab.ac_uid, 0), UT_LINESIZE, UT_LINESIZE,
150: getdev(ab.ac_tty), t / (double)AHZ, ctime(&ab.ac_btime));
1.2 ! deraadt 151: delta = expand(ab.ac_etime) / (double)AHZ;
! 152: printf(" (%1.0lf:%02.0lf:%05.2lf)\n",
! 153: delta / SECSPERHOUR,
! 154: fmod(delta, SECSPERHOUR) / SECSPERMIN,
! 155: fmod(delta, SECSPERMIN));
1.1 deraadt 156: }
157: exit(0);
158: }
159:
160: time_t
161: expand(t)
162: u_int t;
163: {
164: register time_t nt;
165:
166: nt = t & 017777;
167: t >>= 13;
168: while (t) {
169: t--;
170: nt <<= 3;
171: }
172: return (nt);
173: }
174:
175: char *
176: flagbits(f)
177: register int f;
178: {
179: static char flags[20] = "-";
180: char *p;
181:
182: #define BIT(flag, ch) if (f & flag) *p++ = ch
183:
184: p = flags + 1;
185: BIT(ASU, 'S');
186: BIT(AFORK, 'F');
187: BIT(ACOMPAT, 'C');
188: BIT(ACORE, 'D');
189: BIT(AXSIG, 'X');
190: *p = '\0';
191: return (flags);
192: }
193:
194: int
195: requested(argv, acp)
196: register char *argv[];
197: register struct acct *acp;
198: {
199: do {
200: if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
201: return (1);
202: if (!strcmp(getdev(acp->ac_tty), *argv))
203: return (1);
204: if (!strncmp(acp->ac_comm, *argv, fldsiz(acct, ac_comm)))
205: return (1);
206: } while (*++argv);
207: return (0);
208: }
209:
210: char *
211: getdev(dev)
212: dev_t dev;
213: {
214: static dev_t lastdev = (dev_t)-1;
215: static char *lastname;
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:
227: void
228: usage()
229: {
230: (void)fprintf(stderr,
231: "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");
232: exit(1);
233: }