title: What is the correct way to use the stat() function to test if a DIRENT is a directory or a file?
body: <p>I'm having some trouble with the 'if(S_IFDIR(stbuf.st_mode))' line. Is this the correct way to test for a directory to recurse into? The function at the moment seems to do it right for 1 or 2 loops and then fails and segmentation faults.</p>
  
  <p>I've tried the following and probably more as the condition.</p>
  
  <pre><code>S_ISDIR(st_mode)
  ((st_mode &amp; ST_IFMT) == S_IFDIR)
  S_IFDIR(stbuf.st_mode)
  </code></pre>
  
  <p>I've included the whole function because I'm concerned the problem might be elsewhere.</p>
  
  <pre><code>void getFolderContents(char *source, int temp){
      struct stat stbuf;
      int isDir;
      dirPnt = opendir(source);
      if(dirPnt != NULL){
          while(entry = readdir(dirPnt)){
              char *c = entry-&gt;d_name;
              if(strcmp(entry-&gt;d_name, cwd) == 0 || strcmp(entry-&gt;d_name, parent) == 0){
              }
              else{
                  stat(entry-&gt;d_name, &amp;stbuf);
                  printf("%i %i ", S_IFMT, stbuf.st_mode);
                  if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                  printf("DIR: %s\n", entry-&gt;d_name);
                  getFolderContents(entry-&gt;d_name, 0);
              }
              printf("FILE: %s\n", entry-&gt;d_name);
          }
      }
      closedir(dirPnt);
  }
  </code></pre>