summaryrefslogtreecommitdiff
path: root/lib/dup2.c
diff options
context:
space:
mode:
authorKO Myung-Hun <komh78@gmail.com>2016-01-14 11:23:47 +0900
committerPaul Eggert <eggert@cs.ucla.edu>2016-01-14 16:45:38 -0800
commitc7e1cc415cd60f989fc75b99596e3bd656d3004e (patch)
tree0410bcb7a34539d6aaab79772cf9cf9e75ab122e /lib/dup2.c
parentcb05f28617541bcc4bf800d7c9567107f1603c29 (diff)
downloadgnulib-c7e1cc415cd60f989fc75b99596e3bd656d3004e.tar.gz
dup, dup2, fcntl: support a directory fd on OS/2 kLIBC
On OS/2 kLIBC, dup(), dup2() and fcntl() do not work on a directory fd. * lib/dup.c (dup_nothrow): New. * lib/dup2.c (klibc_dup2dirfd): New. dup2() for a directory fd. (klibc_dup2): New. * lib/fcntl.c (klibc_fcntl): New. * m4/dup.m4 (gl_FUNC_DUP): Check if dup() works on a directory fd. * m4/dup2.m4 (gl_FUNC_DUP2): Check if dup2() works on a directory fd. * m4/fcntl.m4 (gl_FUNC_FCNTL): Check if F_DUPFD works on a directory fd.
Diffstat (limited to 'lib/dup2.c')
-rw-r--r--lib/dup2.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/dup2.c b/lib/dup2.c
index c913f473ad..5d026f21fa 100644
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -85,6 +85,57 @@ ms_windows_dup2 (int fd, int desired_fd)
# define dup2 ms_windows_dup2
+# elif defined __KLIBC__
+
+# include <InnoTekLIBC/backend.h>
+
+static int
+klibc_dup2dirfd (int fd, int desired_fd)
+{
+ int tempfd;
+ int dupfd;
+
+ tempfd = open ("NUL", O_RDONLY);
+ if (tempfd == -1)
+ return -1;
+
+ if (tempfd == desired_fd)
+ {
+ close (tempfd);
+
+ char path[_MAX_PATH];
+ if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
+ return -1;
+
+ return open(path, O_RDONLY);
+ }
+
+ dupfd = klibc_dup2dirfd (fd, desired_fd);
+
+ close (tempfd);
+
+ return dupfd;
+}
+
+static int
+klibc_dup2 (int fd, int desired_fd)
+{
+ int dupfd;
+ struct stat sbuf;
+
+ dupfd = dup2 (fd, desired_fd);
+ if (dupfd == -1 && errno == ENOTSUP \
+ && !fstat (fd, &sbuf) && S_ISDIR (sbuf.st_mode))
+ {
+ close (desired_fd);
+
+ return klibc_dup2dirfd (fd, desired_fd);
+ }
+
+ return dupfd;
+}
+
+# define dup2 klibc_dup2
# endif
int