]>
git.saurik.com Git - apple/shell_cmds.git/blob - lastcomm/lastcomm.c
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>
66 #include "pathnames.h"
68 time_t expand
__P((u_int
));
69 char *flagbits
__P((int));
70 char *getdev
__P((dev_t
));
71 int main
__P((int, char **));
72 int requested
__P((char *[], struct acct
*));
73 void usage
__P((void));
90 acctfile
= _PATH_ACCT
;
91 while ((ch
= getopt(argc
, argv
, "f:")) != -1)
104 if ((fp
= fopen(acctfile
, "r")) == NULL
|| fstat(fileno(fp
), &sb
))
105 err(1, "%s", acctfile
);
108 * Round off to integral number of accounting records, probably
109 * not necessary, but it doesn't hurt.
111 size
= sb
.st_size
- sb
.st_size
% sizeof(struct acct
);
113 /* Check if any records to display. */
114 if (size
< sizeof(struct acct
))
118 * Seek to before the last entry in the file; use lseek(2) in case
119 * the file is bigger than a "long".
121 size
-= sizeof(struct acct
);
122 if (lseek(fileno(fp
), size
, SEEK_SET
) == -1)
123 err(1, "%s", acctfile
);
126 if (fread(&ab
, sizeof(struct acct
), 1, fp
) != 1)
127 err(1, "%s", acctfile
);
129 if (ab
.ac_comm
[0] == '\0') {
131 ab
.ac_comm
[1] = '\0';
133 for (p
= &ab
.ac_comm
[0];
134 p
< &ab
.ac_comm
[fldsiz(acct
, ac_comm
)] && *p
; ++p
)
137 if (!*argv
|| requested(argv
, &ab
)) {
139 t
= expand(ab
.ac_utime
) + expand(ab
.ac_stime
);
141 "%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
142 (int)fldsiz(acct
, ac_comm
),
143 (int)fldsiz(acct
, ac_comm
),
144 ab
.ac_comm
, flagbits(ab
.ac_flag
),
145 UT_NAMESIZE
, UT_NAMESIZE
,
146 user_from_uid(ab
.ac_uid
, 0), UT_LINESIZE
,
147 UT_LINESIZE
, getdev(ab
.ac_tty
),
148 t
/ (double)AHZ
, ctime(&ab
.ac_btime
));
149 delta
= expand(ab
.ac_etime
) / (double)AHZ
;
150 printf(" (%1.0f:%02.0f:%05.2f)\n",
152 fmod(delta
, SECSPERHOUR
) / SECSPERMIN
,
153 fmod(delta
, SECSPERMIN
));
155 /* are we at the beginning of the file yet? */
158 /* seek backward over the one we read and the next to read */
159 if (fseek(fp
, 2 * -(long)sizeof(struct acct
), SEEK_CUR
) == -1)
160 err(1, "%s", acctfile
);
161 /* and account for its size */
162 size
-= sizeof(struct acct
);
186 static char flags
[20] = "-";
189 #define BIT(flag, ch) if (f & flag) *p++ = ch
207 if (!strcmp(user_from_uid(acp
->ac_uid
, 0), *argv
))
209 if (!strcmp(getdev(acp
->ac_tty
), *argv
))
211 if (!strncmp(acp
->ac_comm
, *argv
, fldsiz(acct
, ac_comm
)))
221 static dev_t lastdev
= (dev_t
)-1;
222 static char *lastname
;
224 if (dev
== NODEV
) /* Special case. */
226 if (dev
== lastdev
) /* One-element cache. */
229 if ((lastname
= devname(dev
, S_IFCHR
)) == NULL
)
237 (void)fprintf(stderr
,
238 "lastcomm [ -f file ] [command ...] [user ...] [tty ...]\n");