mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-25 02:01:15 -03:00
39
src/path.cpp
39
src/path.cpp
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user