title: Portable way to check if directory exists [Windows/Linux, C]
body: <p>I would like to check if a given directory exists. I know how to do this on Windows: </p>
  
  <pre><code>BOOL DirectoryExists(LPCTSTR szPath)
  {
    DWORD dwAttrib = GetFileAttributes(szPath);
  
    return (dwAttrib != INVALID_FILE_ATTRIBUTES &amp;&amp; 
           (dwAttrib &amp; FILE_ATTRIBUTE_DIRECTORY));
  }
  </code></pre>
  
  <p>and Linux:</p>
  
  <pre><code>DIR* dir = opendir("mydir");
  if (dir)
  {
      /* Directory exists. */
      closedir(dir);
  }
  else if (ENOENT == errno)
  {
      /* Directory does not exist. */
  }
  else
  {
      /* opendir() failed for some other reason. */
  }
  </code></pre>
  
  <p>But I need a portable way of doing this .. Is there any way to check if a directory exists no matter what OS Im using? Maybe C standard library way? </p>
  
  <p><strong>I know that I can use preprocessors directives and call those functions on different OSes but thats not the solution Im asking for.</strong></p>
  
  <p><strong>I END UP WITH THIS, AT LEAST FOR NOW:</strong></p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int dirExists(const char *path)
  {
      struct stat info;
  
      if(stat( path, &amp;info ) != 0)
          return 0;
      else if(info.st_mode &amp; S_IFDIR)
          return 1;
      else
          return 0;
  }
  
  int main(int argc, char **argv)
  {
      const char *path = "./TEST/";
      printf("%d\n", dirExists(path));
      return 0;
  }
  </code></pre>