Link to home
Start Free TrialLog in
Avatar of crazy4s
crazy4s

asked on

questions

Hi all,
I was asked to write a directory traversal program in c (using unix). I know directory traversal is something like a recursive directory tree that finds the path and name, am i right? Can someone explain to me in more details as in what is this used for and what other system calls (other than opendir, readdir, chdir) should i include in my program?
Any help will be greatly appreciated.
Avatar of sjm_ee
sjm_ee
Flag of United Kingdom of Great Britain and Northern Ireland image

I would interpret "directory traversal" to mean to "visiting" a named directory and each sub-directory recursively and perform some function on each directory. The function may be to print the name of the directory but it could be something else. For example, consider the command "find DIR -type d -exec COMMAND {} \;" which will execute COMMAND against every directory.under DIR.

From this perspective, you have the routines that you need as long as the called "function" can operate on the data passed to it.
Avatar of crazy4s
crazy4s

ASKER

so do you meant that we can print in the tabbed format names of all files in specified directories all subdir tabbed by depth?
and i remembered that we should include getcwd in our program too, if we're to use this system call in my prog is this able to print the current directory and sub directories in it and also check whether the sub directories is a directory instead a file and if is a directory we'll continue to print out the subdir of the particular directory?
and one more thing what does the macro IS_DIR used in this prog? do we need this?
and in your given example, is that a command line, i'm not really understand?
sorry for asking so many questions but i'm really slow in catching up stuffs, hope you understand.
thank you.
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> and one more thing what does the macro IS_DIR used in this prog? do we need this?
    In the listing 5 is the macro S_ISDIR.

Printing the tree in the form of a tree can get complicated and should be asked in a different question. Printing the tree like ls -R prints it is a bit easier.
Avatar of crazy4s

ASKER

well at first i'm not sure what do but now i think he wants us to do recursive as i remembered he told us to put something like chdir("..") at the end of the loop, and that means back to the parent directory, am i correct?
.. is the parent folder.
Avatar of crazy4s

ASKER

i read the link that you provided earlier, i roughly understand what it is about but i'm not quite sure for some of them.
what is a symbolic links, can you provide some example? and for the lstat, is this used to determine whether it is a dir or a reg file and hence collect the information from it and the print it out?
Avatar of crazy4s

ASKER

so a recursive directory traversal prog is an equivalent for the ls -R command,  they'll have the same output?
but i don't really understand why do we need to back to the parent folder (the chdir(".."))? Is it because the files in this directory might be a directory that's why we need to do that again and print out the files in this subdir?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you sure, the word directory in "Directory Traversal" refers to a directory or a folder?
I feel that, it could be something like searching a "Telephone Directory".
If so, then you should use the "Divide and Conquer" strategy.
directory traversal means going thru each file/dir in the given directory, up to the leaf level. That is, if there is a directory within a directory, traverse recursively in that.

You should be doing a recursive program, with a template similar to this

function to go thru each file in the directory
    if this is a file, print its name (or anything else that you need to do)
    if this is a directory, call the same function again with the directory

The above will recursively go thru each file within the given directory and act as you require
Avatar of crazy4s

ASKER

i'm not too sure what's symbolic links but during his lecture he did used lstat to check the mode so i assumed that we've to use lstat instead of stat.
okay i get the recursive meaning here.
i'll start my codings now thanks for all the help!
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.