Solved

NEED HELP READING DIRECTORY CONTENTS RECURSIVELY

Posted on 2004-04-13
7
557 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Percona cluster member randomly crash 15 459
Chinese translation in RedHat 10 47
Linux 10 106
looking for a CENTOS ISO to download with x window installed 2 19
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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

20 Experts available now in Live!

Get 1:1 Help Now