Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include <dirent.h>
- #define ARGS_COUNT 2 // Not including the program name
- #define PERCENT_20 0.2 // 20%
- #define PERCENT_80 0.8 // 80%
- #define PERCENT_100 1 // 100%
- #define BUFFER 1000 // Buffer for the logger (log builder)
- void writeLog();
- void myExit(int code);
- void checkArgs(int argc);
- void chooseAndScan(char** argv);
- void printWelcome(char** argv);
- void freeMemory(char** filesList, int filesNum);
- void sortFiles(char** filesList, int filesNum);
- void normalScan(char** filesList, char* virusSign, int filesNum);
- void quickScan(char** filesList, char* virusSign, int filesNum);
- int nextOne(FILE* file);
- int countFiles(char* folderDirectory);
- int getFiles(char* folderDirectory, char** filesList);
- bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo);
- enum scanTypes
- {
- normalScanType, // 0
- quickScanType // not 0
- } scanType;
- //String builder that will copied later into Log File (func writeLog())
- char logBuilder[BUFFER] = { 0 };
- int main(int argc, char** argv)
- {
- checkArgs(argc);
- /*
- * argv[0] = Program Name
- * argv[1] = Folder to scan
- * argv[2] = Virus signature
- */
- printWelcome(argv);
- chooseAndScan(argv);
- writeLog();
- getchar();
- return 0;
- }
- /// <summary>
- /// Checks if the arguments of the program correctly
- /// </summary>
- /// <param name="argc">Number of the given arguments</param>
- void checkArgs(int argc)
- {
- if (argc != ARGS_COUNT + 1)
- {
- printf("Invalid execution.\nUsage:\n"
- "> Antivirus <Folder_to_scan [directory]> <Virus_signature [directory]>\n");
- myExit(1);
- }
- }
- /// <summary>
- /// Prints the welcome screen with the given information (folder to scan, virus sign.)
- /// </summary>
- /// <param name="argv">Arguments that was passed with the program</param>
- void printWelcome(char** argv)
- {
- printf("Welcome to my Virus Scan!\n\n");
- printf("Folder to scan: %s\n"
- "Virus signature: %s\n\n", argv[1], argv[2]);
- strcat(logBuilder, "Anti-virus began! Welcome!\n\n");
- strcat(logBuilder, "Folder to scan:\n");
- strcat(logBuilder, argv[1]);
- strcat(logBuilder, "\nVirus signature:\n");
- strcat(logBuilder, argv[2]);
- strcat(logBuilder, "\n\n");
- }
- /// <summary>
- /// The user choses which type of scanning he prefer, and begin the scan
- /// </summary>
- /// <param name="argv">Arguments that was passed with the program</param>
- void chooseAndScan(char** argv)
- {
- char** filesList = (char**)malloc(sizeof(char*) * countFiles(argv[1]));
- if (filesList == NULL)
- {
- printf("Unsuccessful malloc!\n");
- myExit(1);
- }
- int filesNum = getFiles(argv[1], filesList);
- sortFiles(filesList, filesNum); // sort alpha-betical
- printf("Press 0 for a normal scan or any other key for quick scan: ");
- scanf("%d", &scanType);
- getchar();
- strcat(logBuilder, "Scanning option:\n");
- printf("Scanning began...\n"
- "This process may take several minutes...\n\n");
- switch (scanType)
- {
- case normalScanType: // 0
- {
- strcat(logBuilder, "Normal Scan\n\n");
- normalScan(filesList, argv[2], filesNum);
- break;
- }
- default: // quickScanType (not 0)
- {
- strcat(logBuilder, "Quick Scan\n\n");
- quickScan(filesList, argv[2], filesNum);
- break;
- }
- }
- printf("Scan Completed.\n");
- freeMemory(filesList, filesNum);
- }
- /// <summary>
- /// Writes the contect of the builder string (logBuilder) into the file (AntiVirusLog.txt)
- /// </summary>
- void writeLog()
- {
- FILE* fileLog = fopen("AntiVirusLog.txt", "w+");
- if (fileLog == NULL)
- {
- printf("Error to open the file\n");
- myExit(1);
- }
- fputs(logBuilder, fileLog);
- fclose(fileLog);
- }
- /// <summary>
- /// Checks if the given fileToCheck infected with the given virus signature
- /// </summary>
- /// <param name="fileToCheck">File to check</param>
- /// <param name="virusSign">Virus signature</param>
- /// <param name="scanFrom">Scans from (from which pos. scan the fileToCheck)</param>
- /// <param name="scanTo">Scans until (till which pos. scan the fileToCheck)</param>
- /// <returns>True if infected, false if not</returns>
- bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo)
- {
- int checkData = 0, virusData = 0;
- double fileSize = 0;
- FILE* checkFile = fopen(fileToCheck, "rb");
- if (checkFile == NULL)
- {
- printf("Error opening file: %s\n", fileToCheck);
- myExit(1);
- }
- FILE* virusFile = fopen(virusSign, "rb");
- if (virusFile == NULL)
- {
- fclose(checkFile);
- printf("Error opening file: %s\n", virusSign);
- myExit(1);
- }
- fseek(checkFile, 0L, SEEK_END);
- fileSize = ftell(checkFile); // gets the file "size"
- fseek(checkFile, 0L, SEEK_SET);
- /*
- * 0.2 - 20% || 0.8 - 80% || 1 = 100%
- * from 0 to 0.2 -> first 20%
- * from 0.8 to 1 -> last 20% (1 - 8 = 0.2 ~ 20%)
- */
- while ((checkData = fgetc(checkFile)) != EOF) {
- if ((fileSize * scanFrom <= ftell(checkFile)) && (fileSize * scanTo >= ftell(checkFile))) // scan only in the given range
- {
- virusData = fgetc(virusFile);
- if (nextOne(virusFile) == EOF) // Check if the virus sign FULLY exists in the given file to check
- {
- fclose(checkFile);
- fclose(virusFile);
- return true;
- }
- else if (virusData != checkData) // If the virus data doesn't equals to the given file data, "reset" the data "counter"
- {
- fseek(virusFile, 0, SEEK_SET); // start over the virus data
- }
- }
- }
- fclose(checkFile);
- fclose(virusFile);
- return false;
- }
- /// <summary>
- /// Normal scan the files in the given folder according the virus signature
- /// </summary>
- /// <param name="virusSign">Virus signature</param>
- /// <param name="filesList">List of the files to check in the folder</param>
- /// <param name="filesNum">Number of the files to check</param>
- void normalScan(char** filesList, char* virusSign, int filesNum)
- {
- int i = 0;
- strcat(logBuilder, "Results:\n");
- printf("Scanning:\n");
- for (i = 0; i < filesNum; i++)
- {
- strcat(logBuilder, filesList[i]);
- if (isInfected(filesList[i], virusSign, 0, PERCENT_100))
- {
- strcat(logBuilder, " Infected!\n");
- printf("%s - Infected!\n", filesList[i]);
- }
- else
- {
- strcat(logBuilder, " Clean\n");
- printf("%s - Clean\n", filesList[i]);
- }
- }
- }
- /// <summary>
- /// Quick scan the files in the given folder according the virus signature
- /// </summary>
- /// <param name="virusSign">Virus signature</param>
- /// <param name="filesList">List of the files to check in the folder</param>
- /// <param name="filesNum">Number of the files to check</param>
- void quickScan(char** filesList, char* virusSign, int filesNum)
- {
- int i = 0;
- strcat(logBuilder, "Results:\n");
- printf("Scanning:\n");
- for (i = 0; i < filesNum; i++)
- {
- strcat(logBuilder, filesList[i]);
- if (isInfected(filesList[i], virusSign, 0, PERCENT_20))
- {
- strcat(logBuilder, " Infected! (first 20%)\n");
- printf("%s - Infected! (first 20%%)\n", filesList[i]);
- }
- else if (isInfected(filesList[i], virusSign, PERCENT_80, PERCENT_100))
- {
- strcat(logBuilder, " Infected! (last 20%)\n");
- printf("%s - Infected! (last 20%%)\n", filesList[i]);
- }
- else if (isInfected(filesList[i], virusSign, 0, PERCENT_100))
- {
- strcat(logBuilder, " Infected!\n");
- printf("%s - Infected!\n", filesList[i]);
- }
- else
- {
- strcat(logBuilder, " Clean\n");
- printf("%s - Clean\n", filesList[i]);
- }
- }
- }
- /// <summary>
- /// Counts the number of the files in the given folder directory
- /// </summary>
- /// <param name="folderDirectory">Folder directory to count the files</param>
- /// <returns>Number of files in the given directory</returns>
- int countFiles(char* folderDirectory)
- {
- int counter = 0;
- DIR* d = 0;
- struct dirent* dir = 0;
- d = opendir(folderDirectory);
- if (d == NULL)
- {
- printf("Error opening directory: %s\n", folderDirectory);
- myExit(1);
- }
- while ((dir = readdir(d)) != NULL)
- {
- if (strcmp(dir->d_name, ".") &&
- strcmp(dir->d_name, "..") &&
- dir->d_type != DT_DIR)
- {
- counter++;
- }
- }
- closedir(d);
- return counter;
- }
- /// <summary>
- /// Frees the dynimaclly allocated memory
- /// </summary>
- /// <param name="filesList">List (String Array) of the files</param>
- /// <param name="filesNum">Number of the files in the list</param>
- void freeMemory(char** filesList, int filesNum)
- {
- int i = 0;
- for (i = 0; i < filesNum; i++)
- {
- free(filesList[i]);
- }
- free(filesList);
- }
- /// <summary>
- /// Custom exit with getchar() made-in
- /// </summary>
- /// <param name="code">Code to exit with</param>
- void myExit(int code)
- {
- getchar();
- exit(code);
- }
- /// <summary>
- /// Returns the files into filesList (String Array)
- /// </summary>
- /// <param name="folderDirectory">Get files from this folder</param>
- /// <param name="filesList">Where to storage the files names</param>
- /// <returns>Number of the files</returns>
- int getFiles(char* folderDirectory, char** filesList)
- {
- int i = 0;
- DIR* d = 0;
- struct dirent* dir = 0;
- d = opendir(folderDirectory);
- if (d == NULL)
- {
- printf("Error opening directory: %s\n", folderDirectory);
- myExit(1);
- }
- while ((dir = readdir(d)) != NULL)
- {
- if (strcmp(dir->d_name, ".") &&
- strcmp(dir->d_name, "..") &&
- dir->d_type != DT_DIR)
- {
- filesList[i] = (char*)malloc(sizeof(char) * (strlen(folderDirectory) + strlen(dir->d_name) + 3));
- /* Why 3?
- * 1 - "/"
- * 2 - '\0' of folderDirectory
- * 3 - '\0' of dir->d_name
- * strlen(char* str) returns the length of the string WITHOUT the \0 which is +1 to the length that strlen() returns
- */
- if (filesList[i] == NULL)
- {
- while (i)
- {
- i--;
- free(filesList[i]);
- }
- free(filesList);
- printf("Unsuccessful malloc!\n");
- myExit(1);
- }
- strcpy(filesList[i], folderDirectory);
- strcat(filesList[i], "/");
- strcat(filesList[i], dir->d_name);
- i++;
- }
- }
- closedir(d);
- return i;
- }
- /// <summary>
- /// Reads the next data from the given file (only read and returns him, without moving the current position)
- /// </summary>
- /// <param name="file">File</param>
- /// <returns>Data (int) that was read from the file</returns>
- int nextOne(FILE* file)
- {
- int position = ftell(file);
- int data = fgetc(file); // get the "nextOne" data
- fseek(file, position, SEEK_SET); // returns to the position that was before the last move
- return data;
- }
- /// <summary>
- /// Sorts the files in the array alpha-betical
- /// </summary>
- /// <param name="filesList">List of the files (String array)</param>
- /// <param name="filesNum">Number of files in the list</param>
- void sortFiles(char** filesList, int filesNum)
- {
- int i = 0, j = 0;
- char* temp = 0;
- for (i = 0; i < filesNum; i++) //bubble sort strings
- {
- for (j = 0; j < filesNum - 1; j++)
- {
- if (strcmp(filesList[j], filesList[i]) > 0) //dictionary comparison
- {
- //pointers swap
- temp = filesList[i];
- filesList[i] = filesList[j];
- filesList[j] = temp;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment