]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/lastcomm/lastcomm.c
MFC r231054:
[FreeBSD/stable/8.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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)lastcomm.c  8.1 (Berkeley) 6/6/93";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/acct.h>
51
52 #include <ctype.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <utmp.h>
61 #include "pathnames.h"
62
63 /*XXX*/#include <inttypes.h>
64
65 time_t   expand(u_int);
66 char    *flagbits(int);
67 const    char *getdev(dev_t);
68 int      readrec_forward(FILE *f, struct acctv2 *av2);
69 int      readrec_backward(FILE *f, struct acctv2 *av2);
70 int      requested(char *[], struct acctv2 *);
71 static   void usage(void);
72
73 #define AC_UTIME 1 /* user */
74 #define AC_STIME 2 /* system */
75 #define AC_ETIME 4 /* elapsed */
76 #define AC_CTIME 8 /* user + system time, default */
77
78 #define AC_BTIME 16 /* starting time */
79 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/
80
81 int
82 main(int argc, char *argv[])
83 {
84         struct acctv2 ab;
85         char *p;
86         FILE *fp;
87         int (*readrec)(FILE *f, struct acctv2 *av2);
88         time_t t;
89         int ch, rv;
90         const char *acctfile;
91         int flags = 0;
92
93         acctfile = _PATH_ACCT;
94         while ((ch = getopt(argc, argv, "f:usecSE")) != -1)
95                 switch((char)ch) {
96                 case 'f':
97                         acctfile = optarg;
98                         break;
99
100                 case 'u': 
101                         flags |= AC_UTIME; /* user time */
102                         break;
103                 case 's':
104                         flags |= AC_STIME; /* system time */
105                         break;
106                 case 'e':
107                         flags |= AC_ETIME; /* elapsed time */
108                         break;
109                 case 'c':
110                         flags |= AC_CTIME; /* user + system time */
111                         break;
112
113                 case 'S':
114                         flags |= AC_BTIME; /* starting time */
115                         break;
116                 case 'E':
117                         /* exit time (starting time + elapsed time )*/
118                         flags |= AC_FTIME; 
119                         break;
120
121                 case '?':
122                 default:
123                         usage();
124                 }
125
126         /* default user + system time and starting time */
127         if (!flags) {
128             flags = AC_CTIME | AC_BTIME;
129         }
130
131         argc -= optind;
132         argv += optind;
133
134         if (strcmp(acctfile, "-") == 0) {
135                 fp = stdin;
136                 readrec = readrec_forward;
137         } else {
138                 /* Open the file. */
139                 if ((fp = fopen(acctfile, "r")) == NULL)
140                         err(1, "could not open %s", acctfile);
141                 if (fseek(fp, 0l, SEEK_END) == -1)
142                         err(1, "seek to end of %s failed", acctfile);
143                 readrec = readrec_backward;
144         }
145
146         while ((rv = readrec(fp, &ab)) == 1) {
147                 for (p = &ab.ac_comm[0];
148                     p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p)
149                         if (!isprint(*p))
150                                 *p = '?';
151
152                 if (*argv && !requested(argv, &ab))
153                         continue;
154
155                 (void)printf("%-*.*s %-7s %-*s %-*s",
156                              AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm,
157                              flagbits(ab.ac_flagx),
158                              UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
159                              UT_LINESIZE, getdev(ab.ac_tty));
160                 
161                 
162                 /* user + system time */
163                 if (flags & AC_CTIME) {
164                         (void)printf(" %6.3f secs", 
165                             (ab.ac_utime + ab.ac_stime) / 1000000);
166                 }
167                 
168                 /* usr time */
169                 if (flags & AC_UTIME) {
170                         (void)printf(" %6.3f us", ab.ac_utime / 1000000);
171                 }
172                 
173                 /* system time */
174                 if (flags & AC_STIME) {
175                         (void)printf(" %6.3f sy", ab.ac_stime / 1000000);
176                 }
177                 
178                 /* elapsed time */
179                 if (flags & AC_ETIME) {
180                         (void)printf(" %8.3f es", ab.ac_etime / 1000000);
181                 }
182                 
183                 /* starting time */
184                 if (flags & AC_BTIME) {
185                         (void)printf(" %.16s", ctime(&ab.ac_btime));
186                 }
187                 
188                 /* exit time (starting time + elapsed time )*/
189                 if (flags & AC_FTIME) {
190                         t = ab.ac_btime;
191                         t += (time_t)(ab.ac_etime / 1000000);
192                         (void)printf(" %.16s", ctime(&t));
193                 }
194                 printf("\n");
195         }
196         if (rv == EOF)
197                 err(1, "read record from %s failed", acctfile);
198
199         if (fflush(stdout))
200                 err(1, "stdout");
201         exit(0);
202 }
203
204 char *
205 flagbits(int f)
206 {
207         static char flags[20] = "-";
208         char *p;
209
210 #define BIT(flag, ch)   if (f & flag) *p++ = ch
211
212         p = flags + 1;
213         BIT(ASU, 'S');
214         BIT(AFORK, 'F');
215         BIT(ACOMPAT, 'C');
216         BIT(ACORE, 'D');
217         BIT(AXSIG, 'X');
218         *p = '\0';
219         return (flags);
220 }
221
222 int
223 requested(char *argv[], struct acctv2 *acp)
224 {
225         const char *p;
226
227         do {
228                 p = user_from_uid(acp->ac_uid, 0);
229                 if (!strcmp(p, *argv))
230                         return (1);
231                 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv))
232                         return (1);
233                 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN))
234                         return (1);
235         } while (*++argv);
236         return (0);
237 }
238
239 const char *
240 getdev(dev_t dev)
241 {
242         static dev_t lastdev = (dev_t)-1;
243         static const char *lastname;
244
245         if (dev == NODEV)                       /* Special case. */
246                 return ("__");
247         if (dev == lastdev)                     /* One-element cache. */
248                 return (lastname);
249         lastdev = dev;
250         lastname = devname(dev, S_IFCHR);
251         return (lastname);
252 }
253
254 static void
255 usage(void)
256 {
257         (void)fprintf(stderr,
258 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n");
259         exit(1);
260 }