title: Need to clean up errno before calling function then checking errno?
body: <ol>
  <li><p>Do we need to reset <code>errno</code> to zero before calling a function? See below code. Now the scenario is <code>a_dest_path</code> is an existing directory. But when I execute the code, it always tries to <code>mkdir</code> but returns error says that the directory can't be created because it exists. In GDB, I check <code>errno</code> before calling <code>opendir()</code> and <code>errno</code> is 2. And it seems <code>errno</code> is not set to zero during calling <code>opendir()</code>. So do I need to reset <code>errno</code> to zero before calling <code>opendir()</code>?</p>
  </li>
  <li><p><code>errno</code> may be changed in <code>system()</code> calls, then in my <code>else if</code> branch I check the result from <code>system()</code> but not <code>opendir()</code>. So after <code>opendir()</code>, do I need to assign <code>errno</code> to a variable then check this variable in the <code>if..elseif..else</code> branch?</p>
  </li>
  </ol>
  
  <pre><code>DIR *dp = opendir(a_dest_path.c_str());
  if (errno == ENOENT) {
      string mkdir_comman = &quot;mkdir &quot; + a_dest_path;
      system(mkdir_command.c_str());
  } else if (errno == ENOTDIR) {
      printf(&quot;Destination %s exists but is not directory\n&quot;, a_dest_path.c_str());
      return k_error_not_directory;
  } else if (errno == 0) {
      closedir(dp);
  }
  </code></pre>