How to recursively walk through a directory and pr

2019-08-06 12:16发布

问题:

I am trying to recursively walk through a directory and print out all of the files.

When I try doing it with the code below, for example I call the function with sprint(".", ".") I get an output of a bunch of dots.

When I reduce the amount of open processes allowed (ulimit -n 20) I get 17 dots. I don't understand though because I thought readdir() and opendir() doesn't make new processes. Here is my code:

void sprint(char *filename, char * dirToOpen) {
    DIR *directory = opendir(dirToOpen);
    struct dirent *entry;
    struct stat s;
    char pathname[1024];
    if (directory) { /*if it's a directory*/
       while ((entry = readdir(directory)) != NULL) { /*while there are more entries to read*/
          if(strcmp(entry->d_name, filename) == 0) /*if entry name corresponds to filename, print it*/
             printf("%s\n", filename);
          sprintf(pathname, "./%s", entry->d_name); /*makes pathname*/
          if (lstat(pathname, &s) == 0 && S_ISDIR(s.st_mode)) { /*if the file is a directory*/
             if(strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) /*if the directory isn't . or ..*/
                sprint(filename, entry->d_name);
          }
       }
       closedir(directory);
    }
 }

Also somewhere along the way it doesn't reach the rest of the files because it only prints dots, not the full file name. I think it is somewhere in my last if loop but I'm not sure.

回答1:

Here is a recursive code that does that:

void sprint(char *filename, char * dirToOpen, int level = 0)
{
    DIR *dir;
    struct dirent *entry;
    struct stat s;

    if (!(dir = opendir(dirToOpen)))
        return;
    if (!(entry = readdir(dir)))
        return;

    do
    {
        if(lstat(dirToOpen, &s) == 0 && S_ISDIR(s.st_mode)) /*if it's a directory*/
        {
            char path[1024];
            int len = snprintf(path, sizeof(path)-1, "%s/%s", dirToOpen, entry->d_name); /*makes pathname*/
            path[len] = 0;
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) /*if the directory isn't . or ..*/
                continue;
            printf("%*s[%s]\n", level * 2, "", entry->d_name);
            sprint(filename ,path, level + 1);
        }
        else
        {
            if(strcmp(entry->d_name, filename) == 0 || strcmp(filename, ".") == 0) /*if entry name corresponds to filename, print it*/
             printf("%*s- %s\n", 2, "", entry->d_name);
        }
    } while (entry = readdir(dir)); /*while there are more entries to read*/
    closedir(dir);
}

Call it with sprint(".", "."); to recursively walk through a directory and print out all of the files.

Inspired from this answer.