I want to write a program that given a filename searches in the current directory and its subdirectories for that file. The names of the subdirectories containing that file should be printed.
But my code prints just the subdirectories.
#include <dirent.h>
#include <stdio.h>
#include <string.h>
void listDir(char *path, char *name)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir(path)) != NULL) {
while (( ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG && name == ent->d_name) {
printf("%s\n", ent->d_name);
}
if (ent->d_type == DT_DIR && strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
printf("%s\n", ent->d_name);
listDir("/home/dir1/ent->d_name", name);
}
}
closedir(dir);
}
}
void main()
{
listDir("/home/dir1", "file1");
}