diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | hash.cxx | 16 | ||||
-rw-r--r-- | util.cxx | 76 | ||||
-rw-r--r-- | util.h | 2 |
4 files changed, 55 insertions, 44 deletions
@@ -1,3 +1,8 @@ +2008-08-09 Frank Ch. Eigler <fche@elastic.org> + + * util.cxx (find_executable): Reorganize, simplify, canonicalize. + * util.h, hash.cxx: Corresponding changes. + 2008-08-07 Frank Ch. Eigler <fche@elastic.org> * tapsetes.cxx @@ -126,16 +126,14 @@ find_hash (systemtap_session& s, const string& script) h.add(s.runtime_path); // Hash compiler path, size, and mtime. We're just going to assume - // we'll be using gcc, which should be correct most of the time. - string gcc_path; - if (find_executable("gcc", gcc_path)) + // we'll be using gcc. XXX: getting kbuild to spit out out would be + // better. + string gcc_path = find_executable ("gcc"); + if (stat(gcc_path.c_str(), &st) == 0) { - if (stat(gcc_path.c_str(), &st) == 0) - { - h.add(gcc_path); - h.add(st.st_size); - h.add(st.st_mtime); - } + h.add(gcc_path); + h.add(st.st_size); + h.add(st.st_mtime); } // Hash the systemtap size and mtime. We could use VERSION/DATE, @@ -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 @@ -10,7 +10,7 @@ int copy_file(const char *src, const char *dest); int create_dir(const char *dir); void tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters); -bool find_executable(const char *name, std::string& retpath); +std::string find_executable(const std::string& name); const std::string cmdstr_quoted(const std::string& cmd); |