implement command -a

Fixes #2778
This commit is contained in:
Kurtis Rader
2017-06-23 15:42:38 -07:00
parent 2d42baac35
commit c31b9f430f
4 changed files with 74 additions and 15 deletions

View File

@@ -25,10 +25,10 @@
static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
const env_var_t &bin_path_var) {
int err = ENOENT;
debug(3, L"path_get_path( '%ls' )", cmd.c_str());
// If the command has a slash, it must be a full path.
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
// looking for a matching command.
if (cmd.find(L'/') != wcstring::npos) {
if (waccess(cmd, X_OK) != 0) {
return false;
@@ -46,6 +46,7 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path,
return false;
}
int err = ENOENT;
wcstring bin_path;
if (!bin_path_var.missing()) {
bin_path = bin_path_var;
@@ -105,6 +106,40 @@ bool path_get_path(const wcstring &cmd, wcstring *out_path) {
return path_get_path_core(cmd, out_path, env_get_string(L"PATH"));
}
wcstring_list_t path_get_paths(const wcstring &cmd) {
debug(3, L"path_get_paths('%ls')", cmd.c_str());
wcstring_list_t paths;
// If the command has a slash, it must be an absolute or relative path and thus we don't bother
// looking for matching commands in the PATH var.
if (cmd.find(L'/') != wcstring::npos) {
struct stat buff;
if (wstat(cmd, &buff)) return paths;
if (!S_ISREG(buff.st_mode)) return paths;
if (waccess(cmd, X_OK)) return paths;
paths.push_back(cmd);
return paths;
}
wcstring env_path = env_get_string(L"PATH");
std::vector<wcstring> pathsv;
tokenize_variable_array(env_path, pathsv);
for (auto path : pathsv) {
if (path.empty()) continue;
append_path_component(path, cmd);
if (waccess(path, X_OK) == 0) {
struct stat buff;
if (wstat(path, &buff) == -1) {
if (errno != EACCES) wperror(L"stat");
continue;
}
if (S_ISREG(buff.st_mode)) paths.push_back(path);
}
}
return paths;
}
bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd,
const env_vars_snapshot_t &env_vars) {
int err = ENOENT;