mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 21:44:16 +00:00
fcntl a little less
Setting O_CLOEXEC on closed file descriptors and getting E_BADF should be faster than actually checking if an fd is open first.
This commit is contained in:
parent
bf40f84b06
commit
dd9a26715d
2 changed files with 7 additions and 11 deletions
|
@ -401,13 +401,14 @@ int main(int argc, char **argv) {
|
||||||
res = reader_read(STDIN_FILENO, {});
|
res = reader_read(STDIN_FILENO, {});
|
||||||
} else {
|
} else {
|
||||||
char *file = *(argv + (my_optind++));
|
char *file = *(argv + (my_optind++));
|
||||||
|
#if defined(O_CLOEXEC)
|
||||||
|
int fd = open(file, O_RDONLY | O_CLOEXEC);
|
||||||
|
#else
|
||||||
int fd = open(file, O_RDONLY);
|
int fd = open(file, O_RDONLY);
|
||||||
|
#endif
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
perror(file);
|
perror(file);
|
||||||
} else {
|
} else {
|
||||||
// OK to not do this atomically since we cannot have gone multithreaded yet.
|
|
||||||
set_cloexec(fd);
|
|
||||||
|
|
||||||
wcstring_list_t list;
|
wcstring_list_t list;
|
||||||
for (char **ptr = argv + my_optind; *ptr; ptr++) {
|
for (char **ptr = argv + my_optind; *ptr; ptr++) {
|
||||||
list.push_back(str2wcstring(*ptr));
|
list.push_back(str2wcstring(*ptr));
|
||||||
|
|
|
@ -176,14 +176,9 @@ FILE *wfopen(const wcstring &path, const char *mode) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool set_cloexec(int fd) {
|
bool set_cloexec(int fd) {
|
||||||
int flags = fcntl(fd, F_GETFD, 0);
|
int flags = fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
|
||||||
if (flags < 0) {
|
if (flags == -1) return false;
|
||||||
return false;
|
return true;
|
||||||
}
|
|
||||||
if (flags & FD_CLOEXEC) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec) {
|
static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool cloexec) {
|
||||||
|
|
Loading…
Reference in a new issue