title: How to count only the number of directories from a path
body: <p>I am trying to count only the directories from a path, but it doesn't work. So, i don't want to number both files and directories, i want only the directories. Could you help me, please?
  The code:</p>
  
  <pre><code>int listdir(char *dir) {
      struct dirent *dp;
      struct stat s;
      DIR *fd;
      int count = 0;
  
      if ((fd = opendir(dir)) == NULL) {
          fprintf(stderr, "listdir: can't open %s\n", dir);
      }
      while ((dp = readdir(fd)) != NULL) {
          if (!strcmp(dp-&gt;d_name, ".") || !strcmp(dp-&gt;d_name, ".."))
              continue;
          stat(dp-&gt;d_name, &amp;s);
          if (S_ISDIR(s.st_mode))
          count++;
      }
      closedir(fd);
      return count;
  }
  </code></pre>