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