From dan@dan.emsphone.com  Mon May 17 10:25:45 2004
Return-Path: <dan@dan.emsphone.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 7628316A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 17 May 2004 10:25:45 -0700 (PDT)
Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 2EB3143D1D
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 17 May 2004 10:25:44 -0700 (PDT)
	(envelope-from dan@dan.emsphone.com)
Received: (from dan@localhost)
	by dan.emsphone.com (8.12.10/8.12.10) id i4HHPgJN002107;
	Mon, 17 May 2004 12:25:42 -0500 (CDT)
	(envelope-from dan)
Message-Id: <200405171725.i4HHPgJN002107@dan.emsphone.com>
Date: Mon, 17 May 2004 12:25:42 -0500 (CDT)
From: Dan Nelson <dnelson@allantgroup.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: "lastcomm string" is broken on -current
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         66765
>Category:       bin
>Synopsis:       "lastcomm string" is broken on -current
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 17 10:30:17 PDT 2004
>Closed-Date:    Thu May 20 12:47:27 PDT 2004
>Last-Modified:  Thu May 20 12:47:27 PDT 2004
>Originator:     Dan Nelson
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
System: FreeBSD dan.emsphone.com 5.2-CURRENT FreeBSD 5.2-CURRENT #329: Wed May 12 01:05:37 CDT 2004 dan@dan.emsphone.com:/usr/src/sys/i386/compile/DANSMP i386


	
>Description:
	

The lastcomm code has an ugly hack where lseek and fread are mixed in
an attempt to access file offsets past 4gb.  Unfortunately, the last
attempt to fix it ended up breaking the case where a match string is
passed on the argument.

>How-To-Repeat:
	

Run "lastcomm zzz" and notice you either get no results or an error.

>Fix:

Remove all the lseek code and use good old fseeko.  Simplifies the loop
a lot.

Index: lastcomm.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/lastcomm/lastcomm.c,v
retrieving revision 1.18
diff -u -p -r1.18 lastcomm.c
--- lastcomm.c	27 Jan 2003 18:16:32 -0000	1.18
+++ lastcomm.c	14 Apr 2004 03:18:39 -0000
@@ -134,7 +134,7 @@ main(int argc, char *argv[])
 
 	/* Open the file. */
 	if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
-		err(1, "%s", acctfile);
+		err(1, "could not open %s", acctfile);
 
 	/*
 	 * Round off to integral number of accounting records, probably
@@ -146,17 +146,13 @@ main(int argc, char *argv[])
 	if ((unsigned)size < sizeof(struct acct))
 		exit(0);
 
-	/*
-	 * Seek to before the last entry in the file; use lseek(2) in case
-	 * the file is bigger than a "long".
-	 */
-	size -= sizeof(struct acct);
-	if (lseek(fileno(fp), size, SEEK_SET) == -1)
-		err(1, "%s", acctfile);
-
-	for (;;) {
-		if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
-			err(1, "%s", acctfile);
+	do {
+		int rv;
+		size -= sizeof(struct acct);
+		if (fseeko(fp, size, SEEK_SET) == -1)
+			err(1, "seek %s failed", acctfile);
+		if ((rv = fread(&ab, sizeof(struct acct), 1, fp)) != 1)
+			err(1, "read %s returned %d", acctfile, rv);
 
 		if (ab.ac_comm[0] == '\0') {
 			ab.ac_comm[0] = '?';
@@ -211,12 +207,7 @@ main(int argc, char *argv[])
 		}
 		printf("\n");
 
-		if (size == 0)
-			break;
-		size -= sizeof(struct acct);
-		if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
-			err(1, "%s", acctfile);
- 	}
+ 	} while (size > 0);
  	exit(0);
 }
 


	


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: hmp 
State-Changed-When: Thu May 20 12:47:01 PDT 2004 
State-Changed-Why:  
Patch committed.  Thanks Dan! 


http://www.freebsd.org/cgi/query-pr.cgi?pr=66765 
>Unformatted:
