Fix lossage revealed by the recent `lseek() vs. negative offsets' changes;

from John F. Woods <jfw@jfwhome.funhouse.com> in PR/3634.
This commit is contained in:
kleink 1997-05-19 10:01:52 +00:00
parent ac74fdbb9c
commit 85793595bf

View File

@ -1,4 +1,4 @@
/* $NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $ */
/* $NetBSD: lastcomm.c,v 1.10 1997/05/19 10:01:52 kleink Exp $ */
/*
* Copyright (c) 1980, 1993
@ -43,7 +43,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)lastcomm.c 8.2 (Berkeley) 4/29/95";
#endif
static char rcsid[] = "$NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $";
static char rcsid[] = "$NetBSD: lastcomm.c,v 1.10 1997/05/19 10:01:52 kleink Exp $";
#endif /* not lint */
#include <sys/param.h>
@ -124,13 +124,6 @@ main(argc, argv)
if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
err(1, "%s", acctfile);
if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
err(1, "%s", acctfile);
if (size == 0)
break;
size -= sizeof(struct acct);
if (ab.ac_comm[0] == '\0') {
ab.ac_comm[0] = '?';
ab.ac_comm[1] = '\0';
@ -139,21 +132,29 @@ main(argc, argv)
p < &ab.ac_comm[fldsiz(acct, ac_comm)] && *p; ++p)
if (!isprint(*p))
*p = '?';
if (*argv && !requested(argv, &ab))
continue;
if (!*argv || requested(argv, &ab)) {
t = expand(ab.ac_utime) + expand(ab.ac_stime);
(void)printf(
"%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
fldsiz(acct, ac_comm), fldsiz(acct, ac_comm), ab.ac_comm,
flagbits(ab.ac_flag), UT_NAMESIZE, UT_NAMESIZE,
user_from_uid(ab.ac_uid, 0), UT_LINESIZE, UT_LINESIZE,
getdev(ab.ac_tty), t / (double)AHZ, ctime(&ab.ac_btime));
delta = expand(ab.ac_etime) / (double)AHZ;
printf(" (%1.0lf:%02.0lf:%05.2lf)\n",
delta / SECSPERHOUR,
fmod(delta, SECSPERHOUR) / SECSPERMIN,
fmod(delta, SECSPERMIN));
t = expand(ab.ac_utime) + expand(ab.ac_stime);
(void)printf(
"%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
fldsiz(acct, ac_comm), fldsiz(acct, ac_comm), ab.ac_comm,
flagbits(ab.ac_flag), UT_NAMESIZE, UT_NAMESIZE,
user_from_uid(ab.ac_uid, 0), UT_LINESIZE, UT_LINESIZE,
getdev(ab.ac_tty), t / (double)AHZ, ctime(&ab.ac_btime));
delta = expand(ab.ac_etime) / (double)AHZ;
printf(" (%1.0lf:%02.0lf:%05.2lf)\n",
delta / SECSPERHOUR,
fmod(delta, SECSPERHOUR) / SECSPERMIN,
fmod(delta, SECSPERMIN));
}
/* are we at the beginning of the file yet? */
if (size == 0)
break;
/* seek backward over the one we read and the next to read */
if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
err(1, "%s", acctfile);
/* and account for its size */
size -= sizeof(struct acct);
}
exit(0);
}