From 27fbdc6a12b6a4d9e24101add2d58951c35eb837 Mon Sep 17 00:00:00 2001 From: dockes Date: Fri, 23 Jan 2009 09:27:33 +0000 Subject: [PATCH] accept additional path argument to execmd::which --- src/utils/execmd.cpp | 14 ++++++++++---- src/utils/execmd.h | 9 +++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/utils/execmd.cpp b/src/utils/execmd.cpp index dd889053..253d4c7a 100644 --- a/src/utils/execmd.cpp +++ b/src/utils/execmd.cpp @@ -68,22 +68,28 @@ exec_is_there(const char *candidate) return false; } -bool ExecCmd::which(const string& cmd, string& path) +bool ExecCmd::which(const string& cmd, string& exepath, const char* path) { if (cmd.empty()) return false; if (cmd[0] == '/') { if (exec_is_there(cmd.c_str())) { - path = cmd; + exepath = cmd; return true; } else { return false; } } - const char *pp = getenv("PATH"); + const char *pp; + if (path) { + pp = path; + } else { + pp = getenv("PATH"); + } if (pp == 0) return false; + list pels; stringToTokens(pp, pels, ":"); for (list::iterator it = pels.begin(); it != pels.end(); it++) { @@ -91,7 +97,7 @@ bool ExecCmd::which(const string& cmd, string& path) *it = "."; string candidate = (it->empty() ? string(".") : *it) + "/" + cmd; if (exec_is_there(candidate.c_str())) { - path = candidate; + exepath = candidate; return true; } } diff --git a/src/utils/execmd.h b/src/utils/execmd.h index 24fe27f7..1e9d71cd 100644 --- a/src/utils/execmd.h +++ b/src/utils/execmd.h @@ -125,9 +125,14 @@ class ExecCmd { /** * Utility routine: check if/where a command is found according to the - * current PATH + * current PATH (or the specified one + * @param cmd command name + * @param exe on return, executable path name if found + * @param path exec seach path to use instead of getenv(PATH) + * @return true if found */ - static bool which(const string& cmd, string& path); + static bool which(const string& cmd, string& exepath, + const char* path = 0); private: vector m_env;