Merge pull request #4832 from justinmk/do_source_fuss

do_source: less fuss about fopen_noinh_readbin
This commit is contained in:
Justin M. Keyes
2016-05-28 19:01:15 -04:00
2 changed files with 11 additions and 24 deletions

View File

@@ -49,7 +49,6 @@
#cmakedefine UNIX #cmakedefine UNIX
#cmakedefine USE_FNAME_CASE #cmakedefine USE_FNAME_CASE
#define FEAT_CSCOPE
#cmakedefine FEAT_TUI #cmakedefine FEAT_TUI
#ifndef UNIT_TESTING #ifndef UNIT_TESTING

View File

@@ -2481,32 +2481,31 @@ int source_level(void *cookie)
return ((struct source_cookie *)cookie)->level; return ((struct source_cookie *)cookie)->level;
} }
/// Special function to open a file without handle inheritance.
#if (defined(WIN32) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC) /// If possible the handle is closed on exec().
# define USE_FOPEN_NOINH
/*
* Special function to open a file without handle inheritance.
* When possible the handle is closed on exec().
*/
static FILE *fopen_noinh_readbin(char *filename) static FILE *fopen_noinh_readbin(char *filename)
{ {
#ifdef WIN32
int fd_tmp = os_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
#else
int fd_tmp = os_open(filename, O_RDONLY, 0); int fd_tmp = os_open(filename, O_RDONLY, 0);
#endif
if (fd_tmp < 0) if (fd_tmp < 0) {
return NULL; return NULL;
}
# ifdef HAVE_FD_CLOEXEC #ifdef HAVE_FD_CLOEXEC
{ {
int fdflags = fcntl(fd_tmp, F_GETFD); int fdflags = fcntl(fd_tmp, F_GETFD);
if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) { if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) {
(void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC); (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC);
} }
} }
# endif #endif
return fdopen(fd_tmp, READBIN); return fdopen(fd_tmp, READBIN);
} }
#endif
/* /*
@@ -2560,11 +2559,7 @@ do_source (
/* Apply SourcePre autocommands, they may get the file. */ /* Apply SourcePre autocommands, they may get the file. */
apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf); apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
#ifdef USE_FOPEN_NOINH
cookie.fp = fopen_noinh_readbin((char *)fname_exp); cookie.fp = fopen_noinh_readbin((char *)fname_exp);
#else
cookie.fp = mch_fopen((char *)fname_exp, READBIN);
#endif
if (cookie.fp == NULL && check_other) { if (cookie.fp == NULL && check_other) {
/* /*
* Try again, replacing file name ".vimrc" by "_vimrc" or vice versa, * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
@@ -2573,15 +2568,8 @@ do_source (
p = path_tail(fname_exp); p = path_tail(fname_exp);
if ((*p == '.' || *p == '_') if ((*p == '.' || *p == '_')
&& (STRICMP(p + 1, "nvimrc") == 0 || STRICMP(p + 1, "exrc") == 0)) { && (STRICMP(p + 1, "nvimrc") == 0 || STRICMP(p + 1, "exrc") == 0)) {
if (*p == '_') *p = (*p == '_') ? '.' : '_';
*p = '.';
else
*p = '_';
#ifdef USE_FOPEN_NOINH
cookie.fp = fopen_noinh_readbin((char *)fname_exp); cookie.fp = fopen_noinh_readbin((char *)fname_exp);
#else
cookie.fp = mch_fopen((char *)fname_exp, READBIN);
#endif
} }
} }