Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SYS_PROG_LAB1.cpp : Defines the entry point for the console application.
- //
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <iostream>
- #include <bitset>
- #include <dirent.h>
- #include <sys/stat.h>
- int dirNameLen;
- const int MAXBUFSIZE = 3000;
- const int MAXSIGNSIZE = 50;
- char sign[MAXSIGNSIZE] = "asdfasdfdfdf";
- char fileName[255];
- char times[40];
- char buf[MAXBUFSIZE + MAXSIGNSIZE - 1];
- int bufSize;
- using namespace std;
- using std::cin;
- using std::cout;
- int find_sing_in_buf() {
- int flag;
- if (strlen(buf) < strlen(sign))
- return 0;
- for (int i = 0;i < strlen(buf) - strlen(sign) + 1; i++) {
- flag = 1;
- for (int j = 0; j < strlen(sign) && flag; j++) {
- if (sign[j] != buf[i + j])
- flag = 0;
- }
- if (flag)
- return 1;
- }
- return 0;
- }
- int find_sign() {
- FILE *p;
- p = fopen(fileName, "rb");
- if (p == 0)
- return 0;
- int size;
- size = fread(buf, 1, bufSize, p);
- buf[size] = '\0';
- if (find_sing_in_buf())
- return 1;
- if (size < bufSize)
- return 0;
- for (int i = 0;i < strlen(sign) - 1;i++)
- buf[i] = buf[i + bufSize - strlen(sign) + 1];
- while (size = fread(buf+strlen(sign)-1, 1, bufSize, p))
- buf[size+strlen(sign)-1]='\0';
- if (find_sing_in_buf())
- return 1;
- if (size < bufSize)
- return 0;
- for (int i = 0; i < strlen(sign) - 1; i++)
- buf[i] = buf[i + bufSize];
- fclose(p);
- return 0;
- }
- void antiVirus(char* path) {
- DIR* ob = opendir(path);
- dirent* info;
- struct stat buf;
- // cout<<"Scanning directory "<<path<<endl;
- for (info=readdir(ob); info; info=readdir(ob)) {
- lstat(info->d_name,&buf);
- mode_t x=buf.st_mode;
- std::bitset<16> tmp(x);
- if ((strcmp(info->d_name,".")==0) || (strcmp(info->d_name,"..")==0))
- continue;
- if (info->d_type==DT_DIR) {
- //if (S_ISDIR(x))
- strcat(path,info->d_name);
- strcat(path,"/");
- antiVirus(path);
- //cout<<"Exiting "<<path<<endl;
- *(strrchr(path,'/'))='\0';
- *(strrchr(path,'/')+1)='\0';
- }
- }
- if (info->d_type==DT_REG) {
- //if (S_ISREG(x))
- strcat(path,info->d_name);
- // cout<<"Found file "<<info->d_name<<"; d_type is "<<int(info->d_type);
- // cout<<path<<endl;
- strcpy(fileName,path);
- fileName[strlen(path)]='\0';
- // cout<<fileName<<endl;
- if(find_sign()) {
- cout<<fileName<<endl;
- *(strrchr(path,'/')+1)='\0';
- }
- }
- closedir(ob);
- }
- int main(int argc, char **argv){
- bufSize = 32;
- char * dir=new char[255];
- // strcpy(dir,"/home/pirogki/");
- strcpy(dir,argv[1]);
- dir[strlen(argv[1])]='\0';
- strcpy(sign,argv[2]);
- sign[strlen(argv[2])]='\0';
- antiVirus(dir);
- delete dir;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment