From 05a398f967bb030c6dbb9d6458340767325e46f4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 28 Jul 2017 16:25:49 +0200 Subject: Add ability to restrict executable search to PATH only --- libbutl/process.cxx | 16 +++++++++------- libbutl/process.hxx | 26 +++++++++++++++++++------- libbutl/process.ixx | 20 ++++++++++---------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/libbutl/process.cxx b/libbutl/process.cxx index 6f6ea8e..2260213 100644 --- a/libbutl/process.cxx +++ b/libbutl/process.cxx @@ -72,12 +72,12 @@ namespace butl // process // static process_path - path_search (const char*, const dir_path&); + path_search (const char*, const dir_path&, bool); process_path process:: - path_search (const char* f, bool init, const dir_path& fb) + path_search (const char* f, bool init, const dir_path& fb, bool po) { - process_path r (try_path_search (f, init, fb)); + process_path r (try_path_search (f, init, fb, po)); if (r.empty ()) throw process_error (ENOENT); @@ -86,9 +86,9 @@ namespace butl } process_path process:: - try_path_search (const char* f, bool init, const dir_path& fb) + try_path_search (const char* f, bool init, const dir_path& fb, bool po) { - process_path r (butl::path_search (f, fb)); + process_path r (butl::path_search (f, fb, po)); if (!init && !r.empty ()) { @@ -150,7 +150,7 @@ namespace butl #ifndef _WIN32 static process_path - path_search (const char* f, const dir_path& fb) + path_search (const char* f, const dir_path& fb, bool) { // Note that there is a similar version for Win32. @@ -614,7 +614,7 @@ namespace butl #else // _WIN32 static process_path - path_search (const char* f, const dir_path& fb) + path_search (const char* f, const dir_path& fb, bool po) { // Note that there is a similar version for Win32. @@ -731,6 +731,7 @@ namespace butl // The search order is documented in CreateProcess(). First we look in the // directory of the parent executable. // + if (!po) { char d[_MAX_PATH + 1]; DWORD n (GetModuleFileName (NULL, d, _MAX_PATH + 1)); @@ -778,6 +779,7 @@ namespace butl // The recall path is the same as initial, though it might not be a bad // idea to prepend .\ for clarity. // + if (!po) { const string& d (traits::current_directory ()); diff --git a/libbutl/process.hxx b/libbutl/process.hxx index cd692ca..3e1a990 100644 --- a/libbutl/process.hxx +++ b/libbutl/process.hxx @@ -313,8 +313,14 @@ namespace butl // This, for example, can be used to implement the Windows "search in the // parent executable's directory" semantics across platforms. // + // If path_only is true then only search in the PATH environment variable + // (or in CWD if there is a directory component) ignorting other places + // (like calling process' directory and, gasp, CWD on Windows). + // static process_path - path_search (const char*& args0, const dir_path& fallback = dir_path ()); + path_search (const char*& args0, + const dir_path& fallback = dir_path (), + bool path_only = false); // This version is primarily useful when you want to pre-search the // executable before creating the args[] array. In this case you will @@ -330,25 +336,31 @@ namespace butl // the same). // static process_path - path_search (const char* file, bool init, const dir_path& = dir_path ()); + path_search (const char* file, bool init, + const dir_path& = dir_path (), bool = false); static process_path - path_search (const std::string&, bool, const dir_path& = dir_path ()); + path_search (const std::string&, bool, + const dir_path& = dir_path (), bool = false); static process_path - path_search (const path&, bool, const dir_path& = dir_path ()); + path_search (const path&, bool, + const dir_path& = dir_path (), bool = false); // As above but if not found return empty process_path instead of // throwing. // static process_path - try_path_search (const char*, bool, const dir_path& = dir_path ()); + try_path_search (const char*, bool, + const dir_path& = dir_path (), bool = false); static process_path - try_path_search (const std::string&, bool, const dir_path& = dir_path ()); + try_path_search (const std::string&, bool, + const dir_path& = dir_path (), bool = false); static process_path - try_path_search (const path&, bool, const dir_path& = dir_path ()); + try_path_search (const path&, bool, + const dir_path& = dir_path (), bool = false); // Print process commmand line. If the number of elements is specified, // then it will print the piped multi-process command line, if present. diff --git a/libbutl/process.ixx b/libbutl/process.ixx index 4bdd438..1f1a2f4 100644 --- a/libbutl/process.ixx +++ b/libbutl/process.ixx @@ -90,9 +90,9 @@ namespace butl #endif inline process_path process:: - path_search (const char*& a0, const dir_path& fb) + path_search (const char*& a0, const dir_path& fb, bool po) { - process_path r (path_search (a0, true, fb)); + process_path r (path_search (a0, true, fb, po)); if (!r.recall.empty ()) { @@ -104,27 +104,27 @@ namespace butl } inline process_path process:: - path_search (const std::string& f, bool i, const dir_path& fb) + path_search (const std::string& f, bool i, const dir_path& fb, bool po) { - return path_search (f.c_str (), i, fb); + return path_search (f.c_str (), i, fb, po); } inline process_path process:: - path_search (const path& f, bool i, const dir_path& fb) + path_search (const path& f, bool i, const dir_path& fb, bool po) { - return path_search (f.string ().c_str (), i, fb); + return path_search (f.string ().c_str (), i, fb, po); } inline process_path process:: - try_path_search (const std::string& f, bool i, const dir_path& fb) + try_path_search (const std::string& f, bool i, const dir_path& fb, bool po) { - return try_path_search (f.c_str (), i, fb); + return try_path_search (f.c_str (), i, fb, po); } inline process_path process:: - try_path_search (const path& f, bool i, const dir_path& fb) + try_path_search (const path& f, bool i, const dir_path& fb, bool po) { - return try_path_search (f.string ().c_str (), i, fb); + return try_path_search (f.string ().c_str (), i, fb, po); } inline process:: -- cgit v1.1