Im try to make a directory crawler.
Lets say this is the working directory for the program
Code:
Master Folder
Category 1(folder)
Folder 1
Folder 2
Category 2(folder)
Folder 3
Folder 4
file
file2
Id like to start in "Master Folder". From there i would like to enter every sub folder. So the program first sees "Category 1". It enters it and then prints all the folders in it.
Here what ive tried:
Code:
#define PORTAGE_DIR "."
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
int main() {
/* Holds info for the main portage directory. */
DIR *master;
struct dirent *master_dirent;
struct stat master_stat;
/* Holds info for the crawler which goes in every category folder. */
DIR *crawl;
struct dirent *crawl_dirent;
struct stat crawl_stat;
master = opendir(PORTAGE_DIR);
if(master != NULL) {
/* Read in every category folder. */
while( (master_dirent = readdir(master)) != 0 ) {
if( stat(master_dirent->d_name, &master_stat) == 0 ) {
/* Check to see if its a folder. */
if( S_ISDIR(master_stat.st_mode) != 0 ) {
/* It is a director, enter and crawl. */
/* Open folder. */
crawl = opendir(master_dirent->d_name);
while( (crawl_dirent = readdir(crawl)) != 0 ) {
if( S_ISDIR(crawl_stat.st_mode) != 0 )
printf("%s\n", crawl_dirent->d_name);
}
closedir(crawl);
}
}
}
closedir(master);
} else {
perror("Error: ");
}
return 0;
}
This gives me back nothing. I checked the FAQ and a couple of posts, im sure its right in front of me but i dont see the problem.
Thanks
<edit>
Id like thsi to work on Linux and Windows, if that cant be done than just Linux/Unix. And this is being done with GCC.