summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2016-12-04 11:39:55 -0500
committerTavian Barnes <tavianator@tavianator.com>2016-12-04 11:39:55 -0500
commit49e767ccb9dcfd2161b89f0d7a980eb85f056c5d (patch)
tree3cdb06282e30c02e3b37615d39b5c1c75af53e3d /util.c
parent6962fb41b8e57f8bc817b477cf16262a0ce876d5 (diff)
downloadbfs-49e767ccb9dcfd2161b89f0d7a980eb85f056c5d.tar.xz
Move portability code into util.h
Diffstat (limited to 'util.c')
-rw-r--r--util.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/util.c b/util.c
index aef5fbb..bc611a8 100644
--- a/util.c
+++ b/util.c
@@ -58,3 +58,21 @@ int redirect(int fd, const char *path, int flags, ...) {
return ret;
}
+
+int dup_cloexec(int fd) {
+#ifdef F_DUPFD_CLOEXEC
+ return fcntl(fd, F_DUPFD_CLOEXEC, 0);
+#else
+ int ret = dup(fd);
+ if (ret < 0) {
+ return -1;
+ }
+
+ if (fcntl(ret, F_SETFD, FD_CLOEXEC) == -1) {
+ close(ret);
+ return -1;
+ }
+
+ return ret;
+#endif
+}