diff options
-rw-r--r-- | paludis/util/process.cc | 15 | ||||
-rw-r--r-- | paludis/util/process.hh | 1 | ||||
-rw-r--r-- | paludis/util/process_TEST.cc | 16 |
3 files changed, 32 insertions, 0 deletions
diff --git a/paludis/util/process.cc b/paludis/util/process.cc index b6d83f3..1458124 100644 --- a/paludis/util/process.cc +++ b/paludis/util/process.cc @@ -26,6 +26,8 @@ #include <functional> #include <algorithm> #include <vector> +#include <map> +#include <cstdlib> #include <unistd.h> #include <sys/types.h> @@ -203,6 +205,8 @@ namespace paludis std::ostream * capture_stdout; std::ostream * capture_stderr; + std::map<std::string, std::string> setenvs; + Imp(ProcessCommand && c) : command(std::move(c)), need_thread(false), @@ -262,6 +266,10 @@ Process::run() throw ProcessError("dup2() failed"); } + for (auto m(_imp->setenvs.begin()), m_end(_imp->setenvs.end()) ; + m != m_end ; ++m) + ::setenv(m->first.c_str(), m->second.c_str(), 1); + _imp->command.exec(); } catch (const ProcessError & e) @@ -294,6 +302,13 @@ Process::capture_stderr(std::ostream & s) return *this; } +Process & +Process::setenv(const std::string & a, const std::string & b) +{ + _imp->setenvs.insert(std::make_pair(a, b)).first->second = b; + return *this; +} + namespace paludis { template <> diff --git a/paludis/util/process.hh b/paludis/util/process.hh index b9515f8..b6699f6 100644 --- a/paludis/util/process.hh +++ b/paludis/util/process.hh @@ -73,6 +73,7 @@ namespace paludis Process & capture_stdout(std::ostream &); Process & capture_stderr(std::ostream &); + Process & setenv(const std::string &, const std::string &); }; class PALUDIS_VISIBLE RunningProcessHandle : diff --git a/paludis/util/process_TEST.cc b/paludis/util/process_TEST.cc index 9bf0666..9688c07 100644 --- a/paludis/util/process_TEST.cc +++ b/paludis/util/process_TEST.cc @@ -145,5 +145,21 @@ namespace test_cases TEST_CHECK(! std::getline(stdout_stream, s)); } } test_grab_stdout_long; + + struct SetenvTest : TestCase + { + SetenvTest() : TestCase("setenv") { } + + void run() + { + std::stringstream stdout_stream; + Process printenv_process(ProcessCommand({"printenv", "monkey"})); + printenv_process.capture_stdout(stdout_stream); + printenv_process.setenv("monkey", "in space"); + + TEST_CHECK_EQUAL(printenv_process.run().wait(), 0); + TEST_CHECK_EQUAL(stdout_stream.str(), "in space\n"); + } + } test_setenv; } |