prestige

KolyaLab5(2)

Jan 15th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. // SYS_PROG_LAB1.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <iostream>
  7. #include <bitset>
  8. #include <dirent.h>
  9. #include <sys/stat.h>
  10. int dirNameLen;
  11. const int MAXBUFSIZE = 3000;
  12. const int MAXSIGNSIZE = 50;
  13. char sign[MAXSIGNSIZE] = "asdfasdfdfdf";
  14. char fileName[255];
  15. char times[40];
  16. char buf[MAXBUFSIZE + MAXSIGNSIZE - 1];
  17. int bufSize;
  18. using namespace std;
  19. using std::cin;
  20. using std::cout;
  21.  
  22. int find_sing_in_buf() {
  23. int flag;
  24. if (strlen(buf) < strlen(sign))
  25. return 0;
  26. for (int i = 0;i < strlen(buf) - strlen(sign) + 1; i++) {
  27. flag = 1;
  28. for (int j = 0; j < strlen(sign) && flag; j++) {
  29. if (sign[j] != buf[i + j])
  30. flag = 0;
  31. }
  32. if (flag)
  33. return 1;
  34. }
  35. return 0;
  36. }
  37.  
  38. int find_sign() {
  39. FILE *p;
  40. p = fopen(fileName, "rb");
  41. if (p == 0)
  42. return 0;
  43. int size;
  44. size = fread(buf, 1, bufSize, p);
  45. buf[size] = '\0';
  46. if (find_sing_in_buf())
  47. return 1;
  48. if (size < bufSize)
  49. return 0;
  50. for (int i = 0;i < strlen(sign) - 1;i++)
  51. buf[i] = buf[i + bufSize - strlen(sign) + 1];
  52. while (size = fread(buf+strlen(sign)-1, 1, bufSize, p))
  53. buf[size+strlen(sign)-1]='\0';
  54. if (find_sing_in_buf())
  55. return 1;
  56. if (size < bufSize)
  57. return 0;
  58. for (int i = 0; i < strlen(sign) - 1; i++)
  59. buf[i] = buf[i + bufSize];
  60. fclose(p);
  61. return 0;
  62. }
  63.  
  64. void antiVirus(char* path) {
  65. DIR* ob = opendir(path);
  66. dirent* info;
  67. struct stat buf;
  68. // cout<<"Scanning directory "<<path<<endl;
  69. for (info=readdir(ob); info; info=readdir(ob)) {
  70. lstat(info->d_name,&buf);
  71. mode_t x=buf.st_mode;
  72. std::bitset<16> tmp(x);
  73. if ((strcmp(info->d_name,".")==0) || (strcmp(info->d_name,"..")==0))
  74. continue;
  75. if (info->d_type==DT_DIR) {
  76. //if (S_ISDIR(x))
  77. strcat(path,info->d_name);
  78. strcat(path,"/");
  79. antiVirus(path);
  80. //cout<<"Exiting "<<path<<endl;
  81. *(strrchr(path,'/'))='\0';
  82. *(strrchr(path,'/')+1)='\0';
  83. }
  84. }
  85. if (info->d_type==DT_REG) {
  86. //if (S_ISREG(x))
  87. strcat(path,info->d_name);
  88. // cout<<"Found file "<<info->d_name<<"; d_type is "<<int(info->d_type);
  89. // cout<<path<<endl;
  90. strcpy(fileName,path);
  91. fileName[strlen(path)]='\0';
  92. // cout<<fileName<<endl;
  93. if(find_sign()) {
  94. cout<<fileName<<endl;
  95. *(strrchr(path,'/')+1)='\0';
  96. }
  97. }
  98. closedir(ob);
  99. }
  100.  
  101.  
  102.  
  103. int main(int argc, char **argv){
  104. bufSize = 32;
  105. char * dir=new char[255];
  106. // strcpy(dir,"/home/pirogki/");
  107. strcpy(dir,argv[1]);
  108. dir[strlen(argv[1])]='\0';
  109. strcpy(sign,argv[2]);
  110. sign[strlen(argv[2])]='\0';
  111.  
  112. antiVirus(dir);
  113. delete dir;
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment