From kbyanc@teapot.egroups.com  Mon Jun 26 17:47:32 2000
Return-Path: <kbyanc@teapot.egroups.com>
Received: from teapot.egroups.net (teapot.egroups.net [63.204.207.250])
	by hub.freebsd.org (Postfix) with SMTP id 1471737B69B
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 26 Jun 2000 17:47:32 -0700 (PDT)
	(envelope-from kbyanc@teapot.egroups.com)
Received: (qmail 17300 invoked from network); 27 Jun 2000 00:47:31 -0000
Received: (QMFILT: 1.0); 27 Jun 2000 01:47:31 -0000
Received: from dhcp147.corp.onelist.com (HELO kbyanc.corp.ONElist.com) (192.168.10.147)
  by teapot.egroups.net with SMTP; 27 Jun 2000 00:47:31 -0000
Received: (from kbyanc@localhost)
	by kbyanc.corp.ONElist.com (8.9.3/8.9.3) id RAA23860;
	Mon, 26 Jun 2000 17:47:31 -0700 (PDT)
	(envelope-from kbyanc@teapot.egroups.com)
Message-Id: <200006270047.RAA23860@kbyanc.corp.ONElist.com>
Date: Mon, 26 Jun 2000 17:47:31 -0700 (PDT)
From: kbyanc@posi.net
Sender: kbyanc@teapot.egroups.com
Reply-To: kbyanc@posi.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: patch to prevent head'ing directories
X-Send-Pr-Version: 3.2

>Number:         19536
>Category:       bin
>Synopsis:       patch to prevent head'ing directories
>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:   Mon Jun 26 17:50:00 PDT 2000
>Closed-Date:    Thu Jun 14 06:11:18 PDT 2001
>Last-Modified:  Thu Jun 14 06:12:19 PDT 2001
>Originator:     Kelly Yancey
>Release:        FreeBSD 4.0-STABLE i386
>Organization:
>Environment:

FreeBSD backroom.corp.ONElist.com 5.0-CURRENT FreeBSD 5.0-CURRENT #3: Sat
Jun 10 12:08:26 PDT 2000
kbyanc@backroom.corp.ONElist.com:/usr/src/sys/compile/BACKROOM  i386

>Description:

	Similar to my previous PR (19514), except this patch affects
	head(1) rather than tail(1). Otherwise it is identical. This patch
	is made again -current head.c.

>How-To-Repeat:

	head .

>Fix:

Index: usr.bin/head/head.c
===================================================================
RCS file: /home/cvs/src/usr.bin/head/head.c,v
retrieving revision 1.10
diff -u -r1.10 head.c
--- usr.bin/head/head.c	1999/08/28 01:01:58	1.10
+++ usr.bin/head/head.c	2000/06/27 00:40:28
@@ -45,6 +45,7 @@
   "$FreeBSD: src/usr.bin/head/head.c,v 1.10 1999/08/28 01:01:58 peter Exp $";
 #endif /* not lint */
 
+#include <sys/stat.h>
 #include <sys/types.h>
 
 #include <ctype.h>
@@ -73,6 +74,7 @@
 	char *argv[];
 {
 	register int ch;
+	struct stat sb;
 	FILE *fp;
 	int first, linecnt = -1, bytecnt = -1;
 	char *ep;
@@ -103,11 +105,22 @@
 		linecnt = 10;
 	if (*argv) {
 		for (first = 1; *argv; ++argv) {
-			if ((fp = fopen(*argv, "r")) == NULL) {
+			if ((fp = fopen(*argv, "r")) == NULL ||
+			    fstat(fileno(fp), &sb)) {
 				warn("%s", *argv);
 				eval = 1;
 				continue;
 			}
+			if (sb.st_mode & S_IFDIR)
+				errx(1, "%s is a directory", *argv);
+			if (sb.st_mode & S_IFLNK)
+				/* This should transparently be resolved and
+				 * thus never happen.
+				 */
+				errx(1, "%s is a symlink", *argv);
+			if (sb.st_mode & S_IFWHT)
+				/* This should never happen. */
+				errx(1, "%s is a whiteout entry", *argv);
 			if (argc > 1) {
 				(void)printf("%s==> %s <==\n",
 				    first ? "" : "\n", *argv);

>Release-Note:
>Audit-Trail:

From: Kelly Yancey <kbyanc@posi.net>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: subject=Re: bin/19536: patch to prevent head'ing directories
Date: Mon, 26 Jun 2000 22:34:47 -0700 (PDT)

   Attached is a more correct patch, please apply it instead. Thanks,
 
   Kelly
 
 Index: usr.bin/head/head.c
 ===================================================================
 RCS file: /home/cvs/src/usr.bin/head/head.c,v
 retrieving revision 1.10
 diff -u -r1.10 head.c
 --- usr.bin/head/head.c	1999/08/28 01:01:58	1.10
 +++ usr.bin/head/head.c	2000/06/27 05:25:44
 @@ -45,6 +45,7 @@
    "$FreeBSD: src/usr.bin/head/head.c,v 1.10 1999/08/28 01:01:58 peter Exp $";
  #endif /* not lint */
  
 +#include <sys/stat.h>
  #include <sys/types.h>
  
  #include <ctype.h>
 @@ -73,6 +74,7 @@
  	char *argv[];
  {
  	register int ch;
 +	struct stat sb;
  	FILE *fp;
  	int first, linecnt = -1, bytecnt = -1;
  	char *ep;
 @@ -103,11 +105,22 @@
  		linecnt = 10;
  	if (*argv) {
  		for (first = 1; *argv; ++argv) {
 -			if ((fp = fopen(*argv, "r")) == NULL) {
 +			if ((fp = fopen(*argv, "r")) == NULL ||
 +			    fstat(fileno(fp), &sb)) {
  				warn("%s", *argv);
  				eval = 1;
  				continue;
  			}
 +			if (S_ISDIR(sb.st_mode))
 +				errx(1, "%s is a directory", *argv);
 +			if (S_ISLNK(sb.st_mode))
 +				/* This should transparently be resolved and
 +				 * thus never happen.
 +				 */
 +				errx(1, "%s is a symlink", *argv);
 +			if (S_ISWHT(sb.st_mode))
 +				/* This should never happen. */
 +				errx(1, "%s is a whiteout entry", *argv);
  			if (argc > 1) {
  				(void)printf("%s==> %s <==\n",
  				    first ? "" : "\n", *argv);
 
 

From: Kelly Yancey <kbyanc@egroups.net>
To: freebsd-gnats-submit@FreeBSD.org
Cc: bde@zeta.org.au
Subject: Re: bin/19536: patch to prevent head'ing directories
Date: Wed, 28 Jun 2000 16:41:47 -0700 (PDT)

   Update to patch in response to comments from BDE.
 
   -Kelly
 
 Index: usr.bin/head/head.c
 ===================================================================
 RCS file: /home/cvs/src/usr.bin/head/head.c,v
 retrieving revision 1.10
 diff -u -r1.10 head.c
 --- usr.bin/head/head.c	1999/08/28 01:01:58	1.10
 +++ usr.bin/head/head.c	2000/06/28 23:36:45
 @@ -45,6 +45,7 @@
    "$FreeBSD: src/usr.bin/head/head.c,v 1.10 1999/08/28 01:01:58 peter Exp $";
  #endif /* not lint */
  
 +#include <sys/stat.h>
  #include <sys/types.h>
  
  #include <ctype.h>
 @@ -73,8 +74,9 @@
  	char *argv[];
  {
  	register int ch;
 +        struct stat sb;
  	FILE *fp;
 -	int first, linecnt = -1, bytecnt = -1;
 +	int first, linecnt = -1, bytecnt = -1, num = 0;
  	char *ep;
  
  	obsolete(argv);
 @@ -103,12 +105,24 @@
  		linecnt = 10;
  	if (*argv) {
  		for (first = 1; *argv; ++argv) {
 -			if ((fp = fopen(*argv, "r")) == NULL) {
 +			if ((fp = fopen(*argv, "r")) == NULL ||
 +			    fstat(fileno(fp), &sb)) {
  				warn("%s", *argv);
  				eval = 1;
  				continue;
  			}
 -			if (argc > 1) {
 +			if (S_ISDIR(sb.st_mode)) {
 +				warnx("%s is a directory", *argv);
 +				continue;
 +			} else if (S_ISLNK(sb.st_mode)) {
 +				warnx("%s is a symlink", *argv);
 +				continue;
 +			} else if (S_ISWHT(sb.st_mode)) {
 +				warnx("%s is a whiteout entry", *argv);
 +				continue;
 +			}
 +			num++;
 +			if (num > 1) {
  				(void)printf("%s==> %s <==\n",
  				    first ? "" : "\n", *argv);
  				first = 0;
 
 
State-Changed-From-To: open->closed 
State-Changed-By: schweikh 
State-Changed-When: Thu Jun 14 06:11:18 PDT 2001 
State-Changed-Why:  
We think that it's better to stick to the WYGIWYAF principle 
(what you get is what you asked for). As bde points out, 
why should "tail /" not do the same thing as "cat / | tail"? 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=19536 
>Unformatted:
