mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-31 12:21:19 -03:00
Apply new indentation, brace, and whitespace style
This commit is contained in:
398
path.cpp
398
path.cpp
@@ -25,110 +25,110 @@
|
||||
|
||||
static bool path_get_path_core(const wcstring &cmd, wcstring *out_path, const env_var_t &bin_path_var)
|
||||
{
|
||||
int err = ENOENT;
|
||||
int err = ENOENT;
|
||||
|
||||
debug( 3, L"path_get_path( '%ls' )", cmd.c_str() );
|
||||
debug(3, L"path_get_path( '%ls' )", cmd.c_str());
|
||||
|
||||
/* If the command has a slash, it must be a full path */
|
||||
if (cmd.find(L'/') != wcstring::npos)
|
||||
{
|
||||
if( waccess( cmd, X_OK )==0 )
|
||||
if (cmd.find(L'/') != wcstring::npos)
|
||||
{
|
||||
struct stat buff;
|
||||
if(wstat( cmd, &buff ))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if( S_ISREG(buff.st_mode) )
|
||||
if (waccess(cmd, X_OK)==0)
|
||||
{
|
||||
struct stat buff;
|
||||
if (wstat(cmd, &buff))
|
||||
{
|
||||
if (out_path)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (S_ISREG(buff.st_mode))
|
||||
{
|
||||
if (out_path)
|
||||
out_path->assign(cmd);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = EACCES;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = EACCES;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
struct stat buff;
|
||||
wstat(cmd, &buff);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
struct stat buff;
|
||||
wstat( cmd, &buff );
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
wcstring bin_path;
|
||||
if (! bin_path_var.missing())
|
||||
if (! bin_path_var.missing())
|
||||
{
|
||||
bin_path = bin_path_var;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (contains( PREFIX L"/bin", L"/bin", L"/usr/bin" ))
|
||||
{
|
||||
bin_path = L"/bin" ARRAY_SEP_STR L"/usr/bin";
|
||||
}
|
||||
else
|
||||
{
|
||||
bin_path = L"/bin" ARRAY_SEP_STR L"/usr/bin" ARRAY_SEP_STR PREFIX L"/bin";
|
||||
}
|
||||
}
|
||||
{
|
||||
if (contains(PREFIX L"/bin", L"/bin", L"/usr/bin"))
|
||||
{
|
||||
bin_path = L"/bin" ARRAY_SEP_STR L"/usr/bin";
|
||||
}
|
||||
else
|
||||
{
|
||||
bin_path = L"/bin" ARRAY_SEP_STR L"/usr/bin" ARRAY_SEP_STR PREFIX L"/bin";
|
||||
}
|
||||
}
|
||||
|
||||
wcstring nxt_path;
|
||||
wcstokenizer tokenizer(bin_path, ARRAY_SEP_STR);
|
||||
while (tokenizer.next(nxt_path))
|
||||
{
|
||||
while (tokenizer.next(nxt_path))
|
||||
{
|
||||
if (nxt_path.empty())
|
||||
continue;
|
||||
append_path_component(nxt_path, cmd);
|
||||
if( waccess( nxt_path, X_OK )==0 )
|
||||
{
|
||||
struct stat buff;
|
||||
if( wstat( nxt_path, &buff )==-1 )
|
||||
{
|
||||
if( errno != EACCES )
|
||||
{
|
||||
wperror( L"stat" );
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if( S_ISREG(buff.st_mode) )
|
||||
{
|
||||
if (waccess(nxt_path, X_OK)==0)
|
||||
{
|
||||
struct stat buff;
|
||||
if (wstat(nxt_path, &buff)==-1)
|
||||
{
|
||||
if (errno != EACCES)
|
||||
{
|
||||
wperror(L"stat");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (S_ISREG(buff.st_mode))
|
||||
{
|
||||
if (out_path)
|
||||
out_path->swap(nxt_path);
|
||||
return true;
|
||||
}
|
||||
err = EACCES;
|
||||
return true;
|
||||
}
|
||||
err = EACCES;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
switch( errno )
|
||||
{
|
||||
case ENOENT:
|
||||
case ENAMETOOLONG:
|
||||
case EACCES:
|
||||
case ENOTDIR:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
debug( 1,
|
||||
MISSING_COMMAND_ERR_MSG,
|
||||
nxt_path.c_str() );
|
||||
wperror( L"access" );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (errno)
|
||||
{
|
||||
case ENOENT:
|
||||
case ENAMETOOLONG:
|
||||
case EACCES:
|
||||
case ENOTDIR:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
debug(1,
|
||||
MISSING_COMMAND_ERR_MSG,
|
||||
nxt_path.c_str());
|
||||
wperror(L"access");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
errno = err;
|
||||
return false;
|
||||
errno = err;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool path_get_path(const wcstring &cmd, wcstring *out_path, const env_vars_snapshot_t &vars)
|
||||
@@ -143,36 +143,37 @@ bool path_get_path(const wcstring &cmd, wcstring *out_path)
|
||||
|
||||
bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_var_t &cdpath)
|
||||
{
|
||||
wchar_t *res = 0;
|
||||
int err = ENOENT;
|
||||
wchar_t *res = 0;
|
||||
int err = ENOENT;
|
||||
bool success = false;
|
||||
|
||||
const wchar_t *const dir = dir_str.c_str();
|
||||
if( dir[0] == L'/'|| (wcsncmp( dir, L"./", 2 )==0) )
|
||||
{
|
||||
struct stat buf;
|
||||
if( wstat( dir, &buf ) == 0 )
|
||||
if (dir[0] == L'/'|| (wcsncmp(dir, L"./", 2)==0))
|
||||
{
|
||||
if( S_ISDIR(buf.st_mode) )
|
||||
{
|
||||
result = dir_str;
|
||||
struct stat buf;
|
||||
if (wstat(dir, &buf) == 0)
|
||||
{
|
||||
if (S_ISDIR(buf.st_mode))
|
||||
{
|
||||
result = dir_str;
|
||||
success = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
else
|
||||
{
|
||||
|
||||
wcstring path = L".";
|
||||
|
||||
// Respect CDPATH
|
||||
env_var_t cdpath = env_get_string(L"CDPATH");
|
||||
if (! cdpath.missing_or_empty()) {
|
||||
if (! cdpath.missing_or_empty())
|
||||
{
|
||||
path = cdpath.c_str();
|
||||
}
|
||||
|
||||
@@ -186,44 +187,44 @@ bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env
|
||||
wcstring whole_path = next_path;
|
||||
append_path_component(whole_path, dir);
|
||||
|
||||
struct stat buf;
|
||||
if( wstat( whole_path, &buf ) == 0 )
|
||||
{
|
||||
if( S_ISDIR(buf.st_mode) )
|
||||
{
|
||||
struct stat buf;
|
||||
if (wstat(whole_path, &buf) == 0)
|
||||
{
|
||||
if (S_ISDIR(buf.st_mode))
|
||||
{
|
||||
result = whole_path;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( lwstat( whole_path, &buf ) == 0 )
|
||||
{
|
||||
err = EROTTEN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (lwstat(whole_path, &buf) == 0)
|
||||
{
|
||||
err = EROTTEN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( !success )
|
||||
{
|
||||
errno = err;
|
||||
}
|
||||
if (!success)
|
||||
{
|
||||
errno = err;
|
||||
}
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, const env_vars_snapshot_t &env_vars)
|
||||
{
|
||||
int err = ENOENT;
|
||||
if (dir.empty())
|
||||
return false;
|
||||
int err = ENOENT;
|
||||
if (dir.empty())
|
||||
return false;
|
||||
|
||||
if (wd)
|
||||
{
|
||||
@@ -232,19 +233,24 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, cons
|
||||
}
|
||||
|
||||
wcstring_list_t paths;
|
||||
if (dir.at(0) == L'/') {
|
||||
if (dir.at(0) == L'/')
|
||||
{
|
||||
/* Absolute path */
|
||||
paths.push_back(dir);
|
||||
} else if (string_prefixes_string(L"./", dir) ||
|
||||
string_prefixes_string(L"../", dir) ||
|
||||
dir == L"." || dir == L"..") {
|
||||
}
|
||||
else if (string_prefixes_string(L"./", dir) ||
|
||||
string_prefixes_string(L"../", dir) ||
|
||||
dir == L"." || dir == L"..")
|
||||
{
|
||||
/* Path is relative to the working directory */
|
||||
wcstring path;
|
||||
if (wd)
|
||||
path.append(wd);
|
||||
path.append(dir);
|
||||
paths.push_back(path);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Respect CDPATH
|
||||
env_var_t path = env_vars.get(L"CDPATH");
|
||||
if (path.missing_or_empty())
|
||||
@@ -255,7 +261,8 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, cons
|
||||
while (tokenizer.next(nxt_path))
|
||||
{
|
||||
|
||||
if (nxt_path == L"." && wd != NULL) {
|
||||
if (nxt_path == L"." && wd != NULL)
|
||||
{
|
||||
// nxt_path is just '.', and we have a working directory, so use the wd instead
|
||||
// TODO: if nxt_path starts with ./ we need to replace the . with the wd
|
||||
nxt_path = wd;
|
||||
@@ -274,27 +281,28 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, cons
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
for (wcstring_list_t::const_iterator iter = paths.begin(); iter != paths.end(); ++iter) {
|
||||
struct stat buf;
|
||||
const wcstring &dir = *iter;
|
||||
if( wstat( dir, &buf ) == 0 )
|
||||
for (wcstring_list_t::const_iterator iter = paths.begin(); iter != paths.end(); ++iter)
|
||||
{
|
||||
if( S_ISDIR(buf.st_mode) )
|
||||
{
|
||||
struct stat buf;
|
||||
const wcstring &dir = *iter;
|
||||
if (wstat(dir, &buf) == 0)
|
||||
{
|
||||
if (S_ISDIR(buf.st_mode))
|
||||
{
|
||||
success = true;
|
||||
if (out)
|
||||
out->assign(dir);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = ENOTDIR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (! success)
|
||||
errno = err;
|
||||
errno = err;
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -305,9 +313,9 @@ bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wch
|
||||
|
||||
bool result = false;
|
||||
if (string_prefixes_string(L"/", exp_path) ||
|
||||
string_prefixes_string(L"./", exp_path) ||
|
||||
string_prefixes_string(L"../", exp_path) ||
|
||||
exp_path == L"..")
|
||||
string_prefixes_string(L"./", exp_path) ||
|
||||
string_prefixes_string(L"../", exp_path) ||
|
||||
exp_path == L"..")
|
||||
{
|
||||
/* These paths can be implicit cd, so see if you cd to the path. Note that a single period cannot (that's used for sourcing files anyways) */
|
||||
result = path_get_cdpath(exp_path, out_path, wd, vars);
|
||||
@@ -317,41 +325,41 @@ bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wch
|
||||
|
||||
bool path_get_config(wcstring &path)
|
||||
{
|
||||
int done = 0;
|
||||
wcstring res;
|
||||
int done = 0;
|
||||
wcstring res;
|
||||
|
||||
const env_var_t xdg_dir = env_get_string( L"XDG_CONFIG_HOME" );
|
||||
if( ! xdg_dir.missing() )
|
||||
{
|
||||
res = xdg_dir + L"/fish";
|
||||
if( !create_directory( res ) )
|
||||
const env_var_t xdg_dir = env_get_string(L"XDG_CONFIG_HOME");
|
||||
if (! xdg_dir.missing())
|
||||
{
|
||||
done = 1;
|
||||
res = xdg_dir + L"/fish";
|
||||
if (!create_directory(res))
|
||||
{
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const env_var_t home = env_get_string( L"HOME" );
|
||||
if( ! home.missing() )
|
||||
else
|
||||
{
|
||||
res = home + L"/.config/fish";
|
||||
if( !create_directory( res ) )
|
||||
{
|
||||
done = 1;
|
||||
}
|
||||
const env_var_t home = env_get_string(L"HOME");
|
||||
if (! home.missing())
|
||||
{
|
||||
res = home + L"/.config/fish";
|
||||
if (!create_directory(res))
|
||||
{
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( done )
|
||||
{
|
||||
if (done)
|
||||
{
|
||||
path = res;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
debug( 0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access." ));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
debug(0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access."));
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -359,25 +367,28 @@ static void replace_all(wcstring &str, const wchar_t *needle, const wchar_t *rep
|
||||
{
|
||||
size_t needle_len = wcslen(needle);
|
||||
size_t offset = 0;
|
||||
while((offset = str.find(needle, offset)) != wcstring::npos)
|
||||
while ((offset = str.find(needle, offset)) != wcstring::npos)
|
||||
{
|
||||
str.replace(offset, needle_len, replacement);
|
||||
offset += needle_len;
|
||||
}
|
||||
}
|
||||
|
||||
void path_make_canonical( wcstring &path )
|
||||
void path_make_canonical(wcstring &path)
|
||||
{
|
||||
|
||||
/* Remove double slashes */
|
||||
size_t size;
|
||||
do {
|
||||
do
|
||||
{
|
||||
size = path.size();
|
||||
replace_all(path, L"//", L"/");
|
||||
} while (path.size() != size);
|
||||
}
|
||||
while (path.size() != size);
|
||||
|
||||
/* Remove trailing slashes, except don't remove the first one */
|
||||
while (size-- > 1) {
|
||||
while (size-- > 1)
|
||||
{
|
||||
if (path.at(size) != L'/')
|
||||
break;
|
||||
}
|
||||
@@ -389,41 +400,56 @@ bool path_is_valid(const wcstring &path, const wcstring &working_directory)
|
||||
{
|
||||
bool path_is_valid;
|
||||
/* Some special paths are always valid */
|
||||
if (path.empty()) {
|
||||
if (path.empty())
|
||||
{
|
||||
path_is_valid = false;
|
||||
} else if (path == L"." || path == L"./") {
|
||||
}
|
||||
else if (path == L"." || path == L"./")
|
||||
{
|
||||
path_is_valid = true;
|
||||
} else if (path == L".." || path == L"../") {
|
||||
}
|
||||
else if (path == L".." || path == L"../")
|
||||
{
|
||||
path_is_valid = (! working_directory.empty() && working_directory != L"/");
|
||||
} else if (path.at(0) != '/') {
|
||||
}
|
||||
else if (path.at(0) != '/')
|
||||
{
|
||||
/* Prepend the working directory. Note that we know path is not empty here. */
|
||||
wcstring tmp = working_directory;
|
||||
tmp.append(path);
|
||||
path_is_valid = (0 == waccess(tmp, F_OK));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Simple check */
|
||||
path_is_valid = (0 == waccess(path, F_OK));
|
||||
}
|
||||
return path_is_valid;
|
||||
}
|
||||
|
||||
bool paths_are_same_file(const wcstring &path1, const wcstring &path2) {
|
||||
bool paths_are_same_file(const wcstring &path1, const wcstring &path2)
|
||||
{
|
||||
if (path1 == path2)
|
||||
return true;
|
||||
|
||||
struct stat s1, s2;
|
||||
if (wstat(path1, &s1) == 0 && wstat(path2, &s2) == 0) {
|
||||
if (wstat(path1, &s1) == 0 && wstat(path2, &s2) == 0)
|
||||
{
|
||||
return s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
wcstring get_working_directory(void) {
|
||||
wcstring get_working_directory(void)
|
||||
{
|
||||
wcstring wd = L"./";
|
||||
wchar_t dir_path[4096];
|
||||
const wchar_t *cwd = wgetcwd( dir_path, 4096 );
|
||||
if (cwd) {
|
||||
const wchar_t *cwd = wgetcwd(dir_path, 4096);
|
||||
if (cwd)
|
||||
{
|
||||
wd = cwd;
|
||||
/* Make sure the working directory ends with a slash */
|
||||
if (! wd.empty() && wd.at(wd.size() - 1) != L'/')
|
||||
|
||||
Reference in New Issue
Block a user