* Find an exe file in PATH. */
| 222 | * Find an exe file in PATH. |
| 223 | */ |
| 224 | const char *findBinary(const char *filename, bool exe, bool dot) |
| 225 | { |
| 226 | if (filename[0] == '/' || filename[0] == '.') |
| 227 | return filename; |
| 228 | std::vector<std::string> path; |
| 229 | getPath(exe, path); |
| 230 | if (dot) |
| 231 | path.push_back("."); |
| 232 | for (const auto &dirname: path) |
| 233 | { |
| 234 | std::string pathname_0(dirname); |
| 235 | pathname_0 += '/'; |
| 236 | pathname_0 += filename; |
| 237 | |
| 238 | char *pathname = realpath(pathname_0.c_str(), nullptr); |
| 239 | if (pathname == nullptr) |
| 240 | continue; |
| 241 | struct stat buf; |
| 242 | if (stat(pathname, &buf) == 0 && (buf.st_mode & S_IXOTH) != 0) |
| 243 | return pathname; |
| 244 | free(pathname); |
| 245 | } |
| 246 | |
| 247 | error("failed to find %s file \"%s\" in %s", |
| 248 | (exe? "executable": "library"), filename, (exe? "PATH": "RPATH")); |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Usage. |