Bug 96191

Summary: "cat" can display directory contents
Product: Base System Reporter: Bjoern Voigt <bjoern>
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 6.1-PRERELEASE   
Hardware: Any   
OS: Any   

Description Bjoern Voigt 2006-04-23 10:00:29 UTC
The FreeBSD's version of "cat" is able to display directory contains:

$ cat /etc
(garbage)

Solaris (tested with SunOS 5.9) shows the same behavior.

The behavior on directories in not described in the manual page.

Fix: 

"cat" from "coreutils" (GNU Linux) refuses to display directory contents.

$ cat /etc
cat: /etc: Is a directory
How-To-Repeat: $ cat /etc
Comment 1 Poul-Henning Kamp 2006-04-23 10:05:13 UTC
In message <200604230850.k3N8oWre066807@www.freebsd.org>, Bjoern Voigt writes:

>The FreeBSD's version of "cat" is able to display directory contains:
>
>$ cat /etc
>(garbage)

In UNIX a file is a file is a file.

But also, directories, pipes and devices are files.

And as such cat(1) works on them, as it should.

This is not a bug.

-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk@FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.
Comment 2 Maxim Konovalov freebsd_committer freebsd_triage 2006-04-23 10:12:31 UTC
State Changed
From-To: open->closed

As Poul explains this is not a bug.
Comment 3 Bjoern Voigt 2006-04-23 11:12:46 UTC
I know, that everything is a file on UNIX. But also a system should 
prevent a user from falling into a trap. Shell script authors can fall 
into a trap if they accidentally work on directories with "cat" and 
"cat" returns with exit code 0. I think the Linux' implementation is 
better here, but this is my personal opinion.

I looked at the problem more detailed now. I found, that the different 
behavior between "cat /etc" on FreeBSD and Linux does not come from the 
cat-tools itself. The difference comes from different implementations of 
the read(2) system call. Linux's read(2) call refuses to read from 
directories.

I tested FreeBSD's "cat" on Linux:

    * Linux has no support for 'udom' and does not know the __FBSDID
      macro (both is not important here)
    * compilation on Linux: gcc -g -o cat2 -DNO_UDOM_SUPPORT
      -D'__FBSDID(a)=/**/' cat.c
    * The result:
      $ ./cat2 /etc
      cat2: /etc: Is a directory

As a result also other UNIX tools behave different on Linux and on 
FreeBSD. For instance the same version of "bash" behaves different on 
Linux and on FreeBSD:

    FreeBSD:
    $ echo $0; head < /etc
    /usr/local/bin/bash
    (garbage)

    Linux:
     echo $0; head < /etc
    -bash
    head: error reading `standard input': Is a directory
Comment 4 freebsd 2006-04-24 15:05:23 UTC
in message <200604230850.k3N8oWre066807@www.freebsd.org>,
wrote Bjoern Voigt thusly...
>
> >Description:
> The FreeBSD's version of "cat" is able to display directory contains:
> 
> $ cat /etc
> (garbage)
> 
> Solaris (tested with SunOS 5.9) shows the same behavior.
> 
> The behavior on directories in not described in the manual page. 
> 
> >How-To-Repeat:
> $ cat /etc

I personally like this feature.

BTW, a user can edit the directory entries too, at least in vi
included w/ FreeBSD.


> >Fix:
> "cat" from "coreutils" (GNU Linux) refuses to display directory contents.
> 
> $ cat /etc
> cat: /etc: Is a directory

As a user i oppose the proposed change, regardless of the weight of my
opinion.


  - Parv

--