Solved

NEED HELP READING DIRECTORY CONTENTS RECURSIVELY

Posted on 2004-04-13
7
560 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Equivalent solution for gettimezoneinformation 12 135
linux rpm install 5 228
install command in linux 1 54
How to run a Linux Command from a different other than Root in Linux 5 114
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 fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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