diff options
Diffstat (limited to 'src/nvim/os/fs.c')
-rw-r--r-- | src/nvim/os/fs.c | 168 |
1 files changed, 93 insertions, 75 deletions
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 30e08ac129..097c672887 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -219,49 +219,86 @@ int os_exepath(char *buffer, size_t *size) bool os_can_exe(const char_u *name, char_u **abspath, bool use_path) FUNC_ATTR_NONNULL_ARG(1) { - // when use_path is false or if it's an absolute or relative path don't - // need to use $PATH. - if (!use_path || path_is_absolute_path(name) - || (name[0] == '.' - && (name[1] == '/' - || (name[1] == '.' && name[2] == '/')))) { - // There must be a path separator, files in the current directory - // can't be executed - if (gettail_dir(name) != name && is_executable(name)) { + bool no_path = !use_path || path_is_absolute_path(name); +#ifndef WIN32 + // If the filename is "qualified" (relative or absolute) do not check $PATH. + no_path |= (name[0] == '.' + && (name[1] == '/' || (name[1] == '.' && name[2] == '/'))); +#endif + + if (no_path) { +#ifdef WIN32 + const char *pathext = os_getenv("PATHEXT"); + if (!pathext) { + pathext = ".com;.exe;.bat;.cmd"; + } + bool ok = is_executable((char *)name) || is_executable_ext((char *)name, + pathext); +#else + // Must have path separator, cannot execute files in the current directory. + bool ok = gettail_dir(name) != name && is_executable((char *)name); +#endif + if (ok) { if (abspath != NULL) { *abspath = save_absolute_path(name); } - return true; } - return false; } return is_executable_in_path(name, abspath); } -// Return true if "name" is an executable file, false if not or it doesn't -// exist. -static bool is_executable(const char_u *name) +/// Returns true if `name` is an executable file. +static bool is_executable(const char *name) FUNC_ATTR_NONNULL_ALL { - int32_t mode = os_getperm(name); + int32_t mode = os_getperm((char_u *)name); if (mode < 0) { return false; } -#if WIN32 +#ifdef WIN32 // Windows does not have exec bit; just check if the file exists and is not // a directory. return (S_ISREG(mode)); #else return (S_ISREG(mode) && (S_IXUSR & mode)); #endif +} +#ifdef WIN32 +/// Appends file extensions from `pathext` to `name` and returns true if any +/// such combination is executable. +static bool is_executable_ext(char *name, const char *pathext) + FUNC_ATTR_NONNULL_ALL +{ + xstrlcpy((char *)NameBuff, name, sizeof(NameBuff)); + char *buf_end = xstrchrnul((char *)NameBuff, '\0'); + for (const char *ext = pathext; *ext; ext++) { + // Skip the extension if there is no suffix after a '.'. + if (ext[0] == '.' && (ext[1] == '\0' || ext[1] == ENV_SEPCHAR)) { + ext++; + continue; + } + + const char *ext_end = xstrchrnul(ext, ENV_SEPCHAR); + STRLCPY(buf_end, ext, ext_end - ext + 1); + + if (is_executable((char *)NameBuff)) { + return true; + } + + if (*ext_end != ENV_SEPCHAR) { + break; + } + ext = ext_end; + } return false; } +#endif /// Checks if a file is inside the `$PATH` and is executable. /// @@ -272,89 +309,69 @@ static bool is_executable(const char_u *name) static bool is_executable_in_path(const char_u *name, char_u **abspath) FUNC_ATTR_NONNULL_ARG(1) { - const char *path = os_getenv("PATH"); - if (path == NULL) { + const char *path_env = os_getenv("PATH"); + if (path_env == NULL) { return false; } - size_t buf_len = STRLEN(name) + STRLEN(path) + 2; +#ifdef WIN32 + // Prepend ".;" to $PATH. + size_t pathlen = strlen(path_env); + char *path = memcpy(xmallocz(pathlen + 3), "." ENV_SEPSTR, 2); + memcpy(path + 2, path_env, pathlen); +#else + char *path = xstrdup(path_env); +#endif + + size_t buf_len = STRLEN(name) + strlen(path) + 2; #ifdef WIN32 const char *pathext = os_getenv("PATHEXT"); if (!pathext) { pathext = ".com;.exe;.bat;.cmd"; } - - buf_len += STRLEN(pathext); + buf_len += strlen(pathext); #endif - char_u *buf = xmalloc(buf_len); + char *buf = xmalloc(buf_len); // Walk through all entries in $PATH to check if "name" exists there and // is an executable file. + char *p = path; + bool rv = false; for (;; ) { - const char *e = xstrchrnul(path, ENV_SEPCHAR); + char *e = xstrchrnul(p, ENV_SEPCHAR); - // Glue together the given directory from $PATH with name and save into - // buf. - STRLCPY(buf, path, e - path + 1); - append_path((char *) buf, (const char *) name, buf_len); - - if (is_executable(buf)) { - // Check if the caller asked for a copy of the path. - if (abspath != NULL) { - *abspath = save_absolute_path(buf); - } - - xfree(buf); - - return true; - } + // Combine the $PATH segment with `name`. + STRLCPY(buf, p, e - p + 1); + append_path(buf, (char *)name, buf_len); #ifdef WIN32 - // Try appending file extensions from $PATHEXT to the name. - char *buf_end = xstrchrnul((char *)buf, '\0'); - for (const char *ext = pathext; *ext; ext++) { - // Skip the extension if there is no suffix after a '.'. - if (ext[0] == '.' && (ext[1] == '\0' || ext[1] == ';')) { - *ext++; - - continue; - } - - const char *ext_end = xstrchrnul(ext, ENV_SEPCHAR); - STRLCPY(buf_end, ext, ext_end - ext + 1); - - if (is_executable(buf)) { - // Check if the caller asked for a copy of the path. - if (abspath != NULL) { - *abspath = save_absolute_path(buf); - } - - xfree(buf); - - return true; + bool ok = is_executable(buf) || is_executable_ext(buf, pathext); +#else + bool ok = is_executable(buf); +#endif + if (ok) { + if (abspath != NULL) { // Caller asked for a copy of the path. + *abspath = save_absolute_path((char_u *)buf); } - if (*ext_end != ENV_SEPCHAR) { - break; - } - ext = ext_end; + rv = true; + goto end; } -#endif if (*e != ENV_SEPCHAR) { // End of $PATH without finding any executable called name. - xfree(buf); - return false; + goto end; } - path = e + 1; + p = e + 1; } - // We should never get to this point. - assert(false); - return false; +end: + xfree(buf); + xfree(path); + return rv; } /// Opens or creates a file and returns a non-negative integer representing @@ -407,11 +424,11 @@ ptrdiff_t os_read(const int fd, bool *ret_eof, char *const ret_buf, size_t read_bytes = 0; bool did_try_to_free = false; while (read_bytes != size) { + assert(size >= read_bytes); const ptrdiff_t cur_read_bytes = read(fd, ret_buf + read_bytes, size - read_bytes); if (cur_read_bytes > 0) { read_bytes += (size_t)cur_read_bytes; - assert(read_bytes <= size); } if (cur_read_bytes < 0) { const int error = os_translate_sys_error(errno); @@ -510,6 +527,7 @@ ptrdiff_t os_write(const int fd, const char *const buf, const size_t size) } size_t written_bytes = 0; while (written_bytes != size) { + assert(size >= written_bytes); const ptrdiff_t cur_written_bytes = write(fd, buf + written_bytes, size - written_bytes); if (cur_written_bytes > 0) { @@ -932,12 +950,12 @@ bool os_fileid_equal_fileinfo(const FileID *file_id, /// When "fname" is the name of a shortcut (*.lnk) resolve the file it points /// to and return that name in allocated memory. /// Otherwise NULL is returned. -char_u * os_resolve_shortcut(char_u *fname) +char *os_resolve_shortcut(char_u *fname) { HRESULT hr; IPersistFile *ppf = NULL; OLECHAR wsz[MAX_PATH]; - char_u *rfname = NULL; + char *rfname = NULL; int len; IShellLinkW *pslw = NULL; WIN32_FIND_DATAW ffdw; @@ -1019,7 +1037,7 @@ shortcut_end: int os_translate_sys_error(int sys_errno) { #ifdef HAVE_UV_TRANSLATE_SYS_ERROR return uv_translate_sys_error(sys_errno); -#elif WIN32 +#elif defined(WIN32) // TODO(equalsraf): libuv does not yet expose uv_translate_sys_error() // in its public API, include a version here until it can be used. // See https://github.com/libuv/libuv/issues/79 |