aboutsummaryrefslogtreecommitdiff
path: root/libbutl/process.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-06-06 14:05:38 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-06-07 14:08:27 +0300
commit7c4f5e6464a7d9a9c48b4d6773fbb348624cc32e (patch)
treeb8b86eba7852bd00191600e0e4696cc5a7c31e42 /libbutl/process.cxx
parenta32378a9aedd61c51166432ecd26a6a5dd405ebb (diff)
Support passing environment variables to child process
Diffstat (limited to 'libbutl/process.cxx')
-rw-r--r--libbutl/process.cxx46
1 files changed, 36 insertions, 10 deletions
diff --git a/libbutl/process.cxx b/libbutl/process.cxx
index 265c775..6f4fd30 100644
--- a/libbutl/process.cxx
+++ b/libbutl/process.cxx
@@ -5,6 +5,7 @@
#include <libbutl/process.hxx>
#ifndef _WIN32
+# include <stdlib.h> // setenv(), unsetenv()
# include <signal.h> // SIG*
# include <unistd.h> // execvp, fork, dup2, pipe, chdir, *_FILENO, getpid
# include <sys/wait.h> // waitpid
@@ -134,10 +135,11 @@ namespace butl
}
process::
- process (const char* cwd,
- const process_path& pp, const char* args[],
- process& in, int out, int err)
- : process (cwd, pp, args, in.in_ofd.get (), out, err)
+ process (const process_path& pp, const char* args[],
+ process& in, int out, int err,
+ const char* cwd,
+ const char* const* envvars)
+ : process (pp, args, in.in_ofd.get (), out, err, cwd, envvars)
{
assert (in.in_ofd.get () != -1); // Should be a pipe.
in.in_ofd.reset (); // Close it on our side.
@@ -264,9 +266,10 @@ namespace butl
}
process::
- process (const char* cwd,
- const process_path& pp, const char* args[],
- int in, int out, int err)
+ process (const process_path& pp, const char* args[],
+ int in, int out, int err,
+ const char* cwd,
+ const char* const* envvars)
{
fdpipe out_fd;
fdpipe in_ofd;
@@ -396,6 +399,23 @@ namespace butl
if (cwd != nullptr && *cwd != '\0' && chdir (cwd) != 0)
fail (true);
+ // Set/unset environment variables if requested.
+ //
+ if (envvars != nullptr)
+ {
+ while (const char* ev = *envvars++)
+ {
+ const char* v (strchr (ev, '='));
+
+ int r (v != nullptr
+ ? setenv (string (ev, v - ev).c_str (), v + 1, 1)
+ : unsetenv (ev));
+
+ if (r == -1)
+ fail (true);
+ }
+ }
+
if (execv (pp.effect_string (), const_cast<char**> (&args[0])) == -1)
fail (true);
}
@@ -919,10 +939,16 @@ namespace butl
static map<string, bool> detect_msys_cache_;
process::
- process (const char* cwd,
- const process_path& pp, const char* args[],
- int in, int out, int err)
+ process (const process_path& pp, const char* args[],
+ int in, int out, int err,
+ const char* cwd,
+ const char* const* envvars)
{
+ // Currently we don't support (un)setting environment variables for the
+ // child process.
+ //
+ assert (envvars == nullptr);
+
// Figure out if this is a batch file since running them requires starting
// cmd.exe and passing the batch file as an argument (see CreateProcess()
// for deails).