LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Tried but in vain to list contents of a directory (https://www.linuxquestions.org/questions/programming-9/tried-but-in-vain-to-list-contents-of-a-directory-520251/)

rs_vijay 01-17-2007 02:52 PM

Tried but in vain to list contents of a directory
 
Hi all,

Having learnt directory too is a interpreted as a file , I tried opening it in vi editor and i found its contents(i mean files n sub-dirs) there , maybe , i thought these strings are symbolic links to the actual contents in memory..

Later, when I tried to print same contents using "cat" it gave me an error that the argument is a directory and hence I wrote a simple C code to open the directory argument as a file (it is ofcourse) n list its contents parsing the file character by char. When i compile n run it with some sub-directory as an argument , unfortunately it didn't print anything.

So this time i edited my code such that it prints the no of bytes too in this (directory) file (just seeking the file pointer to the end n printing its offset using ftell()) n when i run it it gives me the fixed integer "4096" for many directories which i have tried (including the / directory), but still no contents(i.e no bytes) being displayed ..

I felt it crazy n something is there in these directory files which i yet donno ,

as the code appears elementary i don't think its necessary posting it here...

just learnt C in UNIX ( all those editing n using cc,gdb,linking libraries , running executables..) , i am a sort of beginner in UNIX ...
hope i am pretty clear with my doubt.. n any solution is greatly appreciated...

- Vijay

mackdav 01-17-2007 03:22 PM

vim interprets directories as files for your convenience to assist you in getting to the file that you actually want. They are not "files". There should be "readdir" functions you can use to read directories.

PTrenholme 01-17-2007 03:36 PM

Quote:

Originally Posted by mackdav
vim interprets directories as files for your convenience to assist you in getting to the file that you actually want. They are not "files". There should be "readdir" functions you can use to read directories.

And, of course, you can get the source for the "readdir" function -- or, perhaps, the "ls" command -- and see how the directory is read.

Note that a directory, like almost everyting in *nix systems, is a file, but some files require "special" handeling. The information need to do so is (I believe) to be found in the POSIX standards for UNIX systems. (That's a guess, since I've never actually looked at the standards.) :study:

matthewg42 01-17-2007 03:59 PM

Getting the raw bytes of a directory entry wouldn't be so useful, since the contents (and hence what to do with them to achieve some goal) will depend on the underlying filesystem you are using.


All times are GMT -5. The time now is 05:16 PM.