diff options
author | Frank Ch. Eigler <fche@elastic.org> | 2008-08-09 12:09:01 -0400 |
---|---|---|
committer | Frank Ch. Eigler <fche@elastic.org> | 2008-08-09 12:09:01 -0400 |
commit | d9736de16f58966ff0fd7f9e7391d6beba8d7366 (patch) | |
tree | e795e54ac0537d30bcfd2d54e724eaac08ff1c14 /util.cxx | |
parent | 12a7f85b2dd19fbfa34c81f6cba15015a8a2723d (diff) | |
download | systemtap-steved-d9736de16f58966ff0fd7f9e7391d6beba8d7366.tar.gz systemtap-steved-d9736de16f58966ff0fd7f9e7391d6beba8d7366.tar.xz systemtap-steved-d9736de16f58966ff0fd7f9e7391d6beba8d7366.zip |
prep find_executable() for use by process() probes
Diffstat (limited to 'util.cxx')
-rw-r--r-- | util.cxx | 76 |
1 files changed, 42 insertions, 34 deletions
@@ -155,49 +155,57 @@ tokenize(const string& str, vector<string>& tokens, // Find an executable by name in $PATH. -bool -find_executable(const char *name, string& retpath) +string find_executable(const string& name) { - const char *p; - string path; - vector<string> dirs; - struct stat st1, st2; + string retpath; - if (*name == '/') - { - retpath = name; - return true; - } + if (name.size() == 0) + return name; - p = getenv("PATH"); - if (!p) - return false; - path = p; - - // Split PATH up. - tokenize(path, dirs, string(":")); - - // Search the path looking for the first executable of the right name. - for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); i++) + if (name[0] == '/') // already fully qualified + retpath = name; + else // need to search along $PATH { - string fname = *i + "/" + name; - const char *f = fname.c_str(); - - // Look for a normal executable file. - if (access(f, X_OK) == 0 - && lstat(f, &st1) == 0 - && stat(f, &st2) == 0 - && S_ISREG(st2.st_mode)) + char *path = getenv("PATH"); + if (path) { - // Found it! - retpath = fname; - return true; - } + // Split PATH up. + vector<string> dirs; + tokenize(string(path), dirs, string(":")); + + // Search the path looking for the first executable of the right name. + for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); i++) + { + string fname = *i + "/" + name; + const char *f = fname.c_str(); + struct stat st1, st2; + + // Look for a normal executable file. + if (access(f, X_OK) == 0 + && lstat(f, &st1) == 0 + && stat(f, &st2) == 0 + && S_ISREG(st2.st_mode)) + { + retpath = fname; + break; + } + } + } } - return false; + // Canonicalize the path name. + char *cf = canonicalize_file_name (retpath.c_str()); + if (cf) + { + retpath = string(cf); + free (cf); + } + + return retpath; } + + const string cmdstr_quoted(const string& cmd) { // original cmd : substr1 |