1 /* $NetBSD: lastcomm.c,v 1.20 2008/07/21 14:19:23 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)lastcomm.c 8.2 (Berkeley) 4/29/95";
42 __RCSID("$NetBSD: lastcomm.c,v 1.20 2008/07/21 14:19:23 lukem Exp $");
45 #include <sys/param.h>
62 #include "pathnames.h"
64 static time_t expand(u_int
);
65 static char *flagbits(int);
66 static const char *getdev(dev_t
);
67 static int requested(char *[], struct acct
*);
68 static void usage(void) __dead
;
70 int main(int, char **);
73 main(int argc
, char *argv
[])
83 const char *acctfile
= _PATH_ACCT
;
87 while ((ch
= getopt(argc
, argv
, "f:")) != -1)
100 if ((fp
= fopen(acctfile
, "r")) == NULL
|| fstat(fileno(fp
), &sb
))
101 err(1, "%s", acctfile
);
104 * Round off to integral number of accounting records, probably
105 * not necessary, but it doesn't hurt.
107 size
= sb
.st_size
- sb
.st_size
% sizeof(struct acct
);
109 /* Check if any records to display. */
110 if (size
< (off_t
)sizeof(struct acct
))
114 * Seek to before the last entry in the file; use lseek(2) in case
115 * the file is bigger than a "long".
117 size
-= sizeof(struct acct
);
118 if (lseek(fileno(fp
), size
, SEEK_SET
) == -1)
119 err(1, "%s", acctfile
);
122 if (fread(&ab
, sizeof(struct acct
), 1, fp
) != 1)
123 err(1, "%s", acctfile
);
125 if (ab
.ac_comm
[0] == '\0') {
127 ab
.ac_comm
[1] = '\0';
129 for (p
= &ab
.ac_comm
[0];
130 p
< &ab
.ac_comm
[fldsiz(acct
, ac_comm
)] && *p
; ++p
)
131 if (!isprint((unsigned char)*p
))
133 if (!*argv
|| requested(argv
, &ab
)) {
135 t
= expand(ab
.ac_utime
) + expand(ab
.ac_stime
);
137 "%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
138 (int)fldsiz(acct
, ac_comm
),
139 (int)fldsiz(acct
, ac_comm
),
140 ab
.ac_comm
, flagbits(ab
.ac_flag
),
141 UT_NAMESIZE
, UT_NAMESIZE
,
142 user_from_uid(ab
.ac_uid
, 0), UT_LINESIZE
,
143 UT_LINESIZE
, getdev(ab
.ac_tty
),
144 t
/ (double)AHZ
, ctime(&ab
.ac_btime
));
145 delta
= expand(ab
.ac_etime
) / (double)AHZ
;
146 printf(" (%1.0f:%02.0f:%05.2f)\n",
147 floor(delta
/ SECSPERHOUR
),
148 floor(fmod(delta
, SECSPERHOUR
) / SECSPERMIN
),
149 fmod(delta
, SECSPERMIN
));
151 /* are we at the beginning of the file yet? */
154 /* seek backward over the one we read and the next to read */
155 if (fseek(fp
, 2 * -(long)sizeof(struct acct
), SEEK_CUR
) == -1)
156 err(1, "%s", acctfile
);
157 /* and account for its size */
158 size
-= sizeof(struct acct
);
180 static char flags
[20] = "-";
183 #define BIT(flag, ch) if (f & flag) *p++ = ch
196 requested(char *argv
[], struct acct
*acp
)
199 if (!strcmp(user_from_uid(acp
->ac_uid
, 0), *argv
))
201 if (!strcmp(getdev(acp
->ac_tty
), *argv
))
203 if (!strncmp(acp
->ac_comm
, *argv
, fldsiz(acct
, ac_comm
)))
212 static dev_t lastdev
= (dev_t
)-1;
213 static const char *lastname
;
215 if (dev
== NODEV
) /* Special case. */
217 if (dev
== lastdev
) /* One-element cache. */
220 if ((lastname
= devname(dev
, S_IFCHR
)) == NULL
)
228 (void)fprintf(stderr
,
229 "Usage: %s [ -f file ] [command ...] [user ...] [tty ...]\n",