From nobody@FreeBSD.org  Mon Apr 16 17:55:26 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 730FC16A400
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:55:26 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.freebsd.org (Postfix) with ESMTP id 6256813C46C
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:55:26 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id l3GHtQam052753
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:55:26 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l3GHoPvi047606;
	Mon, 16 Apr 2007 17:50:25 GMT
	(envelope-from nobody)
Message-Id: <200704161750.l3GHoPvi047606@www.freebsd.org>
Date: Mon, 16 Apr 2007 17:50:25 GMT
From: Ricardo Nabinger Sanchez<rnsanchez@wait4.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: cat(1) misbehaves with directories as parameters
X-Send-Pr-Version: www-3.0

>Number:         111711
>Category:       bin
>Synopsis:       cat(1) misbehaves with directories as parameters
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 16 18:00:08 GMT 2007
>Closed-Date:    Tue Apr 17 13:29:45 GMT 2007
>Last-Modified:  Tue Apr 17 16:40:03 GMT 2007
>Originator:     Ricardo Nabinger Sanchez
>Release:        6.1 RELEASE
>Organization:
>Environment:
FreeBSD sauron.lan.box 6.1-RELEASE FreeBSD 6.1-RELEASE #0: Sun May  7 04:32:43 UTC 2006     root@opus.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
If a directory is passed to cat, it outputs its contents, (sometimes) mangles the terminal, and may misbehave, like in the second run with ". Makefile" as parameters.

rnsanchez@sauron:~...src/usr.bin/cat% cat -n Makefile .
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
     1  !g
          .cat.1.gz./~...src/usr.bin/cat% cat -n . Makefile 
     1  !g
          .cat.1.gz./~...src/usr.bin/cat%


The patch supplied fixes this behavior, adding a check to skip directories:

rnsanchez@sauron:~...src/usr.bin/cat% ./cat -n Makefile .
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
rnsanchez@sauron:~...src/usr.bin/cat% ./cat -n . Makefile 
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
rnsanchez@sauron:~...src/usr.bin/cat%

>How-To-Repeat:
Just cat(1) a directory.
>Fix:
Diff against -CURRENT sources attached.  Works OK on a 6.1-RELEASE.

Patch attached with submission follows:

Index: usr.bin/cat/cat.c
===================================================================
RCS file: /home/ncvs/src/bin/cat/cat.c,v
retrieving revision 1.32
diff -u -p -r1.32 cat.c
--- usr.bin/cat/cat.c	10 Jan 2005 08:39:20 -0000	1.32
+++ usr.bin/cat/cat.c	16 Apr 2007 17:32:00 -0000
@@ -136,6 +136,7 @@ scanfiles(char *argv[], int cooked)
 	int i = 0;
 	char *path;
 	FILE *fp;
+	struct stat sbuf;
 
 	while ((path = argv[i]) != NULL || i == 0) {
 		int fd;
@@ -145,6 +146,19 @@ scanfiles(char *argv[], int cooked)
 			fd = STDIN_FILENO;
 		} else {
 			filename = path;
+			/*
+			 * Check if path refers to a directory, in which case
+			 * it should not be opened.
+			 */
+			if (stat(path, &sbuf))
+				err(1, "%s", filename);
+			if (S_ISDIR(sbuf.st_mode)) {
+				/*
+				 * Silently skip this directory.
+				 */
+				++i;
+				continue;
+			}
 			fd = open(path, O_RDONLY);
 #ifndef NO_UDOM_SUPPORT
 			if (fd < 0 && errno == EOPNOTSUPP)

>Release-Note:
>Audit-Trail:

From: Charlie ROOT <root@zeta.org.au>
To: Ricardo Nabinger Sanchez <rnsanchez@wait4.org>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: bin/111711: cat(1) misbehaves with directories as parameters
Date: Tue, 17 Apr 2007 18:49:52 +1000 (EST)

 On Mon, 16 Apr 2007, Ricardo Nabinger Sanchez wrote:
 
 >> Description:
 > If a directory is passed to cat, it outputs its contents, (sometimes) mangles the terminal, and may misbehave, like in the second run with ". Makefile" as parameters.
 
 This is the expected behaviour.  Directories are files, and directory files
 are readable except on broken file systems.
 
 Bruce
State-Changed-From-To: open->closed 
State-Changed-By: remko 
State-Changed-When: Tue Apr 17 13:29:44 UTC 2007 
State-Changed-Why:  
Bruce reports that this is intended behaviour, resolve the PR 
accordingly 

http://www.freebsd.org/cgi/query-pr.cgi?pr=111711 

From: Ricardo Nabinger Sanchez <rnsanchez@wait4.org>
To: Charlie ROOT <root@zeta.org.au>
Cc: freebsd-gnats-submit@freebsd.org, freebsd-bugs@freebsd.org
Subject: Re: bin/111711: cat(1) misbehaves with directories as parameters
Date: Tue, 17 Apr 2007 13:37:49 -0300

 On Tue, 17 Apr 2007 18:49:52 +1000 (EST)
 Charlie ROOT <root@zeta.org.au> wrote:
 
 > >> Description:
 > > If a directory is passed to cat, it outputs its contents, (sometimes)
 > > mangles the terminal, and may misbehave, like in the second run with ".
 > > Makefile" as parameters.
 >=20
 > This is the expected behaviour.  Directories are files, and directory fil=
 es
 > are readable except on broken file systems.
 
 You mean UFS2?  :)
 
 % mount
 /dev/ad1s3a on / (ufs, local)
 devfs on /dev (devfs, local)
 /dev/ad1s2d on /home (ufs, local, soft-updates)
 /dev/ad1s3d on /tmp (ufs, local, soft-updates)
 procfs on /proc (procfs, local)
 
 (The test was under /home.)
 
 Thanks for the info.  In any case, cat do misbehave when both directories a=
 nd
 files are passed as parameters, as it doesn't cat anything else after the
 directory:
 
 % cat -n Makefile Makefile . Makefile Makefile
      1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
      2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien E=
 xp
 $ 3
      4  PROG=3D   cat
      5
      6  .include <bsd.prog.mk>
      1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
      2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien E=
 xp
 $ 3
      4  PROG=3D   cat
      5
      6  .include <bsd.prog.mk>
      1  !g
           .cat.1.gz=D9=E5=F8.=D8=F6/~...src/usr.bin/cat%
 
 
 Regards.
 
 --=20
 Ricardo Nabinger Sanchez     <rnsanchez@{gmail.com,wait4.org}>
 Powered by FreeBSD
 
   "Left to themselves, things tend to go from bad to worse."
>Unformatted:
