accept additional path argument to execmd::which

This commit is contained in:
dockes 2009-01-23 09:27:33 +00:00
parent 631fadc7cf
commit 27fbdc6a12
2 changed files with 17 additions and 6 deletions

View File

@ -68,22 +68,28 @@ exec_is_there(const char *candidate)
return false; return false;
} }
bool ExecCmd::which(const string& cmd, string& path) bool ExecCmd::which(const string& cmd, string& exepath, const char* path)
{ {
if (cmd.empty()) if (cmd.empty())
return false; return false;
if (cmd[0] == '/') { if (cmd[0] == '/') {
if (exec_is_there(cmd.c_str())) { if (exec_is_there(cmd.c_str())) {
path = cmd; exepath = cmd;
return true; return true;
} else { } else {
return false; return false;
} }
} }
const char *pp = getenv("PATH"); const char *pp;
if (path) {
pp = path;
} else {
pp = getenv("PATH");
}
if (pp == 0) if (pp == 0)
return false; return false;
list<string> pels; list<string> pels;
stringToTokens(pp, pels, ":"); stringToTokens(pp, pels, ":");
for (list<string>::iterator it = pels.begin(); it != pels.end(); it++) { for (list<string>::iterator it = pels.begin(); it != pels.end(); it++) {
@ -91,7 +97,7 @@ bool ExecCmd::which(const string& cmd, string& path)
*it = "."; *it = ".";
string candidate = (it->empty() ? string(".") : *it) + "/" + cmd; string candidate = (it->empty() ? string(".") : *it) + "/" + cmd;
if (exec_is_there(candidate.c_str())) { if (exec_is_there(candidate.c_str())) {
path = candidate; exepath = candidate;
return true; return true;
} }
} }

View File

@ -125,9 +125,14 @@ class ExecCmd {
/** /**
* Utility routine: check if/where a command is found according to the * 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: private:
vector<string> m_env; vector<string> m_env;