bin/66765: "lastcomm string" is broken on -current

Dan Nelson dnelson at allantgroup.com
Mon May 17 10:30:18 PDT 2004


>Number:         66765
>Category:       bin
>Synopsis:       "lastcomm string" is broken on -current
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 17 10:30:17 PDT 2004
>Closed-Date:
>Last-Modified:
>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 at 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:
>Unformatted:


More information about the freebsd-bugs mailing list