I'm currently working on a 4 part project, the last part of which is to recursively list the names of all files in a directory tree. The program works fine if run from the cwd but when I try to pass in a directory as a command line argument the program won't open any subdirectories, it just prints the name. I've tested and narrowed the issue down (I think) to the S_ISDIR call not returning true if I'm not in the cwd but I really don't see why it would do that. I'm actually having a similar problem with another part of the project so I don't know if I'm just misunderstanding directory traversal or what. Any help would be greatly appreciated. Thanks! This is what I have so far for the function:
void recursiveSearch(char* dir){
DIR* cur = opendir(dir);
assert(cur != NULL);
struct dirent* d;
struct stat* s = malloc(sizeof(struct stat));
while((d = readdir(cur)) != NULL){
if((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
continue;
stat(d->d_name, s);
if(S_ISDIR(s->st_mode))
recursiveSearch(d->d_name);
printf("%s\n", d->d_name);
}
closedir(cur);
return;
}
Here's my not-so-elegant solution after getting advice:
while((d = readdir(cur)) != NULL){
if((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
continue;
char* buf = malloc(sizeof(char) * BUFSIZ);
buf = realpath(dir, buf);
strcat(buf, "/");
strcat(buf, d->d_name);
stat(buf, s);
if(S_ISDIR(s->st_mode))
recursiveSearch(buf);
printf("%s\n", d->d_name);
}