diff options
author | ZyX <kp-pav@yandex.ru> | 2016-06-21 22:18:03 +0300 |
---|---|---|
committer | ZyX <kp-pav@yandex.ru> | 2016-06-23 21:17:51 +0300 |
commit | a8f3849bc04cee5d7166138a28cb1e7bbf300803 (patch) | |
tree | 640f647982f1745bf5b26d19e5896a92f00b87a7 /src/nvim/file.c | |
parent | 2ac5f1138b5c4c5540479e26ca9b9755cf7b784d (diff) | |
download | rneovim-a8f3849bc04cee5d7166138a28cb1e7bbf300803.tar.gz rneovim-a8f3849bc04cee5d7166138a28cb1e7bbf300803.tar.bz2 rneovim-a8f3849bc04cee5d7166138a28cb1e7bbf300803.zip |
file: Use own constants, do not rely on fcntl.h
One of the reasons is that O_RDONLY is zero, which makes checking whether file
is opened read- or write-only harder. It is not guaranteed that on other system
O_WRONLY will not be zero (e.g. because file can only be opened in read-write
mode).
Diffstat (limited to 'src/nvim/file.c')
-rw-r--r-- | src/nvim/file.c | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/nvim/file.c b/src/nvim/file.c index 262343fe29..35c659f7fd 100644 --- a/src/nvim/file.c +++ b/src/nvim/file.c @@ -8,6 +8,7 @@ #include <assert.h> #include <stddef.h> #include <stdbool.h> +#include <fcntl.h> #include "auto/config.h" @@ -46,15 +47,37 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, const int flags, const int mode) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { + int os_open_flags = 0; int fd; + TriState wr = kNone; +#define FLAG(flags, flag, fcntl_flags, wrval, cond) \ + do { \ + if (flags & flag) { \ + os_open_flags |= fcntl_flags; \ + assert(cond); \ + if (wrval != kNone) { \ + wr = wrval; \ + } \ + } \ + } while (0) + FLAG(flags, kFileWriteOnly, O_WRONLY, kTrue, true); + FLAG(flags, kFileCreateOnly, O_CREAT|O_EXCL|O_WRONLY, kTrue, true); + FLAG(flags, kFileCreate, O_CREAT|O_WRONLY, kTrue, !(flags & kFileCreateOnly)); + FLAG(flags, kFileTruncate, O_TRUNC|O_WRONLY, kTrue, + !(flags & kFileCreateOnly)); + FLAG(flags, kFileReadOnly, O_RDONLY, kFalse, wr != kTrue); +#ifdef O_NOFOLLOW + FLAG(flags, kFileNoSymlink, O_NOFOLLOW, kNone, true); +#endif +#undef FLAG - fd = os_open(fname, flags, mode); + fd = os_open(fname, os_open_flags, mode); if (fd < 0) { return fd; } - ret_fp->wr = (bool)(!!(flags & FILE_WRITE_ONLY)); + ret_fp->wr = (wr == kTrue); ret_fp->fd = fd; ret_fp->eof = false; ret_fp->rv = rbuffer_new(RWBUFSIZE); |