title: C Recursive Directory copy of sub folders
body: <p>I'm having issues getting the following code to run to recursively copy sub-folders in C. I saw this in another post but the code seem to not run the if statement to check if the current file is a directory. </p>
  
  <pre><code>void SearchDirectory(const char *name) {
  DIR *dir = opendir(name);                
  if(dir) {
      char Path[256], *EndPtr = Path;
      struct dirent *e;
      strcpy(Path, name);                  
      EndPtr += strlen(name);              
      while((e = readdir(dir)) != NULL) {  
          struct stat info;                
          strcpy(EndPtr, e-&gt;d_name);       
          if(!stat(Path, &amp;info)) {         //code stops here and won't check if the current file is a directory or not..
              if(S_ISDIR(info.st_mode)) {  
  
                  SearchDirectory(Path);   
              } else if(S_ISREG(info.st_mode) { 
                  //Copy routine
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><em>Edit</em></p>
  
  <p>So I added a slash on the the end of the path and it seem to find the directory but is crashing with stack error on execution. I think it is recursing without end. New code is:</p>
  
  <pre><code>void SearchDirectory(const char *name) {
  DIR *dir = opendir(name);                
  if(dir) {
      char Path[256], *EndPtr = Path;
      struct dirent *e;
      strcpy(Path, name);   
  strcat(Path, slash);               
      EndPtr += (strlen(name)+1);              
      while((e = readdir(dir)) != NULL) {  
          struct stat info;                
          strcpy(EndPtr, e-&gt;d_name);       
          if(!stat(Path, &amp;info)) {         //code stops here and won't check if the current file is a directory or not..
              if(S_ISDIR(info.st_mode)) {  
  
                  SearchDirectory(Path);   
              } else if(S_ISREG(info.st_mode) { 
                  //Copy routine
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>