On Mon, Sep 18, 2006 at 11:42:29AM +0200, Roman Zilka wrote:
> > I was wondering why Linux doesn't treat directories like files, as many
> > other unix implementations do.
> > For example, in Linux, you can't do 'cat .' while on FreeBSD you can.
> > Why? There is a practical reason?
> 
> I'd say it's not a matter of how Linux treats directories
> (putting aside the problem of diverse filesystems), but how
> coreutils or "cat", to be precise, treats directories. You could just as
> well implement such a feature into 'cat' which would make it behave like
> it does on FreeBSD when called on a directory. As to why Linux's "cat"
> acts the way it does...try asking GNU guys.:)

I was not talking about cat itself, but about open() and read().

> Btw, in my place:
> $ uname -a
> FreeBSD howdy123 6.1-PRERELEASE FreeBSD 6.1-PRERELEASE #0: Wed Apr  5
> 12:22:42 CEST 2006     [EMAIL PROTECTED]:/usr/obj/usr/src/sys/GORGO  i386
> $ cat .
> cat: .: Is a directory
> $

Strange, my FreeBSD box, things works differently...
I was asking because, in FreeBSD I can do this bit of magic, but in
Linux not (not really useful, just proving that in Linux, not everything
is a file ;-).

[EMAIL PROTECTED]:~/ $ uname -a
FreeBSD watson.octopus 6.1-STABLE FreeBSD 6.1-STABLE #14: Wed Aug 23 14:47:09 
CEST 2006     [EMAIL PROTECTED]:/usr/obj/usr/src/sys/WATSON  i386
[EMAIL PROTECTED]:~ $ mkdir prova
[EMAIL PROTECTED]:~ $ od -c prova/
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  364 001 004 002   .   .  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000040   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
[EMAIL PROTECTED]:~ $ cd prova/
[EMAIL PROTECTED]:~/prova $ touch file
[EMAIL PROTECTED]:~/prova $ touch file2
[EMAIL PROTECTED]:~/prova $ od -c .
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020   \f  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
[EMAIL PROTECTED]:~/prova $ rm file
[EMAIL PROTECTED]:~/prova $ od -c .
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  034  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000
[EMAIL PROTECTED]:~/prova $ ls
file2

And it's not caused by special code in od, it's using simple open()
and read() calls, as proved by this simple C code:
[EMAIL PROTECTED]:~ $ cat mycat.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
  int fd;
  char buffer;

  fd=open(argv[1], O_RDONLY);
  while (read(fd, &buffer, 1) == 1)
    printf("%c", buffer);

  exit(EXIT_SUCCESS);
}
[EMAIL PROTECTED]:~ $ make mycat
cc -O2 -pipe -march=pentium4  mycat.c  -o mycat
[EMAIL PROTECTED]:~ $ ./mycat prova | od -c
0000000  020 342 002  \0  \f  \0 004 001   .  \0  \0  \0 001   p 001  \0
0000020  034  \0 004 002   .   .  \0  \0 026 342 002  \0 020  \0  \b 004
0000040    f   i   l   e  \0   `   ' 303 032 342 002  \0 330 001  \b 005
0000060    f   i   l   e   2  \0   ' 303  \0  \0  \0  \0  \0  \0  \0  \0
0000100   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001000


Bye.


-- 
 * Pillon Matteo
-- 
[email protected] mailing list

Reply via email to