From owner-freebsd-current@FreeBSD.ORG Tue Jun 9 10:48:54 2009 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7824A106566B; Tue, 9 Jun 2009 10:48:54 +0000 (UTC) (envelope-from hhasenbe@techfak.uni-bielefeld.de) Received: from smarthost.TechFak.Uni-Bielefeld.DE (smarthost.piip.TechFak.Uni-Bielefeld.DE [129.70.137.17]) by mx1.freebsd.org (Postfix) with ESMTP id 06D358FC18; Tue, 9 Jun 2009 10:48:53 +0000 (UTC) (envelope-from hhasenbe@techfak.uni-bielefeld.de) Received: from [129.70.104.75] (navy.spectrum.uni-bielefeld.de [129.70.104.75]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smarthost.TechFak.Uni-Bielefeld.DE (Postfix) with ESMTP id 93E2C481FB; Tue, 9 Jun 2009 12:17:36 +0200 (CEST) Message-ID: <4A2E3579.20105@techfak.uni-bielefeld.de> Date: Tue, 09 Jun 2009 12:12:09 +0200 From: Hendrik Hasenbein User-Agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103) MIME-Version: 1.0 To: Thomas Backman References: <60F36765-E4FA-4A2D-AD6B-E7881537F6DA@exscape.org> In-Reply-To: <60F36765-E4FA-4A2D-AD6B-E7881537F6DA@exscape.org> X-Enigmail-Version: 0.95.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: freebsd-current@freebsd.org, Ivan Voras Subject: Re: FS utils treates directories as files? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Jun 2009 10:48:54 -0000 Thomas Backman wrote: > > On Jun 9, 2009, at 11:03 AM, Ivan Voras wrote: > >> Thomas Backman wrote: >>> FreeBSD 8.0-CURRENT r193521 (Jun 5), bash: >>> [root@chaos /usr/ports]# file / >>> /: directory >>> [root@chaos /usr/ports]# cat / >>> �g��=[root@chaos /usr/ports]# >>> [root@chaos /usr/ports]# cat /usr/ports/mail >>> � >> >> This is the traditional behaviour because yes, directories are just >> simply ordinary files with a special bit set to distinguish them. Other >> systems might have modified "cat" to check if directories are files but >> it's not standard. >> >> You can easily check this yourself. The following small program should >> work on every unix-ish system: >> >> #include >> #include >> #include >> >> int main() { >> int fd, i; >> char buf[512]; >> >> fd = open(".", O_RDONLY); >> read(fd, buf, 512); >> for (i = 0; i < 512; i++) >> printf("%4d ", buf[i]); >> } > > Yes, I realize that, and actually added a stat() call to cat to check > for directories... before I realized it was true for other utils as well. > I still think it's weird, though, and that the utils should check (as > long as they return gibberish; less /etc on my GNU/Linux system actually > shows a readable list of files - it seems as if less /etc == ls -al /etc > | less). Is there *any* use for this behaviour, or is it simply there > because nobody has added a check? In rare cases you might want to look at the real content of the directory file. Unix is all about having the choice. The linux variant should give a hint of the form 'This file is a directory. Do you want to 'ls -la | less' instead? Like it does for 8bit content. It isn't a GNU/linux feature. It depends on the distribution. Try a slackware or a debian (debian does it sun style, but more /etc != less /etc). They all handle it different. So you better don't use these features if you want to retain portability. (Example don't use #!/bin/sh and use bash features. It works on many systems, but not all.) Hendrik