From 69e3e11a75ed1dabc854406e29bacd5401912874 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 21 Jan 2017 16:34:08 +0100 Subject: Don't open files from /dev/fd/, just return the fd. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Advantage 1: Less file descriptors opened. Advantage 2: The other end will be alerted when the process closes the file descriptor, and does not have to wait until the process exits. Signed-off-by: Mattias Andrée --- src/util.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index c315010..60e4cb0 100644 --- a/src/util.c +++ b/src/util.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -220,3 +221,17 @@ enjoin_jobs(int status, int is_master, pid_t *pids) } /* } */ + + +int +xenopen(int status, const char *path, int flags, int mode, ...) +{ + int fd; + if (strncmp(path, "/dev/fd/", sizeof("/dev/fd/") - 1)) + if (!toi(path + sizeof("/dev/fd/") - 1, 0, INT_MAX, &fd)) + return fd; + fd = open(path, flags, mode); + if (fd < 0) + enprintf(status, "open %s:", path); + return fd; +} -- cgit v1.2.3-70-g09d2