[git commit] dup2: Use dup3 if arch does not have the dup2 syscall

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Wed Feb 20 12:45:10 UTC 2013


commit: http://git.uclibc.org/uClibc/commit/?id=90accddebef0cf967e67c9d2412082a361d9f2bd
branch: http://git.uclibc.org/uClibc/commit/?id=refs/heads/master

Signed-off-by: Markos Chandras <markos.chandras at imgtec.com>

Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
 include/unistd.h                 |    1 +
 libc/sysdeps/linux/common/dup2.c |   17 +++++++++++++++++
 libc/sysdeps/linux/common/dup3.c |    1 +
 3 files changed, 19 insertions(+), 0 deletions(-)

diff --git a/include/unistd.h b/include/unistd.h
index 9479554..81f8e24 100644
--- a/include/unistd.h
+++ b/include/unistd.h
@@ -566,6 +566,7 @@ libc_hidden_proto(dup2)
 /* Duplicate FD to FD2, closing FD2 and making it open on the same
    file while setting flags according to FLAGS.  */
 extern int dup3 (int __fd, int __fd2, int __flags) __THROW;
+libc_hidden_proto(dup3)
 #endif
 
 /* NULL-terminated array of "NAME=VALUE" environment variables.  */
diff --git a/libc/sysdeps/linux/common/dup2.c b/libc/sysdeps/linux/common/dup2.c
index 006f06b..98ac4e3 100644
--- a/libc/sysdeps/linux/common/dup2.c
+++ b/libc/sysdeps/linux/common/dup2.c
@@ -9,7 +9,24 @@
 
 #include <sys/syscall.h>
 #include <unistd.h>
+#if defined __NR_dup3 && !defined __NR_dup2
+# include <fcntl.h>
+extern int __libc_fcntl (int fd, int cmd, ...);
+libc_hidden_proto(__libc_fcntl);
 
+int dup2(int old, int newfd)
+{
+	/*
+	 * Check if old fd is valid before we try
+	 * to ducplicate it. Return it if valid
+	 * or EBADF otherwise
+	 */
+	if (old == newfd)
+		return fcntl(old, F_GETFL, 0) < 0 ? -1 : newfd;
 
+	return dup3(old, newfd, 0);
+}
+#else
 _syscall2(int, dup2, int, oldfd, int, newfd)
+#endif
 libc_hidden_def(dup2)
diff --git a/libc/sysdeps/linux/common/dup3.c b/libc/sysdeps/linux/common/dup3.c
index 0472dd3..5e8acdc 100644
--- a/libc/sysdeps/linux/common/dup3.c
+++ b/libc/sysdeps/linux/common/dup3.c
@@ -12,4 +12,5 @@
 
 #if defined(__NR_dup3)
 _syscall3(int, dup3, int, oldfd, int, newfd, int, flags)
+libc_hidden_def(dup3)
 #endif


More information about the uClibc-cvs mailing list