/*
* Recursive function that searches for a given file in a given folder.
* input: Char pointer to the path folder.
* Char pointer to the target file name.
* Char pointer to the type of file to search for.
*/
void find_file(char* path, char* target, char typeValue){
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(path))){
perror("Could not open directory.");
return;
}
while ((entry = readdir( dir )) != NULL) {
if ( !strcmp( entry->d_name, "." )) continue;
if ( !strcmp( entry->d_name, ".." )) continue;
struct stat s;
char fullpath[strlen(path) + strlen(entry->d_name) + 2];
sprintf(fullpath, "%s/%s", path, entry->d_name);
lstat(fullpath, &s);
// If entry is a directory - add directory to list
if (S_ISDIR(s.st_mode)) {
addFolderToList(fullpath);
}
// Switch over type value, only print matches with correct type
if (strcmp(target, entry->d_name) == 0) {
switch (typeValue){
case ('a'):
printf("%s \n", fullpath);
break;
case ('d'):
if (S_ISDIR(s.st_mode)) printf("%s \n", fullpath);
break;
case ('l'):
if(S_ISLNK(s.st_mode)) printf("%s \n", fullpath);
break;
case ('f'):
if(S_ISREG(s.st_mode)) printf("%s \n", fullpath);
break;
}
}
}
closedir(dir);
}