Hello, this is my first one here, please be kind if I'm doing something wrong both in coding and/or posting.
So I'm trying to search a given folder for let's say "infected objects". In fact, I'm looking for filenames that match some filename patterns stored in a txt file. I have some problems when I'm traversing the directory tree. I recursively check any directory found, but when it is supposed to return and keep checking the previous directory, it just does not.
Here's my code:
Code:
void vircheck(char* directory, char* option, char* mod)
{
char type;
char pattern[10]="";
char thePath[PATH_MAX], quar[PATH_MAX], pathBack[PATH_MAX];
struct stat buf;
struct dirent *dirp;
if ((dp = opendir(directory)) == NULL)
err_die("Directory open error");
while ((dirp = readdir(dp)) != NULL)
{
if ( !strcmp(dirp->d_name,".") || !strcmp(dirp->d_name, "..") )
continue;
printf("%-30s\t", dirp->d_name);
sprintf(thePath, "%s/%s", directory, dirp->d_name);
if (lstat(thePath, &buf)==-1) err_die("lstat failure");
else if(S_ISREG(buf.st_mode))
{
printf(" regular file ");
rewind(names);
while(!feof(names))
{
fscanf(names,"%c\t%s\n",&type,pattern);
if ((type=='f')||(type=='*'))
{
if (fnmatch(pattern,dirp->d_name,FNM_FILE_NAME)==0)
{ printf("Infected object"); count++;
if (strcmp(option,"delete")==0) remove(thePath);
else if (strcmp(option,"quarantine")==0) {sprintf(quar,"%s/%s",mod,dirp->d_name); rename(thePath,quar);}
}
}
}
}
else if(S_ISDIR(buf.st_mode))
{
printf(" directory ");
rewind(names);
while(!feof(names))
{
fscanf(names,"%c\t%s\n",&type,pattern);
if ((type=='d')||(type=='*'))
{
if (fnmatch(pattern,dirp->d_name,FNM_FILE_NAME)==0)
printf("Infected object");
}
}
printf("\n");
vircheck(thePath,option,mod);
sprintf(pathBack, "%s/%s", thePath, "..");
dp=opendir(pathBack);
}
This version loops indefinitely because it goes back to ".." and keeps doing the same again and again. You don't need to worry about what the "option" and "mod" stuff is, I just want some ideas how I can "flag" a directory so I can tell that I checked it, or what I'm doing wrong in the recursion thing. I also need to say that I'm running this on Cygwin and I'm not allowed to use any of the FTW functions (just readdir, opendir, chdir etc.)
Thanks for your time fellas.