Thread: Problem with traversal of directories/files

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Problem with traversal of directories/files

    Hello, this is my first one here, please be kind if I'm doing something wrong both in coding and/or posting.


    So I'm trying to search a given folder for let's say "infected objects". In fact, I'm looking for filenames that match some filename patterns stored in a txt file. I have some problems when I'm traversing the directory tree. I recursively check any directory found, but when it is supposed to return and keep checking the previous directory, it just does not.

    Here's my code:

    Code:
    void vircheck(char* directory, char* option, char* mod)
    {
        char type;
        char pattern[10]="";
        char thePath[PATH_MAX], quar[PATH_MAX], pathBack[PATH_MAX];
        struct stat buf;
        struct dirent *dirp;    
        
        if ((dp = opendir(directory)) == NULL)
             err_die("Directory open error");
            
        while ((dirp = readdir(dp)) != NULL) 
        {
            if ( !strcmp(dirp->d_name,".") || !strcmp(dirp->d_name, "..") )
                continue;
                
            printf("%-30s\t", dirp->d_name);
            sprintf(thePath, "%s/%s", directory, dirp->d_name);    
            
            
            if (lstat(thePath, &buf)==-1) err_die("lstat failure");
            
         else if(S_ISREG(buf.st_mode))             
            { 
                printf("  regular file  ");
                rewind(names);
                while(!feof(names))
                {
                    fscanf(names,"%c\t%s\n",&type,pattern);
                    if ((type=='f')||(type=='*'))
                    {
                        if (fnmatch(pattern,dirp->d_name,FNM_FILE_NAME)==0)
                        {    printf("Infected object"); count++;
                            if (strcmp(option,"delete")==0) remove(thePath); 
                            else if (strcmp(option,"quarantine")==0) {sprintf(quar,"%s/%s",mod,dirp->d_name); rename(thePath,quar);}
                        }
                    }
                }
            }
         else if(S_ISDIR(buf.st_mode)) 
            { 
                printf("  directory  ");
                rewind(names);
                while(!feof(names))
                {
                    fscanf(names,"%c\t%s\n",&type,pattern);
                    if ((type=='d')||(type=='*'))
                    {
                        if (fnmatch(pattern,dirp->d_name,FNM_FILE_NAME)==0)
                        printf("Infected object");
                    }
                }                 
                
             printf("\n");                         
                  vircheck(thePath,option,mod);
                  sprintf(pathBack, "%s/%s", thePath, "..");
                   dp=opendir(pathBack);
                     
                 
            }
    This version loops indefinitely because it goes back to ".." and keeps doing the same again and again. You don't need to worry about what the "option" and "mod" stuff is, I just want some ideas how I can "flag" a directory so I can tell that I checked it, or what I'm doing wrong in the recursion thing. I also need to say that I'm running this on Cygwin and I'm not allowed to use any of the FTW functions (just readdir, opendir, chdir etc.)

    Thanks for your time fellas.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your problem is you're using global variables. They can be especially problematic with recursive functions. Read this: Global Variables Are Bad.

    Using globals here forces you to reopen the original directory using pathBack. Instead, declare dp locally, that way each recursive call to the vircheck function will have it's own list of directories, so when you return from the recursive call, the previous list of directories will be right where you left off. No need for pathBack and no need to call opendir again. Just make sure you call closedir before returning from the function.

    Also, read this: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com, then fix your while (!feof(names)) to check the return value of fscanf instead.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    You are bloody well right sir. I have to admit I toyed around and made it work completely by chance before reading your reply, but your explanation makes it all clear now. The use of global variables is a bad habit of mine - but I'll break free from it.

    Thanks for spending time reading this mate, appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Directories as regular files
    By Aculaniveus in forum C Programming
    Replies: 3
    Last Post: 01-26-2010, 04:26 PM
  2. depth-first traversal of directories
    By geekoftheweek in forum C Programming
    Replies: 0
    Last Post: 03-31-2009, 01:22 AM
  3. Directories and files using dirent.h
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 11-19-2008, 05:10 PM
  4. How to add C++ files from directories in makefile.am
    By Bargi in forum Linux Programming
    Replies: 0
    Last Post: 10-15-2007, 04:43 AM
  5. problem while opening files from multiple directories
    By V.G in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2004, 03:29 PM

Tags for this Thread