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.
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.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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.
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.
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.
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?
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?
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
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
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!
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.
From this perspective, you have the routines that you need as long as the called "function" can operate on the data passed to it.