refactor/rename: path_is_absolute()

This commit is contained in:
Justin M. Keyes
2018-03-24 11:21:20 +01:00
parent 84a25770ac
commit 998a16c926
11 changed files with 54 additions and 58 deletions

View File

@@ -13496,7 +13496,7 @@ static void f_resolve(typval_T *argvars, typval_T *rettv, FunPtr fptr)
q[-1] = NUL; q[-1] = NUL;
q = (char *)path_tail((char_u *)p); q = (char *)path_tail((char_u *)p);
} }
if (q > p && !path_is_absolute_path((const char_u *)buf)) { if (q > p && !path_is_absolute((const char_u *)buf)) {
// Symlink is relative to directory of argument. Replace the // Symlink is relative to directory of argument. Replace the
// symlink with the resolved name in the same directory. // symlink with the resolved name in the same directory.
const size_t p_len = strlen(p); const size_t p_len = strlen(p);

View File

@@ -8547,7 +8547,7 @@ eval_vars (
break; break;
case SPEC_AFILE: // file name for autocommand case SPEC_AFILE: // file name for autocommand
if (autocmd_fname != NULL && !path_is_absolute_path(autocmd_fname)) { if (autocmd_fname != NULL && !path_is_absolute(autocmd_fname)) {
// Still need to turn the fname into a full path. It was // Still need to turn the fname into a full path. It was
// postponed to avoid a delay when <afile> is not used. // postponed to avoid a delay when <afile> is not used.
result = (char_u *)FullName_save((char *)autocmd_fname, false); result = (char_u *)FullName_save((char *)autocmd_fname, false);

View File

@@ -4923,13 +4923,14 @@ static void expand_shellcmd(char_u *filepat, int *num_file, char_u ***file,
flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
bool mustfree = false; // Track memory allocation for *path. bool mustfree = false; // Track memory allocation for *path.
/* For an absolute name we don't use $PATH. */ // For an absolute name we don't use $PATH.
if (path_is_absolute_path(pat)) if (path_is_absolute(pat)) {
path = (char_u *)" "; path = (char_u *)" ";
else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) } else if (pat[0] == '.' && (vim_ispathsep(pat[1])
|| (pat[1] == '.' && vim_ispathsep(pat[2]))))) || (pat[1] == '.'
&& vim_ispathsep(pat[2])))) {
path = (char_u *)"."; path = (char_u *)".";
else { } else {
path = (char_u *)vim_getenv("PATH"); path = (char_u *)vim_getenv("PATH");
if (path == NULL) { if (path == NULL) {
path = (char_u *)""; path = (char_u *)"";

View File

@@ -4318,7 +4318,7 @@ void shorten_fnames(int force)
&& !path_with_url((char *)buf->b_fname) && !path_with_url((char *)buf->b_fname)
&& (force && (force
|| buf->b_sfname == NULL || buf->b_sfname == NULL
|| path_is_absolute_path(buf->b_sfname))) { || path_is_absolute(buf->b_sfname))) {
xfree(buf->b_sfname); xfree(buf->b_sfname);
buf->b_sfname = NULL; buf->b_sfname = NULL;
p = path_shorten_fname(buf->b_ffname, dirname); p = path_shorten_fname(buf->b_ffname, dirname);

View File

@@ -3020,20 +3020,17 @@ int resolve_symlink(const char_u *fname, char_u *buf)
} }
buf[ret] = NUL; buf[ret] = NUL;
/* // Check whether the symlink is relative or absolute.
* Check whether the symlink is relative or absolute. // If it's relative, build a new path based on the directory
* If it's relative, build a new path based on the directory // portion of the filename (if any) and the path the symlink
* portion of the filename (if any) and the path the symlink // points to.
* points to. if (path_is_absolute(buf)) {
*/
if (path_is_absolute_path(buf))
STRCPY(tmp, buf); STRCPY(tmp, buf);
else { } else {
char_u *tail; char_u *tail = path_tail(tmp);
if (STRLEN(tail) + STRLEN(buf) >= MAXPATHL) {
tail = path_tail(tmp);
if (STRLEN(tail) + STRLEN(buf) >= MAXPATHL)
return FAIL; return FAIL;
}
STRCPY(tail, buf); STRCPY(tail, buf);
} }
} }

View File

@@ -886,7 +886,7 @@ bool os_setenv_append_path(const char *fname)
// No prescribed maximum on unix. // No prescribed maximum on unix.
# define MAX_ENVPATHLEN INT_MAX # define MAX_ENVPATHLEN INT_MAX
#endif #endif
if (!path_is_absolute_path((char_u *)fname)) { if (!path_is_absolute((char_u *)fname)) {
internal_error("os_setenv_append_path()"); internal_error("os_setenv_append_path()");
return false; return false;
} }

View File

@@ -222,7 +222,7 @@ int os_exepath(char *buffer, size_t *size)
bool os_can_exe(const char_u *name, char_u **abspath, bool use_path) bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(1)
{ {
bool no_path = !use_path || path_is_absolute_path(name); bool no_path = !use_path || path_is_absolute(name);
#ifndef WIN32 #ifndef WIN32
// If the filename is "qualified" (relative or absolute) do not check $PATH. // If the filename is "qualified" (relative or absolute) do not check $PATH.
no_path |= (name[0] == '.' no_path |= (name[0] == '.'
@@ -244,7 +244,7 @@ bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
#endif #endif
if (ok) { if (ok) {
if (abspath != NULL) { if (abspath != NULL) {
*abspath = save_absolute_path(name); *abspath = save_abs_path(name);
} }
return true; return true;
} }
@@ -357,7 +357,7 @@ static bool is_executable_in_path(const char_u *name, char_u **abspath)
#endif #endif
if (ok) { if (ok) {
if (abspath != NULL) { // Caller asked for a copy of the path. if (abspath != NULL) { // Caller asked for a copy of the path.
*abspath = save_absolute_path((char_u *)buf); *abspath = save_abs_path((char_u *)buf);
} }
rv = true; rv = true;

View File

@@ -452,10 +452,10 @@ char *FullName_save(const char *fname, bool force)
/// Saves the absolute path. /// Saves the absolute path.
/// @param name An absolute or relative path. /// @param name An absolute or relative path.
/// @return The absolute path of `name`. /// @return The absolute path of `name`.
char_u *save_absolute_path(const char_u *name) char_u *save_abs_path(const char_u *name)
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{ {
if (!path_is_absolute_path(name)) { if (!path_is_absolute(name)) {
return (char_u *)FullName_save((char *)name, true); return (char_u *)FullName_save((char *)name, true);
} }
return vim_strsave((char_u *) name); return vim_strsave((char_u *) name);
@@ -814,7 +814,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
STRCPY(buf, curdir); // relative to current directory STRCPY(buf, curdir); // relative to current directory
} else if (path_with_url((char *)buf)) { } else if (path_with_url((char *)buf)) {
continue; // URL can't be used here continue; // URL can't be used here
} else if (!path_is_absolute_path(buf)) { } else if (!path_is_absolute(buf)) {
// Expand relative path to their full path equivalent // Expand relative path to their full path equivalent
size_t len = STRLEN(curdir); size_t len = STRLEN(curdir);
if (len + STRLEN(buf) + 3 > MAXPATHL) { if (len + STRLEN(buf) + 3 > MAXPATHL) {
@@ -949,19 +949,17 @@ static void uniquefy_paths(garray_T *gap, char_u *pattern)
} }
} }
if (path_is_absolute_path(path)) { if (path_is_absolute(path)) {
/* // Last resort: shorten relative to curdir if possible.
* Last resort: shorten relative to curdir if possible. // 'possible' means:
* 'possible' means: // 1. It is under the current directory.
* 1. It is under the current directory. // 2. The result is actually shorter than the original.
* 2. The result is actually shorter than the original. //
* // Before curdir After
* Before curdir After // /foo/bar/file.txt /foo/bar ./file.txt
* /foo/bar/file.txt /foo/bar ./file.txt // c:\foo\bar\file.txt c:\foo\bar .\file.txt
* c:\foo\bar\file.txt c:\foo\bar .\file.txt // /file.txt / /file.txt
* /file.txt / /file.txt // c:\file.txt c:\ .\file.txt
* c:\file.txt c:\ .\file.txt
*/
short_name = path_shorten_fname(path, curdir); short_name = path_shorten_fname(path, curdir);
if (short_name != NULL && short_name > path + 1 if (short_name != NULL && short_name > path + 1
) { ) {
@@ -1221,7 +1219,7 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file,
*/ */
if (path_has_exp_wildcard(p)) { if (path_has_exp_wildcard(p)) {
if ((flags & EW_PATH) if ((flags & EW_PATH)
&& !path_is_absolute_path(p) && !path_is_absolute(p)
&& !(p[0] == '.' && !(p[0] == '.'
&& (vim_ispathsep(p[1]) && (vim_ispathsep(p[1])
|| (p[1] == '.' && vim_ispathsep(p[2])))) || (p[1] == '.' && vim_ispathsep(p[2]))))
@@ -1667,7 +1665,7 @@ int path_with_url(const char *fname)
*/ */
bool vim_isAbsName(char_u *name) bool vim_isAbsName(char_u *name)
{ {
return path_with_url((char *)name) != 0 || path_is_absolute_path(name); return path_with_url((char *)name) != 0 || path_is_absolute(name);
} }
/// Save absolute file name to "buf[len]". /// Save absolute file name to "buf[len]".
@@ -2201,7 +2199,7 @@ static int path_get_absolute_path(const char_u *fname, char_u *buf,
char *end_of_path = (char *) fname; char *end_of_path = (char *) fname;
// expand it if forced or not an absolute path // expand it if forced or not an absolute path
if (force || !path_is_absolute_path(fname)) { if (force || !path_is_absolute(fname)) {
p = vim_strrchr(fname, '/'); p = vim_strrchr(fname, '/');
#ifdef WIN32 #ifdef WIN32
if (p == NULL) { if (p == NULL) {
@@ -2234,10 +2232,10 @@ static int path_get_absolute_path(const char_u *fname, char_u *buf,
return append_path((char *)buf, end_of_path, len); return append_path((char *)buf, end_of_path, len);
} }
/// Check if the given file is absolute. /// Check if file `fname` is a full (absolute) path.
/// ///
/// @return `TRUE` if "fname" is absolute. /// @return `TRUE` if "fname" is absolute.
int path_is_absolute_path(const char_u *fname) int path_is_absolute(const char_u *fname)
{ {
#ifdef WIN32 #ifdef WIN32
// A name like "d:/foo" and "//server/share" is absolute // A name like "d:/foo" and "//server/share" is absolute
@@ -2262,7 +2260,7 @@ void path_guess_exepath(const char *argv0, char *buf, size_t bufsize)
{ {
char *path = getenv("PATH"); char *path = getenv("PATH");
if (path == NULL || path_is_absolute_path((char_u *)argv0)) { if (path == NULL || path_is_absolute((char_u *)argv0)) {
xstrlcpy(buf, argv0, bufsize); xstrlcpy(buf, argv0, bufsize);
} else if (argv0[0] == '.' || strchr(argv0, PATHSEP)) { } else if (argv0[0] == '.' || strchr(argv0, PATHSEP)) {
// Relative to CWD. // Relative to CWD.

View File

@@ -4239,11 +4239,11 @@ static void syn_cmd_include(exarg_T *eap, int syncing)
*/ */
eap->argt |= (XFILE | NOSPC); eap->argt |= (XFILE | NOSPC);
separate_nextcmd(eap); separate_nextcmd(eap);
if (*eap->arg == '<' || *eap->arg == '$' || path_is_absolute_path(eap->arg)) { if (*eap->arg == '<' || *eap->arg == '$' || path_is_absolute(eap->arg)) {
/* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the // For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
* file. Need to expand the file name first. In other cases // file. Need to expand the file name first. In other cases
* ":runtime!" is used. */ // ":runtime!" is used.
source = TRUE; source = true;
if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL) { if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL) {
if (errormsg != NULL) if (errormsg != NULL)
EMSG(errormsg); EMSG(errormsg);

View File

@@ -199,7 +199,7 @@ describe('fs.c', function()
itp('returns the absolute path when given an executable inside $PATH', function() itp('returns the absolute path when given an executable inside $PATH', function()
local fullpath = exe('ls') local fullpath = exe('ls')
eq(1, fs.path_is_absolute_path(to_cstr(fullpath))) eq(1, fs.path_is_absolute(to_cstr(fullpath)))
end) end)
itp('returns the absolute path when given an executable relative to the current dir', function() itp('returns the absolute path when given an executable relative to the current dir', function()

View File

@@ -585,22 +585,22 @@ describe('path.c', function()
end) end)
end) end)
describe('path_is_absolute_path', function() describe('path_is_absolute', function()
local function path_is_absolute_path(filename) local function path_is_absolute(filename)
filename = to_cstr(filename) filename = to_cstr(filename)
return cimp.path_is_absolute_path(filename) return cimp.path_is_absolute(filename)
end end
itp('returns true if filename starts with a slash', function() itp('returns true if filename starts with a slash', function()
eq(OK, path_is_absolute_path('/some/directory/')) eq(OK, path_is_absolute('/some/directory/'))
end) end)
itp('returns true if filename starts with a tilde', function() itp('returns true if filename starts with a tilde', function()
eq(OK, path_is_absolute_path('~/in/my/home~/directory')) eq(OK, path_is_absolute('~/in/my/home~/directory'))
end) end)
itp('returns false if filename starts not with slash nor tilde', function() itp('returns false if filename starts not with slash nor tilde', function()
eq(FAIL, path_is_absolute_path('not/in/my/home~/directory')) eq(FAIL, path_is_absolute('not/in/my/home~/directory'))
end) end)
end) end)
end) end)