From 97794c7889732bf9aa560bec4e7b263ed815fa82 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 7 Oct 2019 10:46:45 +0200 Subject: Add ability to specify custom path list in process::path_search() --- libbutl/process.cxx | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'libbutl/process.cxx') diff --git a/libbutl/process.cxx b/libbutl/process.cxx index b82af3c..bc05c5c 100644 --- a/libbutl/process.cxx +++ b/libbutl/process.cxx @@ -183,12 +183,13 @@ namespace butl // process // static process_path - path_search (const char*, const dir_path&, bool); + path_search (const char*, const dir_path&, bool, const char*); process_path process:: - path_search (const char* f, bool init, const dir_path& fb, bool po) + path_search (const char* f, bool init, + const dir_path& fb, bool po, const char* ps) { - process_path r (try_path_search (f, init, fb, po)); + process_path r (try_path_search (f, init, fb, po, ps)); if (r.empty ()) throw process_error (ENOENT); @@ -197,9 +198,10 @@ namespace butl } process_path process:: - try_path_search (const char* f, bool init, const dir_path& fb, bool po) + try_path_search (const char* f, bool init, + const dir_path& fb, bool po, const char* ps) { - process_path r (butl::path_search (f, fb, po)); + process_path r (butl::path_search (f, fb, po, ps)); if (!init && !r.empty ()) { @@ -250,7 +252,7 @@ namespace butl #ifndef _WIN32 static process_path - path_search (const char* f, const dir_path& fb, bool) + path_search (const char* f, const dir_path& fb, bool, const char* paths) { // Note that there is a similar version for Win32. @@ -326,8 +328,11 @@ namespace butl // 2. We do not continue searching on EACCES from exec(). // 3. We do not execute via default shell on ENOEXEC from exec(). // - optional p (getenv ("PATH")); - for (const char* b (p ? p->c_str () : nullptr), *e; + optional paths_env; + if (paths == nullptr && (paths_env = getenv ("PATH"))) + paths = paths_env->c_str (); + + for (const char* b (paths), *e; b != nullptr; b = (e != nullptr ? e + 1 : e)) { @@ -930,7 +935,7 @@ namespace butl #else // _WIN32 static process_path - path_search (const char* f, const dir_path& fb, bool po) + path_search (const char* f, const dir_path& fb, bool po, const char* paths) { // Note that there is a similar version for Win32. @@ -1104,8 +1109,11 @@ namespace butl // Now search in PATH. Recall is unchanged. // - optional p (getenv ("PATH")); - for (const char* b (p ? p->c_str () : nullptr), *e; + optional paths_env; + if (paths == nullptr && (paths_env = getenv ("PATH"))) + paths = paths_env->c_str (); + + for (const char* b (paths), *e; b != nullptr; b = (e != nullptr ? e + 1 : e)) { -- cgit v1.1