1 |
On Mon, Sep 18, 2006 at 11:42:29AM +0200, Roman Zilka wrote: |
2 |
> > I was wondering why Linux doesn't treat directories like files, as many |
3 |
> > other unix implementations do. |
4 |
> > For example, in Linux, you can't do 'cat .' while on FreeBSD you can. |
5 |
> > Why? There is a practical reason? |
6 |
> |
7 |
> I'd say it's not a matter of how Linux treats directories |
8 |
> (putting aside the problem of diverse filesystems), but how |
9 |
> coreutils or "cat", to be precise, treats directories. You could just as |
10 |
> well implement such a feature into 'cat' which would make it behave like |
11 |
> it does on FreeBSD when called on a directory. As to why Linux's "cat" |
12 |
> acts the way it does...try asking GNU guys.:) |
13 |
|
14 |
I was not talking about cat itself, but about open() and read(). |
15 |
|
16 |
> Btw, in my place: |
17 |
> $ uname -a |
18 |
> FreeBSD howdy123 6.1-PRERELEASE FreeBSD 6.1-PRERELEASE #0: Wed Apr 5 |
19 |
> 12:22:42 CEST 2006 root@howdy123:/usr/obj/usr/src/sys/GORGO i386 |
20 |
> $ cat . |
21 |
> cat: .: Is a directory |
22 |
> $ |
23 |
|
24 |
Strange, my FreeBSD box, things works differently... |
25 |
I was asking because, in FreeBSD I can do this bit of magic, but in |
26 |
Linux not (not really useful, just proving that in Linux, not everything |
27 |
is a file ;-). |
28 |
|
29 |
pmatthew@watson:~/ $ uname -a |
30 |
FreeBSD watson.octopus 6.1-STABLE FreeBSD 6.1-STABLE #14: Wed Aug 23 14:47:09 CEST 2006 root@××××××.octopus:/usr/obj/usr/src/sys/WATSON i386 |
31 |
pmatthew@watson:~ $ mkdir prova |
32 |
pmatthew@watson:~ $ od -c prova/ |
33 |
0000000 020 342 002 \0 \f \0 004 001 . \0 \0 \0 001 p 001 \0 |
34 |
0000020 364 001 004 002 . . \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 |
35 |
0000040 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 |
36 |
* |
37 |
0001000 |
38 |
pmatthew@watson:~ $ cd prova/ |
39 |
pmatthew@watson:~/prova $ touch file |
40 |
pmatthew@watson:~/prova $ touch file2 |
41 |
pmatthew@watson:~/prova $ od -c . |
42 |
0000000 020 342 002 \0 \f \0 004 001 . \0 \0 \0 001 p 001 \0 |
43 |
0000020 \f \0 004 002 . . \0 \0 026 342 002 \0 020 \0 \b 004 |
44 |
0000040 f i l e \0 ` ' 303 032 342 002 \0 330 001 \b 005 |
45 |
0000060 f i l e 2 \0 ' 303 \0 \0 \0 \0 \0 \0 \0 \0 |
46 |
0000100 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 |
47 |
* |
48 |
0001000 |
49 |
pmatthew@watson:~/prova $ rm file |
50 |
pmatthew@watson:~/prova $ od -c . |
51 |
0000000 020 342 002 \0 \f \0 004 001 . \0 \0 \0 001 p 001 \0 |
52 |
0000020 034 \0 004 002 . . \0 \0 026 342 002 \0 020 \0 \b 004 |
53 |
0000040 f i l e \0 ` ' 303 032 342 002 \0 330 001 \b 005 |
54 |
0000060 f i l e 2 \0 ' 303 \0 \0 \0 \0 \0 \0 \0 \0 |
55 |
0000100 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 |
56 |
* |
57 |
0001000 |
58 |
pmatthew@watson:~/prova $ ls |
59 |
file2 |
60 |
|
61 |
And it's not caused by special code in od, it's using simple open() |
62 |
and read() calls, as proved by this simple C code: |
63 |
pmatthew@watson:~ $ cat mycat.c |
64 |
#include <stdio.h> |
65 |
#include <stdlib.h> |
66 |
#include <sys/types.h> |
67 |
#include <sys/stat.h> |
68 |
#include <fcntl.h> |
69 |
|
70 |
int main(int argc, char **argv) |
71 |
{ |
72 |
int fd; |
73 |
char buffer; |
74 |
|
75 |
fd=open(argv[1], O_RDONLY); |
76 |
while (read(fd, &buffer, 1) == 1) |
77 |
printf("%c", buffer); |
78 |
|
79 |
exit(EXIT_SUCCESS); |
80 |
} |
81 |
pmatthew@watson:~ $ make mycat |
82 |
cc -O2 -pipe -march=pentium4 mycat.c -o mycat |
83 |
pmatthew@watson:~ $ ./mycat prova | od -c |
84 |
0000000 020 342 002 \0 \f \0 004 001 . \0 \0 \0 001 p 001 \0 |
85 |
0000020 034 \0 004 002 . . \0 \0 026 342 002 \0 020 \0 \b 004 |
86 |
0000040 f i l e \0 ` ' 303 032 342 002 \0 330 001 \b 005 |
87 |
0000060 f i l e 2 \0 ' 303 \0 \0 \0 \0 \0 \0 \0 \0 |
88 |
0000100 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 |
89 |
* |
90 |
0001000 |
91 |
|
92 |
|
93 |
Bye. |
94 |
|
95 |
|
96 |
-- |
97 |
* Pillon Matteo |
98 |
-- |
99 |
gentoo-user@g.o mailing list |