helpers: add a function to find an executable in $PATH

Origin commit data
------------------
Branch: ni/coolstream
Commit: e37e425674
Author: Stefan Seyfried <seife@tuxbox-git.slipkontur.de>
Date: 2014-01-26 (Sun, 26 Jan 2014)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
Stefan Seyfried
2014-01-26 16:09:48 +01:00
committed by Thilo Graf
parent 3cd295aa3e
commit 40f236846a
2 changed files with 31 additions and 0 deletions

View File

@@ -290,6 +290,35 @@ bool get_mem_usage(unsigned long &kbtotal, unsigned long &kbfree)
return true;
}
std::string find_executable(const char *name)
{
struct stat s;
char *path = getenv("PATH");
char *p, *n;
if (! path)
path = strdupa("/bin:/usr/bin:/sbin:/usr/sbin");
if (name[0] == '/') { /* full path given */
if (!access(name, X_OK) && !stat(name, &s) && S_ISREG(s.st_mode))
return std::string(name);
return "";
}
p = path;
while (p) {
n = strchr(p, ':');
if (n)
*n++ = '\0';
if (*p != '\0') {
std::string tmp = std::string(p) + "/" + std::string(name);
const char *f = tmp.c_str();
if (!access(f, X_OK) && !stat(f, &s) && S_ISREG(s.st_mode))
return tmp;
}
p = n;
}
return "";
}
std::string _getPathName(std::string &path, std::string sep)
{
size_t pos = path.find_last_of(sep);