prestige

KolyaLab5

Jan 15th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 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.     if (info->d_type==DT_REG) {
  85.     //if (S_ISREG(x))
  86.         strcat(path,info->d_name);
  87.     // cout<<"Found file "<<info->d_name<<"; d_type is "<<int(info->d_type);
  88.     // cout<<path<<endl;
  89.         strcpy(fileName,path);
  90.         fileName[strlen(path)]='\0';
  91.     // cout<<fileName<<endl;
  92.         if(find_sign())
  93.             cout<<fileName<<endl;
  94.         *(strrchr(path,'/')+1)='\0';
  95.         }
  96.     }
  97.     closedir(ob);
  98. }
  99.  
  100.  
  101.  
  102. int main(int argc, char **argv){
  103.     bufSize = 32;
  104.     char * dir=new char[255];
  105.     // strcpy(dir,"/home/pirogki/");
  106.     strcpy(dir,argv[1]);
  107.     dir[strlen(argv[1])]='\0';
  108.     strcpy(sign,argv[2]);
  109.     sign[strlen(argv[2])]='\0';
  110.  
  111.     antiVirus(dir);
  112.     delete dir;
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment