81 lines
1.8 KiB
C++
81 lines
1.8 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <cstring>
|
|
#include <unistd.h>
|
|
#include <signal.h>
|
|
#include <string>
|
|
#include <cstring>
|
|
using namespace std;
|
|
|
|
void handler(int signo, siginfo_t *info, void *context)
|
|
{
|
|
if(signo == SIGTERM)
|
|
{
|
|
cout <<"["<< getpid()<<"] "<<"received SIGTERM\n";
|
|
exit(0);
|
|
}
|
|
}
|
|
int main(int argc, char **argv)
|
|
{
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(struct sigaction));
|
|
sa.sa_sigaction = &handler;
|
|
if (sigaction(SIGTERM, &sa, NULL) == -1)
|
|
{
|
|
perror("sigaction");
|
|
return EXIT_FAILURE;
|
|
}
|
|
pid_t pgid = getpgid(getpid());
|
|
//cout << pgid<< endl;
|
|
if(argc != 5)
|
|
{
|
|
cout <<"usage: ./partitioner.out <path-to-file> <pattern> <search-start-position> <search-end-position>\nprovided arguments:\n";
|
|
for(int i = 0; i < argc; i++)
|
|
cout << argv[i] << "\n";
|
|
return -1;
|
|
}
|
|
|
|
char *file_to_search_in = argv[1];
|
|
char *pattern_to_search_for = argv[2];
|
|
int search_start_position = atoi(argv[3]);
|
|
int search_end_position = atoi(argv[4]);
|
|
//TODO
|
|
ifstream inputFile(file_to_search_in);
|
|
//cout << file_to_search_in <<"\n";
|
|
//cout << pattern_to_search_for <<"\n";
|
|
string pattern = pattern_to_search_for;
|
|
//cout << pattern;
|
|
int len = pattern.length();
|
|
//cout << fork() << endl;
|
|
if ( !inputFile.is_open()){
|
|
cerr << "Error opening the file" << endl;
|
|
return 1;
|
|
}
|
|
|
|
//string line;
|
|
//cout << "File Content: " << endl;
|
|
int i=search_start_position;
|
|
char ch;
|
|
string pat="";
|
|
inputFile.seekg(search_start_position);
|
|
while (inputFile.get(ch) and i<=search_end_position){
|
|
i++;
|
|
if(pat.length() < len){
|
|
pat = pat + ch;
|
|
continue;
|
|
}
|
|
pat.erase(0,1);
|
|
pat = pat+ ch;
|
|
//pid_t pid = fork();
|
|
//cout <<pat<<endl;
|
|
if (pat == pattern){
|
|
cout<< "[" << getpid() <<"] found at " << i - len << endl;
|
|
killpg(pgid,SIGTERM);
|
|
return 0;
|
|
//break;
|
|
}
|
|
}
|
|
|
|
cout <<"[" <<getpid() <<"] didn't find\n";
|
|
return 1;
|
|
} |