From nobody@FreeBSD.org  Mon Apr 16 17:57:59 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 3E65F16A406
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:57:59 +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 1575013C44C
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:57:59 +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 l3GHvwdo056094
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 16 Apr 2007 17:57:58 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id l3GHqv8g050400;
	Mon, 16 Apr 2007 17:52:57 GMT
	(envelope-from nobody)
Message-Id: <200704161752.l3GHqv8g050400@www.freebsd.org>
Date: Mon, 16 Apr 2007 17:52:57 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:         111712
>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:09 GMT 2007
>Closed-Date:    Mon Apr 16 18:06:10 GMT 2007
>Last-Modified:  Mon Apr 16 18:06:10 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:
State-Changed-From-To: open->closed 
State-Changed-By: erwin 
State-Changed-When: Mon Apr 16 18:06:00 UTC 2007 
State-Changed-Why:  
Duplicate of bin/111711. 

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