Sebi95
Sebi95

Reputation: 161

How to count only the number of directories from a path

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:

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->d_name, ".") || !strcmp(dp->d_name, ".."))
            continue;
        stat(dp->d_name, &s);
        if (S_ISDIR(s.st_mode))
        count++;
    }
    closedir(fd);
    return count;
}

Upvotes: 1

Views: 172

Answers (2)

Stian Skjelstad
Stian Skjelstad

Reputation: 2335

your stat() call will fail, since you are not in the correct directory. This you can solve by either changing the current directory, or generate full paths and give to stat as argument.

Some Unixes, you can optimize away the stat call by looking at struct dirent, the d_type field

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);
    }
    chdir (dir); /* needed for stat to work */
    while ((dp = readdir(fd)) != NULL) {
        if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
            continue;
#ifdef _DIRENT_HAVE_D_TYPE
        switch (dp->d_type)
        {
          case DT_UNKNOWN:
            stat(dp->d_name, &s);
            if (S_ISDIR(s.st_mode)) count++;
            break;
          case DT_DIR:
            count++;
            break;
        }
#else
        stat(dp->d_name, &s);
        if (S_ISDIR(s.st_mode)) count++;
#endif            
    }
    closedir(fd);
    return count;
}

Upvotes: 1

user6262880
user6262880

Reputation:

I think you want...

  • written in C
  • count the number of directoriesfrom a path.
  • the counting function will returns int value.

I have no idea about your environment, so it is just a example solution.

If you can use glob, it is so easy to count the number of directories. that is: main.c

#include <stdio.h>
#include <glob.h>
#include <string.h>

#define MAX_PATH 1023

int countDirectories(char* dir)
{
    char path[MAX_PATH] = "";
    strcat(path, dir);
    strcat(path, "/*");

    glob_t globbuf;
    long i, count = 0;

    if (glob(path, GLOB_NOSORT | GLOB_ONLYDIR, NULL, &globbuf) == 0)
    {
        count = globbuf.gl_pathc;
        for (i = 0; i < globbuf.gl_pathc; i++)
        {
            count += countDirectories(globbuf.gl_pathv[i]);
        }
    }
    globfree(&globbuf);

    return count;
}

int main(int argc, char* argv[])
{
    int count;

    if (argc > 1)
    {
        count = countDirectories(argv[1]);
    }
    else
    {
        count = countDirectories(".");
    }

    printf("there are %d directories.\n", count);

    return 0;
}

and you can try this like:

> gcc main.c -o testglob
> ./testglob /path/to/target/dir

then you'll receive output like this:

path = /path/to/target/dir/*, there are N directories

thanks.

Upvotes: 0

Related Questions