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.