title: C programming: How to get directory name?
body: <p>I'm writing a code for printing out the path from root to current directory or referred directory, using recursive function. but I can't get the directory name, only get ..
  Problem happened among base case and call
   <code>dirent-&gt;name</code>.</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;string.h&gt;
  #include &lt;dirent.h&gt;
  #include &lt;unistd.h&gt;
  
  static void list_dir (const char * dir_name)
  {
      DIR * d;
      struct dirent *e;
      struct stat sb;
      struct stat sb2;
      long childIno;
      long parentIno;
      char parent[200];
  
      stat(dir_name, &amp;sb);
      if (stat(dir_name, &amp;sb) == -1) {
          perror("stat");
          exit(EXIT_FAILURE);
      }
  
      childIno = (long) sb.st_ino;
  
      /* get parent dir name */
  
      snprintf(parent, sizeof(parent), "%s/..", dir_name);
      d = opendir(parent);
  
  
      stat(parent, &amp;sb2);
      if (stat(parent, &amp;sb2) == -1) {
          perror("stat2");
          printf("parent name: \n");
          exit(EXIT_FAILURE);
      }
      parentIno = (long) sb2.st_ino;
  
  
      if (d == NULL) {
          printf("Cannot open dircetory '%s'\n", parent);
      }
  
      /*below code is really messed up*/
      if (childIno == parentIno) {
          while ((e = readdir(d)) != NULL) {
              printf("base case %s\n", e-&gt;d_name);
          break;
           }
  
      }else{
          list_dir(parent);
  
      }
  
      /*code above here is really messed up*/
  
      /* After going through all the entries, close the directory. */
      closedir (d);
  }
  
  int main (int argc, char** argv)
  {
      list_dir (argv[1]);
      return 0;
  }
  </code></pre>
  
  <p>right result should be when typing command line</p>
  
  <pre><code>./pathto .
  </code></pre>
  
  <p>should print out path from root directory to my current directory</p>
  
  <p>or if command line as this</p>
  
  <pre><code>./pathto file.txt
  </code></pre>
  
  <p>should print out path from root directory to file.txt</p>