Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / lastcomm / lastcomm.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1980, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)lastcomm.c       8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/lastcomm/lastcomm.c,v 1.10.2.3 2001/10/01 12:51:15 dd Exp $
36  * $DragonFly: src/usr.bin/lastcomm/lastcomm.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/stat.h>
41 #include <sys/acct.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <utmp.h>
51 #include "pathnames.h"
52
53 time_t   expand __P((u_int));
54 char    *flagbits __P((int));
55 const    char *getdev __P((dev_t));
56 int      requested __P((char *[], struct acct *));
57 static   void usage __P((void));
58
59 #define AC_UTIME 1 /* user */
60 #define AC_STIME 2 /* system */
61 #define AC_ETIME 4 /* elapsed */
62 #define AC_CTIME 8 /* user + system time, default */
63
64 #define AC_BTIME 16 /* starting time */
65 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
66
67 #define AC_HZ ((double)AHZ)
68
69 int
70 main(argc, argv)
71         int argc;
72         char *argv[];
73 {
74         char *p;
75         struct acct ab;
76         struct stat sb;
77         FILE *fp;
78         off_t size;
79         time_t t;
80         int ch;
81         const char *acctfile;
82         int flags = 0;
83
84         acctfile = _PATH_ACCT;
85         while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
86                 switch((char)ch) {
87                 case 'f':
88                         acctfile = optarg;
89                         break;
90
91                 case 'u': 
92                         flags |= AC_UTIME; /* user time */
93                         break;
94                 case 's':
95                         flags |= AC_STIME; /* system time */
96                         break;
97                 case 'e':
98                         flags |= AC_ETIME; /* elapsed time */
99                         break;
100                 case 'c':
101                         flags |= AC_CTIME; /* user + system time */
102                         break;
103
104                 case 'S':
105                         flags |= AC_BTIME; /* starting time */
106                         break;
107                 case 'E':
108                         /* exit time (starting time + elapsed time )*/
109                         flags |= AC_FTIME; 
110                         break;
111
112                 case '?':
113                 default:
114                         usage();
115                 }
116
117         /* default user + system time and starting time */
118         if (!flags) {
119             flags = AC_CTIME | AC_BTIME;
120         }
121
122         argc -= optind;
123         argv += optind;
124
125         /* Open the file. */
126         if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
127                 err(1, "%s", acctfile);
128
129         /*
130          * Round off to integral number of accounting records, probably
131          * not necessary, but it doesn't hurt.
132          */
133         size = sb.st_size - sb.st_size % sizeof(struct acct);
134
135         /* Check if any records to display. */
136         if ((unsigned)size < sizeof(struct acct))
137                 exit(0);
138
139         /*
140          * Seek to before the last entry in the file; use lseek(2) in case
141          * the file is bigger than a "long".
142          */
143         size -= sizeof(struct acct);
144         if (lseek(fileno(fp), size, SEEK_SET) == -1)
145                 err(1, "%s", acctfile);
146
147         for (;;) {
148                 if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
149                         err(1, "%s", acctfile);
150
151                 if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
152                         err(1, "%s", acctfile);
153
154                 if (size == 0)
155                         break;
156                 size -= sizeof(struct acct);
157
158                 if (ab.ac_comm[0] == '\0') {
159                         ab.ac_comm[0] = '?';
160                         ab.ac_comm[1] = '\0';
161                 } else
162                         for (p = &ab.ac_comm[0];
163                             p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
164                                 if (!isprint(*p))
165                                         *p = '?';
166                 if (*argv && !requested(argv, &ab))
167                         continue;
168
169                 (void)printf("%-*.*s %-7s %-*s %-*s",
170                              AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
171                              flagbits(ab.ac_flag),
172                              UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
173                              UT_LINESIZE, getdev(ab.ac_tty));
174                 
175                 
176                 /* user + system time */
177                 if (flags & AC_CTIME) {
178                         (void)printf(" %6.2f secs", 
179                                      (expand(ab.ac_utime) + 
180                                       expand(ab.ac_stime))/AC_HZ);
181                 }
182                 
183                 /* usr time */
184                 if (flags & AC_UTIME) {
185                         (void)printf(" %6.2f us", expand(ab.ac_utime)/AC_HZ);
186                 }
187                 
188                 /* system time */
189                 if (flags & AC_STIME) {
190                         (void)printf(" %6.2f sy", expand(ab.ac_stime)/AC_HZ);
191                 }
192                 
193                 /* elapsed time */
194                 if (flags & AC_ETIME) {
195                         (void)printf(" %8.2f es", expand(ab.ac_etime)/AC_HZ);
196                 }
197                 
198                 /* starting time */
199                 if (flags & AC_BTIME) {
200                         (void)printf(" %.16s", ctime(&ab.ac_btime));
201                 }
202                 
203                 /* exit time (starting time + elapsed time )*/
204                 if (flags & AC_FTIME) {
205                         t = ab.ac_btime;
206                         t += (time_t)(expand(ab.ac_etime)/AC_HZ);
207                         (void)printf(" %.16s", ctime(&t));
208                 }
209                 printf("\n");
210         }
211         exit(0);
212 }
213
214 time_t
215 expand(t)
216         u_int t;
217 {
218         time_t nt;
219
220         nt = t & 017777;
221         t >>= 13;
222         while (t) {
223                 t--;
224                 nt <<= 3;
225         }
226         return (nt);
227 }
228
229 char *
230 flagbits(f)
231         int f;
232 {
233         static char flags[20] = "-";
234         char *p;
235
236 #define BIT(flag, ch)   if (f & flag) *p++ = ch
237
238         p = flags + 1;
239         BIT(ASU, 'S');
240         BIT(AFORK, 'F');
241         BIT(ACOMPAT, 'C');
242         BIT(ACORE, 'D');
243         BIT(AXSIG, 'X');
244         *p = '\0';
245         return (flags);
246 }
247
248 int
249 requested(argv, acp)
250         char *argv[];
251         struct acct *acp;
252 {
253         const char *p;
254
255         do {
256                 p = user_from_uid(acp->ac_uid, 0);
257                 if (!strcmp(p, *argv))
258                         return (1);
259                 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
260                         return (1);
261                 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
262                         return (1);
263         } while (*++argv);
264         return (0);
265 }
266
267 const char *
268 getdev(dev)
269         dev_t dev;
270 {
271         static dev_t lastdev = (dev_t)-1;
272         static const char *lastname;
273
274         if (dev == NODEV)                       /* Special case. */
275                 return ("__");
276         if (dev == lastdev)                     /* One-element cache. */
277                 return (lastname);
278         lastdev = dev;
279         lastname = devname(dev, S_IFCHR);
280         return (lastname);
281 }
282
283 static void
284 usage()
285 {
286         (void)fprintf(stderr,
287 "usage: lastcomm [-EScesu] [ -f file ] [command ...] [user ...] [tty ...]\n");
288         exit(1);
289 }