* Find the executable with the given name. Searches the given path and then * the system search path. Returns the full path to the executable if it is * found. Otherwise, the empty string is returned. */
| 2949 | * found. Otherwise, the empty string is returned. |
| 2950 | */ |
| 2951 | std::string SystemTools::FindProgram(std::string const& name, |
| 2952 | std::vector<std::string> const& userPaths, |
| 2953 | bool no_system_path) |
| 2954 | { |
| 2955 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 2956 | std::vector<std::string> extensions; |
| 2957 | // check to see if the name already has a .xxx at |
| 2958 | // the end of it |
| 2959 | // on windows try .com then .exe |
| 2960 | if (name.size() <= 3 || name[name.size() - 4] != '.') { |
| 2961 | extensions.emplace_back(".com"); |
| 2962 | extensions.emplace_back(".exe"); |
| 2963 | |
| 2964 | // first try with extensions if the os supports them |
| 2965 | for (std::string const& ext : extensions) { |
| 2966 | std::string tryPath = name; |
| 2967 | tryPath += ext; |
| 2968 | if (SystemTools::FileIsExecutable(tryPath)) { |
| 2969 | return SystemTools::CollapseFullPath(tryPath); |
| 2970 | } |
| 2971 | } |
| 2972 | } |
| 2973 | #endif |
| 2974 | |
| 2975 | // now try just the name |
| 2976 | if (SystemTools::FileIsExecutable(name)) { |
| 2977 | return SystemTools::CollapseFullPath(name); |
| 2978 | } |
| 2979 | // now construct the path |
| 2980 | std::vector<std::string> path; |
| 2981 | // Add the system search path to our path. |
| 2982 | if (!no_system_path) { |
| 2983 | SystemTools::GetPath(path); |
| 2984 | } |
| 2985 | // now add the additional paths |
| 2986 | path.reserve(path.size() + userPaths.size()); |
| 2987 | path.insert(path.end(), userPaths.begin(), userPaths.end()); |
| 2988 | // Add a trailing slash to all paths to aid the search process. |
| 2989 | for (std::string& p : path) { |
| 2990 | if (p.empty() || p.back() != '/') { |
| 2991 | p += '/'; |
| 2992 | } |
| 2993 | } |
| 2994 | // Try each path |
| 2995 | for (std::string& p : path) { |
| 2996 | #ifdef _WIN32 |
| 2997 | // Remove double quotes from the path on windows |
| 2998 | SystemTools::ReplaceString(p, "\"", ""); |
| 2999 | #endif |
| 3000 | #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) |
| 3001 | // first try with extensions |
| 3002 | for (std::string const& ext : extensions) { |
| 3003 | std::string tryPath = p; |
| 3004 | tryPath += name; |
| 3005 | tryPath += ext; |
| 3006 | if (SystemTools::FileIsExecutable(tryPath)) { |
| 3007 | return SystemTools::CollapseFullPath(tryPath); |
| 3008 | } |