Advertisement
Guest User

Untitled

a guest
May 24th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <sys/types.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. int searchVirus(char* virus, char* fileName);
  7. int main(int argc, char** argv){
  8. DIR *dp;
  9. char directory[100];
  10. struct dirent *ep;
  11. int check;
  12. FILE * resultFile;
  13. char result1[] = " not infected";
  14. char result2[] = " INFECTED!";
  15. resultFile = fopen("AntiVirusLog.txt", "wb");
  16. printf("please write the name of the directory you want to search the files in\n");
  17. scanf("%s", directory);
  18. dp = opendir(directory);
  19. if (dp != NULL)
  20. {
  21. while (ep = readdir(dp))
  22. {
  23. check=searchVirus(argv[1],ep->d_name);
  24. printf("%d", check);
  25. if (check == 1)
  26. {
  27. int ret_code = fwrite(strcat(ep->d_name, result2), sizeof(char), strlen(result2), resultFile);
  28. if (ret_code != strlen(result2))
  29. {
  30. printf("there was a problem");
  31. }
  32. }
  33. if (check == 2)
  34. {
  35. int ret_code = fwrite(strcat(ep->d_name, result1), sizeof(char), strlen(result2), resultFile);
  36. if (ret_code != strlen(result2))
  37. {
  38. printf("there was a problem");
  39. }
  40. }
  41. }
  42.  
  43. (void)closedir(dp);
  44. }
  45. else
  46. {
  47. perror("Couldn't open the directory");
  48. }
  49. fclose(resultFile);
  50. system("PAUSE");
  51. return (0);
  52. }
  53.  
  54. int searchVirus(char* virus, char* fileName)/*if returns 0 something was wrong, if returns 1 the file
  55. is with a virus if returns 2 the file is okay*/
  56. {
  57. FILE * virusFile;
  58. FILE * checkFile;
  59. int ch;
  60. char firstByte[1];//the first byte of the virus
  61. int length = 0;//what is the length of the virus
  62. int i;
  63. virusFile = fopen(virus, "rb");
  64. if (virusFile == NULL)
  65. {
  66. printf("Error opening first file");
  67. return 0;
  68. }
  69. checkFile = fopen(fileName, "rb");
  70. if (virusFile == NULL)
  71. {
  72. fclose(virusFile);
  73. printf("Error opening second file");
  74. return 0;
  75. }
  76. while ((ch = fgetc(virusFile)) != EOF)
  77. {
  78. length++;
  79. }
  80. char* strCheck = (char*)malloc(sizeof(char)*(length + 1));
  81. char* virusBinary = (char*)malloc(sizeof(char)*(length + 1));
  82. size_t ret_code1 = fread(virusBinary, sizeof (char), length, virusFile);
  83. printf("%d", ret_code1);
  84. if (ret_code1 == length)
  85. {
  86. size_t ret_code2 = fread(firstByte, 1, 1, virusFile);
  87. if (ret_code2 == 1)
  88. {
  89. while ((ch = fgetc(checkFile)) != EOF)
  90. {
  91. if (ch == firstByte)
  92. {
  93. i = 0;
  94. while (i < length && ch != firstByte)
  95. {
  96. strCheck[i] = ch;
  97. ch = fgetc(checkFile);
  98. i++;
  99. }
  100. }
  101. if (strcmp(strCheck, virusBinary) == 0)
  102. {
  103. return 1;
  104. }
  105. fseek(checkFile, (-1), SEEK_CUR);
  106. }
  107. return 2;
  108. }
  109. else
  110. {
  111. return 0;
  112. }
  113. }
  114. else
  115. {
  116. return 3;
  117. }
  118. free(strCheck);
  119. free(virusBinary);
  120. fclose(virusFile);
  121. fclose(checkFile);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement