<commit_before>#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

//adds space before and after all instances of op in text to text
void addSpaces(string &text, const string &op) {
	size_t length = op.length();
	size_t pos;
	pos = text.find(op, 0);	//some inspiration from stackoverflow.com/questions/4034750


	while(pos != string::npos) {
		//pos+1 to go to thing pos was pointing at, but want to move AFTER that
		text.insert(pos, " ");	//one space before
		text.insert(pos+1+length, " "); //and one space after
		pos = text.find(op, pos+1+length);
	}
}


//adds space before and after all instances of op in text to text
void addNumbers(string &text, const string &op) {
	size_t length = op.length();
	size_t pos;
	pos = text.find(op, 0);	//some inspiration from stackoverflow.com/questions/4034750


	while(pos != string::npos) {
		//pos+1 to go to thing pos was pointing at, but want to move AFTER that
		text.insert(pos, " ");	//one space before
		text.insert(pos+1+length, " "); //and one space after
		pos = text.find(op, pos+1+length);
	}
}


/*
//takes in string and deliminator and tokenizes that string into vector<char*>
vector<char*> parser(string text, const char *delim) {
	char *line = new char[text.length()+1];	//strtok requires char*
						//got this from cplusplus.com/reference/string/string/c_str
	strcpy(line, text.c_str());


	char *token = strtok(line, delim);	//can be any character
	vector<char*> words;


	while(token != NULL) {	
		words.push_back(token);
		token = strtok(NULL, delim);
	}
	
	delete[] token;
	
	return words;
}
*/

bool redir0(const vector<char*> &onecommand, size_t i) {
		//close stdin
		if(-1 == close(0)) {
			perror("error with close");
		}

		int fd = open(onecommand.at(i), O_RDONLY);	//open to lowest file desc
		if(-1 == fd) {
			perror("error with open");
			exit(1);
		}
}

bool redirectors(const vector<char*> &onecommand) {
	size_t i = 0;
	//find the first redirector
	//putting everything before it into char* vector along the way
	int redirector = -1;
	vector<char*> newcommand;
	for(i = 0; (i < onecommand.size() - 1) && (redirector < 0); i++) {
		if(strcmp(onecommand.at(i), "<") == 0) {
			redirector = 0;		//input
		} else if(strcmp(onecommand.at(i), ">") == 0) {
			redirector = 1;		//single output
		} else if(strcmp(onecommand.at(i), ">>") == 0) {
			redirector = 2;		//double output
		} else if(strcmp(onecommand.at(i), "|") == 0) {
			redirector = 3;		//pipe
		} else {
			newcommand.push_back(onecommand.at(i));
			cout << "theo" << onecommand.at(i) << endl;
		}
	}

	newcommand.push_back(NULL);
	
	if(redirector < 0) return false;

	int backup = dup(redirector);
	if(-1 == backup) {
		perror("error with dup");
	}
	//it's an input
	if(redirector == 0) {
		if(i < onecommand.size()) {		//check if file is in the vector
			redir0(onecommand, i);
		} else {
			perror("improper use of <");
			return false;
		}
	}

	int forkid = fork();
		if(-1 == forkid) {
			perror("error with fork");
			exit(1);
		} else if(0 == forkid) {
			//the child
			if(-1 == execvp(newcommand.at(0), &newcommand[0])) {
				perror("erro with execvp");
			}
			cout << "before exit execvp" << endl;
			exit(0);
		}
		cout << "in parent" << endl;
		if(-1 == wait(0)) {
			perror("error with wait");
		}

		
		//parent here i guess
		if(-1 == dup2(backup, redirector)) {
			perror("error with dup2");
		}
		if(-1 == close(backup)) {
			perror("error with close backup");
		}
		

	
	return true;
}

/*
vector<char*> redirectionstuff(vector<char*> &onecommand) {
	vector<char*> newcommand;
	for(unsigned int i = 0; i < onecommand.size(); i++) {
		if(strcmp(onecommand.at(i), "<") == 0) {
			if(i > 0 && i+1 > onecommand.size()) {
				//let's do input < first
				if(-1 == close(0)) {
					perror("close");
				}
				int fd = open(blah, O_RDONLY);	//opened to 0 since we closed 0
				if(-1 == fd) {
					perror("error with open");
					exit(1);
				}
				//run some execvp shti
				newcommand.push_back(onecommand.at(i-1));
				while(i < )
		
				//close(0);		//closing what i oepened
			} else {
				cout << "Invalid use of redirection" << endl;
				exit(1);
			}
		}
	}<commit_msg>one exec fcn. closing in child<commit_after>#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/wait.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

//adds space before and after all instances of op in text to text
void addSpaces(string &text, const string &op) {
	size_t length = op.length();
	size_t pos;
	pos = text.find(op, 0);	//some inspiration from stackoverflow.com/questions/4034750


	while(pos != string::npos) {
		//pos+1 to go to thing pos was pointing at, but want to move AFTER that
		text.insert(pos, " ");	//one space before
		text.insert(pos+1+length, " "); //and one space after
		pos = text.find(op, pos+1+length);
	}
}


//adds space before and after all instances of op in text to text
void addNumbers(string &text, const string &op) {
	size_t length = op.length();
	size_t pos;
	pos = text.find(op, 0);	//some inspiration from stackoverflow.com/questions/4034750


	while(pos != string::npos) {
		//pos+1 to go to thing pos was pointing at, but want to move AFTER that
		text.insert(pos, " ");	//one space before
		text.insert(pos+1+length, " "); //and one space after
		pos = text.find(op, pos+1+length);
	}
}


/*
//takes in string and deliminator and tokenizes that string into vector<char*>
vector<char*> parser(string text, const char *delim) {
	char *line = new char[text.length()+1];	//strtok requires char*
						//got this from cplusplus.com/reference/string/string/c_str
	strcpy(line, text.c_str());


	char *token = strtok(line, delim);	//can be any character
	vector<char*> words;


	while(token != NULL) {	
		words.push_back(token);
		token = strtok(NULL, delim);
	}
	
	delete[] token;
	
	return words;
}
*/

bool execinout(const vector<char*> &onecommand, const vector<char*> newcommand, size_t i, int redirector) {
	int fd;
	int forkid = fork();
	if(-1 == forkid) {
		perror("error with fork");
		exit(1);
	} else if(0 == forkid) {
		//the child
		if(redirector == 0) {
			if(-1 == close(0)) {
				perror("error with close0");
			}
			fd = open(onecommand.at(i), O_RDONLY);
			if(-1 == fd) {
				perror("error with open0");
			}
		} else if(redirector == 1) {
			if(-1 == close(1)) {
				perror("error with close1");
			}
			fd = open(onecommand.at(i), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
			if(-1 == fd) {
				perror("error with open1");
			}
		} else if(redirector == 2) {
			if(-1 == close(1)) {
				perror("error with close1");
			}
			fd = open(onecommand.at(i), O_WRONLY|O_CREAT|O_APPEND, S_IRWXU);
			if(-1 == fd) {
				perror("error with open1");
			}
		}

		if(-1 == execvp(newcommand.at(0), &newcommand[0])) {
			perror("erro with execvp");
		}
		cout << "before exit execvp" << endl;
		exit(0);
	}
	if(-1 == wait(0)) {
		perror("error with wait");
	}
		
	//parent here i guess
	return true;

}

/*
bool redir0(const vector<char*> &onecommand, size_t i) {
		//close stdin
		if(-1 == close(0)) {
			perror("error with close0");
		}

		int fd = open(onecommand.at(i), O_RDONLY);	//open to lowest file desc
		if(-1 == fd) {
			perror("error with open0");
			exit(1);
		}
	return true;
}

bool redir1(const vector<char*> &onecommand, size_t i, int redirector) {
	//close stdout
	if(-1 == close(1)) {
		perror("error with close1");
	}
	if(redirector == 1) {
		int fd = open(onecommand.at(i), O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
	} else int fd = open(onecommand.at(i), O_WRONLY|O_CREAT|O_APPEND, S_IRWXU);

	if(-1 == fd) {
		perror("error with open1");
		exit(1);
	}
	return true;
}

bool redir3(const vector<char*> &onecommand, size_t i) {
	int fd[2];
	if(-1 == pipe(fd)) {
		perror(__FILE__ ": " "pipe");
	}
	
	int forkid = fork();
	if(-1 == forkid) {
		perror("error with fork");
	} else if(0 == forkid) {
		//child
		close(1);	//need to error check
		dup(fd[1]);	//need to error check
		execvp();
	}
	//parent
	//fork again
	forkid = fork();
	if(-1 == forkid) {
		perror("error with fork2");
	} else if(0 == forkid) {
		close(0);	//need to error check
		dup(fd[0]);	//need to error check
		execvp();
	}
	return true;
}
*/	


bool redirectors(const vector<char*> &onecommand) {
	size_t i = 0;
	//find the first redirector
	//putting everything before it into char* vector along the way
	int redirector = -1;
	vector<char*> newcommand;
	for(i = 0; (i < onecommand.size()) && (redirector < 0); i++) {
		if(strcmp(onecommand.at(i), "<") == 0) {
			redirector = 0;		//input
		} else if(strcmp(onecommand.at(i), ">") == 0) {
			redirector = 1;		//single output
		} else if(strcmp(onecommand.at(i), ">>") == 0) {
			redirector = 2;		//double output
		} else if(strcmp(onecommand.at(i), "|") == 0) {
			redirector = 3;		//pipe
		} else {
			newcommand.push_back(onecommand.at(i));
		}
	}

	newcommand.push_back(NULL);
	
	//this is supposed to take care of position of redirector
	//no redirector, redirector is last thing, redirector is first thing
	if(redirector < 0 || i >= onecommand.size() || i == 0) return false;
	
	//it's an input
	if(redirector < 3) {
		execinout(onecommand, newcommand, i, redirector);
	} else if(redirector == 3) {
	
	}

	return true;
}


/*
vector<char*> redirectionstuff(vector<char*> &onecommand) {
	vector<char*> newcommand;
	for(unsigned int i = 0; i < onecommand.size(); i++) {
		if(strcmp(onecommand.at(i), "<") == 0) {
			if(i > 0 && i+1 > onecommand.size()) {
				//let's do input < first
				if(-1 == close(0)) {
					perror("close");
				}
				int fd = open(blah, O_RDONLY);	//opened to 0 since we closed 0
				if(-1 == fd) {
					perror("error with open");
					exit(1);
				}
				//run some execvp shti
				newcommand.push_back(onecommand.at(i-1));
				while(i < )
		
				//close(0);		//closing what i oepened
			} else {
				cout << "Invalid use of redirection" << endl;
				exit(1);
			}
		}
	}
{'fraction_non_alphanumeric': 0.1727156486672568, 'fraction_numerical': 0.017409265269991148, 'mean_word_length': 3.283066554338669, 'pattern_counts': {'":': 1, '<': 94, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4365163', 'n_tokens_mistral': 4275, 'n_tokens_neox': 3798, 'n_words': 1031}