diff options
author | John Schmidt <john.schmidt.h@gmail.com> | 2014-04-03 18:06:34 +0200 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-04-08 21:56:05 -0300 |
commit | 4348d1e6f74a87af55c6c01e7a0cb292e9dec114 (patch) | |
tree | 86f6e5da3cd9961f675b9aec046b2967e4db756e /src/os | |
parent | 49f5e5d0b132747048f45e10cf5cafef7e07ab7d (diff) | |
download | rneovim-4348d1e6f74a87af55c6c01e7a0cb292e9dec114.tar.gz rneovim-4348d1e6f74a87af55c6c01e7a0cb292e9dec114.tar.bz2 rneovim-4348d1e6f74a87af55c6c01e7a0cb292e9dec114.zip |
Move functions from os/fs.c into path.c
Move unit tests from os/fs.moon to path.moon
Diffstat (limited to 'src/os')
-rw-r--r-- | src/os/fs.c | 191 | ||||
-rw-r--r-- | src/os/os.h | 25 |
2 files changed, 1 insertions, 215 deletions
diff --git a/src/os/fs.c b/src/os/fs.c index 9d09e5eb25..2834b1e836 100644 --- a/src/os/fs.c +++ b/src/os/fs.c @@ -5,6 +5,7 @@ #include "message.h" #include "misc1.h" #include "misc2.h" +#include "path.h" // Many fs functions from libuv return that value on success. static const int kLibuvSuccess = 0; @@ -30,117 +31,6 @@ int os_dirname(char_u *buf, size_t len) return OK; } -/// Get the absolute name of the given relative directory. -/// -/// @param directory Directory name, relative to current directory. -/// @return `FAIL` for failure, `OK` for success. -int os_full_dir_name(char *directory, char *buffer, int len) -{ - int retval = OK; - - if (STRLEN(directory) == 0) { - return os_dirname((char_u *) buffer, len); - } - - char old_dir[MAXPATHL]; - - // Get current directory name. - if (os_dirname((char_u *) old_dir, MAXPATHL) == FAIL) { - return FAIL; - } - - // We have to get back to the current dir at the end, check if that works. - if (os_chdir(old_dir) != kLibuvSuccess) { - return FAIL; - } - - if (os_chdir(directory) != kLibuvSuccess) { - // Do not return immediatly since we may be in the wrong directory. - retval = FAIL; - } - - if (retval == FAIL || os_dirname((char_u *) buffer, len) == FAIL) { - // Do not return immediatly since we are in the wrong directory. - retval = FAIL; - } - - if (os_chdir(old_dir) != kLibuvSuccess) { - // That shouldn't happen, since we've tested if it works. - retval = FAIL; - EMSG(_(e_prev_dir)); - } - - return retval; -} - -// Append to_append to path with a slash in between. -int append_path(char *path, const char *to_append, int max_len) -{ - int current_length = STRLEN(path); - int to_append_length = STRLEN(to_append); - - // Do not append empty strings. - if (to_append_length == 0) { - return OK; - } - - // Do not append a dot. - if (STRCMP(to_append, ".") == 0) { - return OK; - } - - // Glue both paths with a slash. - if (current_length > 0 && path[current_length-1] != '/') { - current_length += 1; // Count the trailing slash. - - // +1 for the NUL at the end. - if (current_length + 1 > max_len) { - return FAIL; - } - - STRCAT(path, "/"); - } - - // +1 for the NUL at the end. - if (current_length + to_append_length + 1 > max_len) { - return FAIL; - } - - STRCAT(path, to_append); - return OK; -} - -int os_get_absolute_path(char_u *fname, char_u *buf, int len, int force) -{ - char_u *p; - *buf = NUL; - - char relative_directory[len]; - char *end_of_path = (char *) fname; - - // expand it if forced or not an absolute path - if (force || !os_is_absolute_path(fname)) { - if ((p = vim_strrchr(fname, '/')) != NULL) { - STRNCPY(relative_directory, fname, p-fname); - relative_directory[p-fname] = NUL; - end_of_path = (char *) (p + 1); - } else { - relative_directory[0] = NUL; - end_of_path = (char *) fname; - } - - if (FAIL == os_full_dir_name(relative_directory, (char *) buf, len)) { - return FAIL; - } - } - return append_path((char *) buf, (char *) end_of_path, len); -} - -int os_is_absolute_path(const char_u *fname) -{ - return *fname == '/' || *fname == '~'; -} - bool os_isdir(const char_u *name) { int32_t mode = os_getperm(name); @@ -155,85 +45,6 @@ bool os_isdir(const char_u *name) return true; } -static bool is_executable(const char_u *name); -static bool is_executable_in_path(const char_u *name); - -bool os_can_exe(const char_u *name) -{ - // If it's an absolute or relative path don't need to use $PATH. - if (os_is_absolute_path(name) || - (name[0] == '.' && (name[1] == '/' || - (name[1] == '.' && name[2] == '/')))) { - return is_executable(name); - } - - return is_executable_in_path(name); -} - -// Return true if "name" is an executable file, false if not or it doesn't -// exist. -static bool is_executable(const char_u *name) -{ - int32_t mode = os_getperm(name); - - if (mode < 0) { - return false; - } - - if (S_ISREG(mode) && (S_IEXEC & mode)) { - return true; - } - - return false; -} - -/// Check if a file is inside the $PATH and is executable. -/// -/// @return `true` if `name` is an executable inside $PATH. -static bool is_executable_in_path(const char_u *name) -{ - const char *path = getenv("PATH"); - // PATH environment variable does not exist or is empty. - if (path == NULL || *path == NUL) { - return false; - } - - int buf_len = STRLEN(name) + STRLEN(path) + 2; - char_u *buf = alloc((unsigned)(buf_len)); - - // Walk through all entries in $PATH to check if "name" exists there and - // is an executable file. - for (;; ) { - const char *e = strchr(path, ':'); - if (e == NULL) { - e = path + STRLEN(path); - } - - // Glue together the given directory from $PATH with name and save into - // buf. - vim_strncpy(buf, (char_u *) path, e - path); - append_path((char *) buf, (const char *) name, buf_len); - - if (is_executable(buf)) { - // Found our executable. Free buf and return. - vim_free(buf); - return true; - } - - if (*e != ':') { - // End of $PATH without finding any executable called name. - vim_free(buf); - return false; - } - - path = e + 1; - } - - // We should never get to this point. - assert(false); - return false; -} - int os_stat(const char_u *name, uv_stat_t *statbuf) { uv_fs_t request; diff --git a/src/os/os.h b/src/os/os.h index eac6e730c5..96164a59f5 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -16,36 +16,11 @@ int os_chdir(const char *path); /// @return `OK` for success, `FAIL` for failure. int os_dirname(char_u *buf, size_t len); -/// Expand a given file to its absolute path. -/// -/// @param fname The filename which should be expanded. -/// @param buf Buffer to store the absolute path of `fname`. -/// @param len Length of `buf`. -/// @param force Also expand when `fname` is already absolute. -/// @return `FAIL` for failure, `OK` for success. -int os_get_absolute_path(char_u *fname, char_u *buf, int len, int force); - -/// Check if the given file is absolute. -/// -/// This just checks if the file name starts with '/' or '~'. -/// @return `true` if "fname" is absolute. -int os_is_absolute_path(const char_u *fname); - /// Check if the given path is a directory or not. /// /// @return `true` if `fname` is a directory. bool os_isdir(const char_u *name); -/// Check if the given path represents an executable file. -/// -/// @return `true` if `name` is executable and -/// - can be found in $PATH, -/// - is relative to current dir or -/// - is absolute. -/// -/// @return `false` otherwise. -bool os_can_exe(const char_u *name); - /// Get the file permissions for a given file. /// /// @return `-1` when `name` doesn't exist. |