From erik@phoenix.smluc.org  Sun Dec 22 15:30:25 2002
Return-Path: <erik@phoenix.smluc.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id AE95537B401
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 22 Dec 2002 15:30:25 -0800 (PST)
Received: from mailout3-eri1.midsouth.rr.com (mailout3-eri1.midsouth.rr.com [24.165.200.8])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 17B9C43EE6
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 22 Dec 2002 15:30:20 -0800 (PST)
	(envelope-from erik@phoenix.smluc.org)
Received: from xarx.localdomain (root@cpe-066-061-038-083.midsouth.rr.com [66.61.38.83])
	by mailout3-eri1.midsouth.rr.com (8.11.4/8.11.4) with ESMTP id gBMNUIa04234
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 22 Dec 2002 17:30:19 -0600 (CST)
Received: (from erik@localhost)
	by xarx.localdomain (8.11.6/8.11.6) id gBMNUcR20194
	for FreeBSD-gnats-submit@freebsd.org; Sun, 22 Dec 2002 17:30:38 -0600
Message-Id: <200212222330.gBMNUcR20194@xarx.localdomain>
Date: Sun, 22 Dec 2002 17:30:38 -0600
From: erik@phoenix.smluc.org
To: FreeBSD-gnats-submit@freebsd.org
Subject: patch to allow negative numbers in head

>Number:         46480
>Category:       bin
>Synopsis:       patch to allow negative numbers in head
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sun Dec 22 15:40:01 PST 2002
>Closed-Date:    Mon Mar 24 02:58:39 PST 2003
>Last-Modified:  Mon Mar 24 02:58:39 PST 2003
>Originator:     Erik Greenwald
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD fenris 5.0-CURRENT FreeBSD 5.0-CURRENT #3: Sun Dec 22 15:57:51 CST 2002 root@fenris:/usr/src/sys/i386/compile/FENRIS i386

>Description:

Patch to allow negative numbers in head. When given a negative number, it
prints all of the file EXCEPT the first N lines. (initiated by irc bs with
larne, rockshox, and hideaway)

>How-To-Repeat:

>Fix:

--- head.patch begins here ---
--- usr.bin/head/head.c.orig	Sun Dec 22 16:49:02 2002
+++ usr.bin/head/head.c	Sun Dec 22 17:15:43 2002
@@ -83,7 +83,7 @@
 			break;
 		case 'n':
 			linecnt = strtol(optarg, &ep, 10);
-			if (*ep || linecnt <= 0)
+			if (*ep || linecnt == 0)
 				errx(1, "illegal line count -- %s", optarg);
 			break;
 		case '?':
@@ -129,11 +129,22 @@
 	char *cp;
 	size_t error, readlen;
 
+	if(cnt>0)
 	while (cnt && (cp = fgetln(fp, &readlen)) != NULL) {
 		error = fwrite(cp, sizeof(char), readlen, stdout);
 		if (error != readlen)
 			err(1, "stdout");
 		cnt--;
+		}
+	else {
+		while (++cnt)
+		 	if (fgetln(fp, &readlen) == NULL)
+				return;
+		while ((cp = fgetln(fp, &readlen)) != NULL) {
+			error = fwrite(cp, sizeof(char), readlen, stdout);
+			if (error != readlen)
+				err(1, "stdout");
+		}
 	}
 }
 
--- head.patch ends here ---


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: tjr 
State-Changed-When: Mon Mar 24 02:57:46 PST 2003 
State-Changed-Why:  
Can already be done with "tail": tail +N somefile. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=46480 
>Unformatted:
 To: FreeBSD-gnats-submit@freebsd.org
 From: Erik Greenwald <erik@smluc.org>
 Reply-To: Erik Greenwald <erik@smluc.org>
 Cc: erik@smluc.org
 X-send-pr-version: 3.113
 X-GNATS-Notify: 
 
 
