]>
git.saurik.com Git - apple/shell_cmds.git/blob - lastcomm/lastcomm.c
589ce7a75e9f4f3488023bbf7cb3e0152c8996fd
1 /* $NetBSD: lastcomm.c,v 1.14 1998/04/02 10:22:03 kleink 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)lastcomm.c 8.2 (Berkeley) 4/29/95";
46 __RCSID("$NetBSD: lastcomm.c,v 1.14 1998/04/02 10:22:03 kleink Exp $");
49 #include <sys/param.h>
65 /* definitions from utmp.h */
68 #include "pathnames.h"
70 time_t expand
__P((u_int
));
71 char *flagbits
__P((int));
72 char *getdev
__P((dev_t
));
73 int main
__P((int, char **));
74 int requested
__P((char *[], struct acct
*));
75 void usage
__P((void));
92 acctfile
= _PATH_ACCT
;
93 while ((ch
= getopt(argc
, argv
, "f:")) != -1)
106 if ((fp
= fopen(acctfile
, "r")) == NULL
|| fstat(fileno(fp
), &sb
))
107 err(1, "%s", acctfile
);
110 * Round off to integral number of accounting records, probably
111 * not necessary, but it doesn't hurt.
113 size
= sb
.st_size
- sb
.st_size
% sizeof(struct acct
);
115 /* Check if any records to display. */
116 if (size
< sizeof(struct acct
))
120 * Seek to before the last entry in the file; use lseek(2) in case
121 * the file is bigger than a "long".
123 size
-= sizeof(struct acct
);
124 if (lseek(fileno(fp
), size
, SEEK_SET
) == -1)
125 err(1, "%s", acctfile
);
128 if (fread(&ab
, sizeof(struct acct
), 1, fp
) != 1)
129 err(1, "%s", acctfile
);
131 if (ab
.ac_comm
[0] == '\0') {
133 ab
.ac_comm
[1] = '\0';
135 for (p
= &ab
.ac_comm
[0];
136 p
< &ab
.ac_comm
[fldsiz(acct
, ac_comm
)] && *p
; ++p
)
139 if (!*argv
|| requested(argv
, &ab
)) {
141 t
= expand(ab
.ac_utime
) + expand(ab
.ac_stime
);
143 "%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
144 (int)fldsiz(acct
, ac_comm
),
145 (int)fldsiz(acct
, ac_comm
),
146 ab
.ac_comm
, flagbits(ab
.ac_flag
),
147 UT_NAMESIZE
, UT_NAMESIZE
,
148 user_from_uid(ab
.ac_uid
, 0), UT_LINESIZE
,
149 UT_LINESIZE
, getdev(ab
.ac_tty
),
150 t
/ (double)AHZ
, ctime(&ab
.ac_btime
));
151 delta
= expand(ab
.ac_etime
) / (double)AHZ
;
152 printf(" (%1.0f:%02.0f:%05.2f)\n",
154 fmod(delta
, SECSPERHOUR
) / SECSPERMIN
,
155 fmod(delta
, SECSPERMIN
));
157 /* are we at the beginning of the file yet? */
160 /* seek backward over the one we read and the next to read */
161 if (fseek(fp
, 2 * -(long)sizeof(struct acct
), SEEK_CUR
) == -1)
162 err(1, "%s", acctfile
);
163 /* and account for its size */
164 size
-= sizeof(struct acct
);
188 static char flags
[20] = "-";
191 #define BIT(flag, ch) if (f & flag) *p++ = ch
209 if (!strcmp(user_from_uid(acp
->ac_uid
, 0), *argv
))
211 if (!strcmp(getdev(acp
->ac_tty
), *argv
))
213 if (!strncmp(acp
->ac_comm
, *argv
, fldsiz(acct
, ac_comm
)))
223 static dev_t lastdev
= (dev_t
)-1;
224 static char *lastname
;
226 if (dev
== NODEV
) /* Special case. */
228 if (dev
== lastdev
) /* One-element cache. */
231 if ((lastname
= devname(dev
, S_IFCHR
)) == NULL
)
239 (void)fprintf(stderr
,
240 "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");