Solved

NEED HELP READING DIRECTORY CONTENTS RECURSIVELY

Posted on 2004-04-13
7
553 Views
Last Modified: 2010-05-18
Hi everyone!

This is my problem:
I'm trying to read the contents of a directory recursively and below is my code so far... I'm sure I have a lot of mistakes but could anyone help me? When I run this program I get Segmentation fault ! What should I do???


DIR* directory;
struct dirent* pdirent;
struct stat  Stat;

void perform( char* A )
{

        if(stat(A , &Stat) < 0)
        {
                printf("Error !!!!");
                return;
        }

        directory = opendir(A);
        while( (pdirent = readdir(directory)) != NULL)
        {

                if((pdirent -> d_name != ".") && (pdirent->d_name != ".."))
                {
                        printf("%s \n" , pdirent -> d_name);

                        if(S_ISDIR(Stat.st_mode))
                        {
                                perform(pdirent-> d_name);

                        }
                 }
        }

        closedir(directory);

}


In main I call perform( argv[1] )



I'd appreciate any help !!!
0
Comment
Question by:akiskal
7 Comments
 
LVL 1

Expert Comment

by:ravenscr98
ID: 10818318
First, make your variables local to the function.  Each directory needs its own DIR* pointer, otherwise when you backtrack from the recursive call the variable directory has changed its value.

Also, when you test to see if the current file name is a directory in order to recursively descend, you are testing Stat for the current directory you are in, not for the file name you have just read.

Look at those two issues.  If you still get a seg fault, figure out where it is and I'm sure one of the experts here can help.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10819091
THere are a few more problems: You cannot compare strings with != - you need to use strcmp(), also, when you call perform() recursively, you are only passing in the subdirectory, and not the whole directory (consisting of the base dirctory (A) plus the subdir.
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 125 total points
ID: 10819181
Try this:

#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <dirent.h>
#include <linux/limits.h>

void
perform (char *A)
{
  DIR *directory;
  struct dirent *pdirent;
  struct stat Stat;
  directory = opendir (A);
  while ((pdirent = readdir (directory)) != NULL)
    {
      if (strcmp (pdirent->d_name, ".") && strcmp (pdirent->d_name, ".."))
        {
          printf ("%s \n", pdirent->d_name);
          char newpath[PATH_MAX];
          sprintf (newpath, "%s/%s", A, pdirent->d_name);
          if (stat (newpath, &Stat) < 0)
            {
              printf ("Error !!!!\n");
              return;
            }
          if (S_ISDIR (Stat.st_mode))
            {
              perform (newpath);
            }
        }
    }
  closedir (directory);
}

int
main (int argc, char *argv[])
{
  perform (argv[1]);
}

But keep in mind that I'm not doing any error checking. As soon as you try to e.g. traverse into a directory that you cannot read, it will segfault. But other than that it does work correctly. There was another problem with the call to stat(): You only called it for the directory, but not for any of the directory entries you read.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Accepted Solution

by:
Alf666 earned 175 total points
ID: 10819253
The following will work. But you have a lot to learn. I suggest you examine line by line...

#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

void perform( char* A )
{
  DIR *directory;
  struct dirent *pdirent;
  struct stat Stat;
  struct stat fileStat ;
  char fileName[FILENAME_MAX] ;

  if(stat(A , &Stat) < 0) {
    perror(A);
    return;
  }

  directory = opendir(A);

  while ((pdirent = readdir(directory)) != NULL) {

    if (strncmp(pdirent->d_name, ".", 1)
      && strncmp(pdirent->d_name, "..", 2)) {

      if (snprintf(fileName, FILENAME_MAX, "%s/%s", A, pdirent->d_name)
        >= FILENAME_MAX) {
      fprintf(stderr, "File name too long : %s", fileName) ;
      return ;
      }

      if(stat(fileName , &fileStat) < 0) {
      perror(fileName);
      continue ;
      }
      printf("%s \n" , fileName);
     
      if(S_ISDIR(fileStat.st_mode)) {
      printf("doing : %s\n" , fileName);
      perform(fileName);
      }

    }
  }
  closedir(directory);
}


main(int argc, char **argv) {

  perform( argv[1] ) ;
}
0
 

Author Comment

by:akiskal
ID: 10822364
Thanks very much guys I really appreciate your help!!!!  

khkremer I made a mistake when I accepted tha last answer and I didn't give you any points!!! I'm really sorry but I really thank you for your help!

Everything worked fine with Alf666 's code and now I can go on with my work! Thanks again guys!
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10822386
You can post a request in the Support area (see the link at the top of the page) and have them re-open this question (happens all the time). This will allow you to accept the correct answer(s) again.  
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now