Im in '/home/martin/crawl'. Then i make a 3 level directory example like this:
Code:
#!/bin/bash
echo "thanks for running this"
mkdir sub1 sub2 sub3
cd sub1
mkdir test1 test2 test3
cd ..
cd sub2
mkdir test4 test5 test6
cd ..
cd sub3
mkdir test7 test9 test9
cd ..
When i run the program, it sees sub 1, enters it and list whats in it. It repeats till theres nothing left. Put it only print directories.
Heres what im trying
Code:
#define PORTAGE_DIR "//home//martin//crawl"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
int isdir(struct dirent **check_dirent) {
struct stat check_stat;
if( stat((*check_dirent)->d_name, &check_stat) == 0 ) {
if( S_ISDIR(check_stat.st_mode) != 0 ) {
if( strcmp( (*check_dirent)->d_name , "." ) != 0 ) {
if( strcmp( (*check_dirent)->d_name , ".." ) != 0 ) {
return 0;
}
}
}
}
return 1;
}
int main() {
DIR *master = opendir(PORTAGE_DIR);
struct dirent *master_dirent;
DIR *crawl;
struct dirent *crawl_dirent;
char *open = NULL;
if(master != NULL) {
while( (master_dirent = readdir(master)) != 0 ) {
if( isdir(&master_dirent) == 0 ) {
open = malloc( strlen(PORTAGE_DIR) + strlen(master_dirent->d_name) + 3 );
sprintf(open, "%s//%s", PORTAGE_DIR, master_dirent->d_name);
crawl = opendir(open);
while( (crawl_dirent = readdir(crawl)) != 0 ) {
if( isdir(&crawl_dirent) == 0 ) {
printf("%s\n", crawl_dirent->d_name);
} else {
printf("\tfailed at %s\n", crawl_dirent->d_name);
}
}
closedir(crawl);
}
}
closedir(master);
} else {
perror("Error: ");
}
return 0;
}
The line
Code:
if( stat(&check_dirent->d_name, &check_stat) == 0 ) {
Seems to cause some problems. If i get rid of the error checking it works.
Why does that seem to fix it?