FS utils treates directories as files?

Ivan Voras ivoras at freebsd.org
Tue Jun 9 09:03:38 UTC 2009


Thomas Backman wrote:
> FreeBSD 8.0-CURRENT r193521 (Jun 5), bash:
> [root at chaos /usr/ports]# file /
> /: directory
> [root at chaos /usr/ports]# cat /
> �g��=[root at chaos /usr/ports]#
> [root at 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 <stdio.h>
#include <stdlib.h>
#include <sys/fcntl.h>

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]);
}



More information about the freebsd-current mailing list