tmpfs - Fix mount_tmpfs -C
[dragonfly.git] / usr.bin / lastcomm / lastcomm.c
blob7f86688f129dd6ef68669cae4ebf02a61bd2a1b7
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)lastcomm.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/lastcomm/lastcomm.c,v 1.20.2.1 2007/04/18 05:53:50 dds Exp $
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include <sys/acct.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include "pathnames.h"
48 time_t expand(u_int);
49 char *flagbits(int);
50 const char *getdev(dev_t);
51 int requested(char *[], struct acct *);
52 static void usage(void);
54 #define AC_UTIME 1 /* user */
55 #define AC_STIME 2 /* system */
56 #define AC_ETIME 4 /* elapsed */
57 #define AC_CTIME 8 /* user + system time, default */
59 #define AC_BTIME 16 /* starting time */
60 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
62 #define AC_HZ ((double)AHZ)
64 int
65 main(int argc, char *argv[])
67 char *p;
68 struct acct ab;
69 struct stat sb;
70 FILE *fp;
71 off_t size = 0;
72 time_t t;
73 int ch;
74 const char *acctfile;
75 int flags = 0;
77 acctfile = _PATH_ACCT;
78 while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
79 switch((char)ch) {
80 case 'f':
81 acctfile = optarg;
82 break;
84 case 'u':
85 flags |= AC_UTIME; /* user time */
86 break;
87 case 's':
88 flags |= AC_STIME; /* system time */
89 break;
90 case 'e':
91 flags |= AC_ETIME; /* elapsed time */
92 break;
93 case 'c':
94 flags |= AC_CTIME; /* user + system time */
95 break;
97 case 'S':
98 flags |= AC_BTIME; /* starting time */
99 break;
100 case 'E':
101 /* exit time (starting time + elapsed time )*/
102 flags |= AC_FTIME;
103 break;
105 case '?':
106 default:
107 usage();
110 /* default user + system time and starting time */
111 if (!flags) {
112 flags = AC_CTIME | AC_BTIME;
115 argc -= optind;
116 argv += optind;
118 if (strcmp(acctfile, "-") == 0)
119 fp = stdin;
120 else {
121 /* Open the file. */
122 if ((fp = fopen(acctfile, "r")) == NULL ||
123 fstat(fileno(fp), &sb))
124 err(1, "could not open %s", acctfile);
127 * Round off to integral number of accounting records,
128 * probably not necessary, but it doesn't hurt.
130 size = sb.st_size - sb.st_size % sizeof(struct acct);
132 /* Check if any records to display. */
133 if ((unsigned)size < sizeof(struct acct))
134 exit(0);
137 do {
138 int rv;
140 if (fp != stdin) {
141 size -= sizeof(struct acct);
142 if (fseeko(fp, size, SEEK_SET) == -1)
143 err(1, "seek %s failed", acctfile);
146 if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1) {
147 if (feof(fp))
148 break;
149 else
150 err(1, "read %s returned %d", acctfile, rv);
153 if (ab.ac_comm[0] == '\0') {
154 ab.ac_comm[0] = '?';
155 ab.ac_comm[1] = '\0';
156 } else
157 for (p = &ab.ac_comm[0];
158 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
159 if (!isprint(*p))
160 *p = '?';
161 if (*argv && !requested(argv, &ab))
162 continue;
164 (void)printf("%-*.*s %-7s %-*s %-8s",
165 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
166 flagbits(ab.ac_flag),
167 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0),
168 getdev(ab.ac_tty));
171 /* user + system time */
172 if (flags & AC_CTIME) {
173 (void)printf(" %6.2f secs",
174 (expand(ab.ac_utime) +
175 expand(ab.ac_stime))/AC_HZ);
178 /* usr time */
179 if (flags & AC_UTIME) {
180 (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
183 /* system time */
184 if (flags & AC_STIME) {
185 (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
188 /* elapsed time */
189 if (flags & AC_ETIME) {
190 (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
193 /* starting time */
194 if (flags & AC_BTIME) {
195 (void)printf(" %.16s", ctime(&ab.ac_btime));
198 /* exit time (starting time + elapsed time )*/
199 if (flags & AC_FTIME) {
200 t = ab.ac_btime;
201 t += (time_t)(expand(ab.ac_etime)/AC_HZ);
202 (void)printf(" %.16s", ctime(&t));
204 printf("\n");
206 } while (size > 0);
207 exit(0);
210 time_t
211 expand(u_int t)
213 time_t nt;
215 nt = t & 017777;
216 t >>= 13;
217 while (t) {
218 t--;
219 nt <<= 3;
221 return (nt);
224 char *
225 flagbits(int f)
227 static char flags[20] = "-";
228 char *p;
230 #define BIT(flag, ch) if (f & flag) *p++ = ch
232 p = flags + 1;
233 BIT(ASU, 'S');
234 BIT(AFORK, 'F');
235 BIT(ACOMPAT, 'C');
236 BIT(ACORE, 'D');
237 BIT(AXSIG, 'X');
238 *p = '\0';
239 return (flags);
243 requested(char *argv[], struct acct *acp)
245 const char *p;
247 do {
248 p = user_from_uid(acp->ac_uid, 0);
249 if (!strcmp(p, *argv))
250 return (1);
251 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
252 return (1);
253 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
254 return (1);
255 } while (*++argv);
256 return (0);
259 const char *
260 getdev(dev_t dev)
262 static dev_t lastdev = (dev_t)-1;
263 static const char *lastname;
265 if (dev == NODEV) /* Special case. */
266 return ("__");
267 if (dev == lastdev) /* One-element cache. */
268 return (lastname);
269 lastdev = dev;
270 lastname = devname(dev, S_IFCHR);
271 return (lastname);
274 static void
275 usage(void)
277 (void)fprintf(stderr,
278 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");
279 exit(1);