Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <string.h>
- int searchVirus(char* virus, char* fileName);
- int main(int argc, char** argv){
- DIR *dp;
- char directory[100];
- struct dirent *ep;
- int check;
- FILE * resultFile;
- char result1[] = " not infected";
- char result2[] = " INFECTED!";
- resultFile = fopen("AntiVirusLog.txt", "wb");
- printf("please write the name of the directory you want to search the files in\n");
- scanf("%s", directory);
- dp = opendir(directory);
- if (dp != NULL)
- {
- while (ep = readdir(dp))
- {
- check=searchVirus(argv[1],ep->d_name);
- printf("%d", check);
- if (check == 1)
- {
- int ret_code = fwrite(strcat(ep->d_name, result2), sizeof(char), strlen(result2), resultFile);
- if (ret_code != strlen(result2))
- {
- printf("there was a problem");
- }
- }
- if (check == 2)
- {
- int ret_code = fwrite(strcat(ep->d_name, result1), sizeof(char), strlen(result2), resultFile);
- if (ret_code != strlen(result2))
- {
- printf("there was a problem");
- }
- }
- }
- (void)closedir(dp);
- }
- else
- {
- perror("Couldn't open the directory");
- }
- fclose(resultFile);
- system("PAUSE");
- return (0);
- }
- int searchVirus(char* virus, char* fileName)/*if returns 0 something was wrong, if returns 1 the file
- is with a virus if returns 2 the file is okay*/
- {
- FILE * virusFile;
- FILE * checkFile;
- int ch;
- char firstByte[1];//the first byte of the virus
- int length = 0;//what is the length of the virus
- int i;
- virusFile = fopen(virus, "rb");
- if (virusFile == NULL)
- {
- printf("Error opening first file");
- return 0;
- }
- checkFile = fopen(fileName, "rb");
- if (virusFile == NULL)
- {
- fclose(virusFile);
- printf("Error opening second file");
- return 0;
- }
- while ((ch = fgetc(virusFile)) != EOF)
- {
- length++;
- }
- char* strCheck = (char*)malloc(sizeof(char)*(length + 1));
- char* virusBinary = (char*)malloc(sizeof(char)*(length + 1));
- size_t ret_code1 = fread(virusBinary, sizeof (char), length, virusFile);
- printf("%d", ret_code1);
- if (ret_code1 == length)
- {
- size_t ret_code2 = fread(firstByte, 1, 1, virusFile);
- if (ret_code2 == 1)
- {
- while ((ch = fgetc(checkFile)) != EOF)
- {
- if (ch == firstByte)
- {
- i = 0;
- while (i < length && ch != firstByte)
- {
- strCheck[i] = ch;
- ch = fgetc(checkFile);
- i++;
- }
- }
- if (strcmp(strCheck, virusBinary) == 0)
- {
- return 1;
- }
- fseek(checkFile, (-1), SEEK_CUR);
- }
- return 2;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 3;
- }
- free(strCheck);
- free(virusBinary);
- fclose(virusFile);
- fclose(checkFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement