The error number reference from the latest POSIX specification (POSIX.1-2008) states:
[EISDIR]
Is a directory. An attempt was made to open a directory with write mode specified.
This means that, on a POSIX compliant OS, you should be able to read() a directory if you opened it read only (O_RDONLY).
I've just tried this on a NetBSD (which really cares about POSIX) box, and works as expected, while it fails on GNU/Linux with EISDIR (which shouldn't happen).
A quick look at Linux shows this is intended (http://lxr.free-electrons.com/source/fs/libfs.c#L189):
ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
{
return -EISDIR;
}
While a concrete filesystem implementation can override it (like CEPH does: http://lxr.free-electrons.com/source/fs/ceph/dir.c#L1142), the default behavior is to return EISDIR whenever someone is trying to read() a directory, even if it's open read only.
I've traced this change back to 2.0.x, and at least for the ext2 filesystem, this was still the case.
So, yes, on a POSIX compliant OS you should be able to read a directory, but some kernels (like Linux and, apparently, others) just ignore this condition and break the standard.
cat .
while on Solaris 11 (as in Linux) you can't. Cool.