src/SystemProcess.h
branchv_0
changeset 28 9172bd97ae99
parent 27 532953173cd5
--- a/src/SystemProcess.h	Sun Nov 10 22:55:42 2019 +0100
+++ b/src/SystemProcess.h	Mon Nov 11 14:42:13 2019 +0100
@@ -39,6 +39,7 @@
 	 * the command + its arguments
 	 */
 	std::vector<std::string> commandLine;
+	std::vector<std::string> environment;
 	int nullFile = -1;
 
 	/**
@@ -87,7 +88,7 @@
 
 public:
 
-	SystemProcess(std::vector<std::string> commandLine) : commandLine(commandLine) {
+	SystemProcess(const std::vector<std::string>& commandLine, const std::vector<std::string>& environment = {}) : commandLine(commandLine), environment(environment) {
 		nullFile = open("/dev/null", O_RDWR);
 	}
 
@@ -99,10 +100,12 @@
 
 		std::stringstream result;
 
+		// FIXME: different kinds of exception or return the exit code (now it enters infinite loop if the execp() fails)
+		// TODO: rename (not specific to hash)
 		int hashReaderFD;
 		int hashWriterFD;
 		createPipe(hashReaderFD, hashWriterFD);
-		
+
 		__pid_t hashPid = fork();
 
 		if (hashPid < 0) {
@@ -113,6 +116,11 @@
 			redirectFD(nullFile, STDIN_FILENO);
 			redirectFD(nullFile, STDERR_FILENO);
 			redirectFD(hashWriterFD, STDOUT_FILENO);
+			for (int i = 0; i < environment.size();) {
+				std::string name = environment[i++];
+				std::string value = environment[i++];
+				setenv(name.c_str(), value.c_str(), true);
+			}
 			execp(commandLine);
 		} else {
 			// Parent process
@@ -120,9 +128,9 @@
 
 			__gnu_cxx::stdio_filebuf<char> hashReaderBuffer(hashReaderFD, std::ios::in);
 			std::istream hashReader(&hashReaderBuffer);
-			
+
 			for (char ch; hashReader.read(&ch, 1).good();) result.put(ch);
-			
+
 			int waitError;
 			__pid_t waitPID = wait(&waitError);
 			if (waitError) throw relpipe::cli::RelpipeCLIException(L"The child process returned an error exit code.", relpipe::cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exception?