diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2015-08-11 16:34:45 -0400 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2015-08-17 01:06:00 -0400 |
commit | 3ce1b4015f8e830af72b679b6d39ab0bf4f69768 (patch) | |
tree | a1932bc8e50f3f2f6cdd55a5fb1f12a5cc40d16c | |
parent | 616b787d129b3550dc4b103868cf09e267195210 (diff) | |
download | rneovim-3ce1b4015f8e830af72b679b6d39ab0bf4f69768.tar.gz rneovim-3ce1b4015f8e830af72b679b6d39ab0bf4f69768.tar.bz2 rneovim-3ce1b4015f8e830af72b679b6d39ab0bf4f69768.zip |
fs.c: implement os_file_is_readable()
Use access() because:
- We already use it for os_file_is_writable()
- Vim's old check_file_readonly() ends up using access() after all.
-rw-r--r-- | src/nvim/eval.c | 23 | ||||
-rw-r--r-- | src/nvim/os/fs.c | 11 |
2 files changed, 14 insertions, 20 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index c7c67cfca4..bf9a219e28 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -8562,27 +8562,12 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv) } } -/* - * "filereadable()" function - */ +/// "filereadable()" function static void f_filereadable(typval_T *argvars, typval_T *rettv) { - int fd; - char_u *p; - int n; - -#ifndef O_NONBLOCK -# define O_NONBLOCK 0 -#endif - p = get_tv_string(&argvars[0]); - if (*p && !os_isdir(p) && (fd = os_open((char *)p, - O_RDONLY | O_NONBLOCK, 0)) >= 0) { - n = TRUE; - close(fd); - } else - n = FALSE; - - rettv->vval.v_number = n; + char_u *p = get_tv_string(&argvars[0]); + rettv->vval.v_number = + (*p && !os_isdir(p) && os_file_is_readable((char*)p)); } /* diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 5eeb275701..4cb1beb22f 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -279,13 +279,22 @@ bool os_file_is_readonly(const char *name) return access(name, W_OK) != 0; } +/// Check if a file is readable. +/// +/// @return true if `name` is readable, otherwise false. +bool os_file_is_readable(const char *name) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + return (access(name, R_OK) == 0); +} + /// Check if a file is writable. /// /// @return `0` if `name` is not writable, /// @return `1` if `name` is writable, /// @return `2` for a directory which we have rights to write into. int os_file_is_writable(const char *name) - FUNC_ATTR_NONNULL_ALL + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { if (access(name, W_OK) == 0) { if (os_isdir((char_u *)name)) { |