Thread: Directory crawler problems

  1. #31
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    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?

  2. #32
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    orbitz_ from #c(irc) helped me out.

    Code:
    #define PORTAGE_DIR     "I:\\tmp"
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dirent.h>
    #include <unistd.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);
                    chdir(open);
                    
                    crawl = opendir(open);
                    
                    while( (crawl_dirent = readdir(crawl)) != 0 ) {
                        if( isdir(&crawl_dirent) == 0 ) {
                            printf("%s\n", crawl_dirent->d_name);
                        }
                    }
                    
                    closedir(crawl);
                    chdir("..");
                }
            }
            
            closedir(master);
        } else {
            perror("Error: ");
        }
    
        return 0;
    }
    The problem was i didnt change the directory every time. I thought my opendir calls would do that...
    Last edited by mart_man00; 08-17-2003 at 09:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding files in a directory
    By smarta_982002 in forum C Programming
    Replies: 1
    Last Post: 01-25-2008, 10:10 AM
  2. Building a tree from a directory structure
    By geekoftheweek in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 03:15 AM
  3. About current working directory
    By George2 in forum Linux Programming
    Replies: 3
    Last Post: 05-02-2007, 10:34 PM
  4. fprintf in directory
    By rkooij in forum C Programming
    Replies: 9
    Last Post: 03-09-2006, 04:23 AM
  5. problems with my first attempt at a Qt application...
    By Captain Penguin in forum C++ Programming
    Replies: 0
    Last Post: 10-01-2002, 09:14 PM