Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************
- * Name: Yair Benamou.*
- * Lesson: 8. *
- * Ex: Mid Project *
- **********************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<conio.h>
- #include<malloc.h>
- // No Prototypes
- int fileSize(FILE* pToFile);
- /**
- The function 'fileLen' get a file and count how much characters he have.
- Input: pToFile - Getting the pointer to the file.
- Output: numCharacters - The counter.
- */
- int fileSize(FILE* pToFile)
- {
- int count = 0;
- while (fgetc(pToFile) != EOF) // Counting the chars
- {
- count++;
- }
- return (count);
- }
- /**
- The function 'choseScan' ask the user which scan he wants to do.
- Inputs: None.
- Outputs: The user's choice.
- */
- int choseScan(void)
- {
- int choice;
- printf("\nWhich scan you want to do?\n1) FAST SCAN.\n2) REGULAR SCAN.\n");
- scanf("%d", &choice);
- return(choice);
- }
- /**
- The function 'fastScan' gets a file and his size. The function will scan the file
- on the fast way: 20% of the file beginning, and 20% from the end.
- Input: pToFile - The file to scan
- pToVirus - The pointer to the virus file.
- size - The file's size.
- Output: 1 if there is a virus.
- 0 if the file is clean.
- */
- int fastScan(FILE* pToFile, int size)
- {
- float heuristic;
- int i;
- int ch;
- char* virusSign, *pTemp = NULL;
- virusSign = (char*)malloc(sizeof(char) + 1); // +1 for the NULL
- if (!virusSign)
- {
- printf("Unsuccessful malloc!\n");
- return 1;
- }
- heuristic = (float)(size * 0.2); // Getting 20% of the file size.
- fseek(pToFile, 0, SEEK_SET); // To put the seek on the file beginning.
- for (i = 0; i < (int)heuristic; i++)
- {
- ch = fgetc(pToFile);
- printf("%c", ch);
- pTemp = realloc(virusSign, sizeof(char)* i + 1);
- if (!pTemp)
- {
- printf("Unsuccessful allocation!\n");
- return 1;
- }
- *(pTemp + i) = (char)ch;
- virusSign = pTemp;
- }
- printf("\n\n\n");
- for (i = 0; i < strlen(virusSign); i++)
- {
- printf("%c", *(virusSign + i));
- }
- printf("\n");
- free(virusSign);
- return 0;
- }
- /**
- This is the main function of the program, which is responsible
- to both start and end the running of the program.
- input: None.
- output: The program returns 0 upon successful completion of its running (windows convention).
- */
- int main(void)
- {
- int size;
- FILE* pToFile = fopen("C:\\Users\\User\\Desktop\\Mid Project\\Log.txt", "rb");
- if (!pToFile) // Verifying if the file didn't opened successfuly.
- {
- printf("An error occured opening the file.\n");
- return 1;
- }
- size = fileSize(pToFile); // Getting the file's size
- printf("File Size: %d.\n", size);
- if (choseScan() == 1)
- {
- fastScan(pToFile, size);
- }
- else
- {
- // regularScan();
- }
- fclose(pToFile);
- system("PAUSE");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement