title: C: Checking the type of a file. Using lstat() and macros doesn't work
body: <p>I use opendir() to open a directory and then readdir() and lstat() to get the stats of each file in that directory. Following this <a href="http://linux.die.net/man/2/stat" rel="noreferrer">manpage</a> I wrote the code under which doesn't work as thought. It does list all the files in the current directory but it doesn't print out whever the file is a regular file, a symlink or a directory.</p>
  
  <pre><code>#include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;stdio.h&gt;
  
  void main(){
  
      char* folder=".";                                     //folder to open
  
      DIR* dir_p;
      struct dirent* dir_element;
      struct stat file_info;
  
      // open directory
      dir_p=opendir(folder);
  
      // show some info for each file in given directory
      while(dir_element = readdir(dir_p)){
  
          lstat(dir_element-&gt;d_name, &amp;file_info);          //getting a file stats
  
          puts(dir_element-&gt;d_name);                       // show current filename
          printf("file mode: %d\n", file_info.st_mode);
  
          // print what kind of file we are dealing with
          if (file_info.st_mode == S_IFDIR) puts("|| directory");
          if (file_info.st_mode == S_IFREG) puts("|| regular file");
          if (file_info.st_mode == S_IFLNK) puts("|| symbolic link");
      }
  
  }
  </code></pre>