LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Opening a directory WITHOUT using 'opendir' and 'readdir' (https://www.linuxquestions.org/questions/programming-9/opening-a-directory-without-using-opendir-and-readdir-116293/)

skywalker27182 11-15-2003 08:09 AM

Opening a directory WITHOUT using 'opendir' and 'readdir'
 
Hi,
i am trying to write a program in 'C' (on RedHat 9) that lists all the files in a directory using only 'open' and 'read' and its details like space, inode no, etc. i remember reading in a book that UNIX treats all directories as normal files. so it is possible, right? but i couldn't do it. i saw in Ritchie's book on 'C' that 'readdir' and 'opendir' does use 'read' and 'open'. but it also uses 'malloc'. someone please tell me why and how to go about the program

ToniT 11-15-2003 02:10 PM

The striucture of the actual directory entry is dependant of the underlying filesystem. You do want to use readdir(3) or if you want a closer (linux spesific) interface, you can use open(2) and getdents(2) to read the contents of the directory. The read(2) gives you a 'EISDIR fd refers to a directory.' error if you try to access the directory directly.

coldfusion 11-16-2003 01:32 PM

you could use exec

you could do something like this:


execl("/bin/ls","/bin/ls","/",NULL);


though, if i recall correctly, exec will take over your program with "ls"


:edited: mistyped the execl :o


All times are GMT -5. The time now is 01:20 AM.