I was looking at an example in K&R 2 (8.6 Example - Listing Directories). It is a stripped down version of Linux command ls
or Windows' dir
. The example shows an implementation of functions like opendir
, readdir
. I've tried and typed the code word-by-word but it still doesn't work. All it does is that it prints the a dot (for the current directory) and exits.
One interesting thing I found in the code (in the implementation of readdir
) was that it was calling the system calls like open
and read
on directory. Something like -
int fd, n;
char buf[1000], *bufp;
bufp = buf;
fd = open("dirname", O_RDONLY, 0);
n = read(fd, bufp, 1000);
write(fd, bufp, n);
When I run this code I get no output even when the folder name "dirname"
has some files in it.
Also, the book says, that the implementation is for Version 7 and System V UNIX systems. Is that the reason why it is not working on Linux?
Here is the code- http://ideone.com/tw8ouX.
So does Linux not allow read
system calls on directories? Or something else is causing this?