Advertisement
Guest User

Untitled

a guest
May 28th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. /*********************
  2. * Name: Yair Benamou.*
  3. * Lesson: 8. *
  4. * Ex: Mid Project *
  5. **********************/
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<string.h>
  10. #include<conio.h>
  11. #include<malloc.h>
  12.  
  13.  
  14.  
  15.  
  16. // No Prototypes
  17. int fileSize(FILE* pToFile);
  18.  
  19.  
  20.  
  21. /**
  22. The function 'fileLen' get a file and count how much characters he have.
  23.  
  24. Input: pToFile - Getting the pointer to the file.
  25.  
  26. Output: numCharacters - The counter.
  27. */
  28. int fileSize(FILE* pToFile)
  29. {
  30. int count = 0;
  31. while (fgetc(pToFile) != EOF) // Counting the chars
  32. {
  33. count++;
  34. }
  35. return (count);
  36. }
  37.  
  38.  
  39.  
  40. /**
  41. The function 'choseScan' ask the user which scan he wants to do.
  42.  
  43. Inputs: None.
  44.  
  45. Outputs: The user's choice.
  46. */
  47. int choseScan(void)
  48. {
  49. int choice;
  50.  
  51. printf("\nWhich scan you want to do?\n1) FAST SCAN.\n2) REGULAR SCAN.\n");
  52. scanf("%d", &choice);
  53.  
  54. return(choice);
  55. }
  56.  
  57.  
  58.  
  59. /**
  60. The function 'fastScan' gets a file and his size. The function will scan the file
  61. on the fast way: 20% of the file beginning, and 20% from the end.
  62.  
  63. Input: pToFile - The file to scan
  64. pToVirus - The pointer to the virus file.
  65. size - The file's size.
  66.  
  67. Output: 1 if there is a virus.
  68. 0 if the file is clean.
  69. */
  70. int fastScan(FILE* pToFile, int size)
  71. {
  72. float heuristic;
  73. int i;
  74. int ch;
  75. char* virusSign, *pTemp = NULL;
  76.  
  77.  
  78. virusSign = (char*)malloc(sizeof(char) + 1); // +1 for the NULL
  79. if (!virusSign)
  80. {
  81. printf("Unsuccessful malloc!\n");
  82. return 1;
  83. }
  84.  
  85. heuristic = (float)(size * 0.2); // Getting 20% of the file size.
  86. fseek(pToFile, 0, SEEK_SET); // To put the seek on the file beginning.
  87.  
  88. for (i = 0; i < (int)heuristic; i++)
  89. {
  90. ch = fgetc(pToFile);
  91. printf("%c", ch);
  92.  
  93. pTemp = realloc(virusSign, sizeof(char)* i + 1);
  94. if (!pTemp)
  95. {
  96. printf("Unsuccessful allocation!\n");
  97. return 1;
  98. }
  99.  
  100. *(pTemp + i) = (char)ch;
  101. virusSign = pTemp;
  102.  
  103. }
  104.  
  105. printf("\n\n\n");
  106.  
  107. for (i = 0; i < strlen(virusSign); i++)
  108. {
  109. printf("%c", *(virusSign + i));
  110. }
  111.  
  112. printf("\n");
  113.  
  114. free(virusSign);
  115.  
  116. return 0;
  117. }
  118.  
  119.  
  120.  
  121. /**
  122. This is the main function of the program, which is responsible
  123. to both start and end the running of the program.
  124. input: None.
  125. output: The program returns 0 upon successful completion of its running (windows convention).
  126. */
  127. int main(void)
  128. {
  129. int size;
  130. FILE* pToFile = fopen("C:\\Users\\User\\Desktop\\Mid Project\\Log.txt", "rb");
  131.  
  132. if (!pToFile) // Verifying if the file didn't opened successfuly.
  133. {
  134. printf("An error occured opening the file.\n");
  135. return 1;
  136. }
  137.  
  138. size = fileSize(pToFile); // Getting the file's size
  139. printf("File Size: %d.\n", size);
  140.  
  141. if (choseScan() == 1)
  142. {
  143. fastScan(pToFile, size);
  144. }
  145. else
  146. {
  147. // regularScan();
  148. }
  149.  
  150. fclose(pToFile);
  151. system("PAUSE");
  152. return (0);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement