From 90accddebef0cf967e67c9d2412082a361d9f2bd Mon Sep 17 00:00:00 2001 From: Markos Chandras Date: Wed, 10 Oct 2012 16:42:37 +0100 Subject: dup2: Use dup3 if arch does not have the dup2 syscall Signed-off-by: Markos Chandras Signed-off-by: Bernhard Reutner-Fischer --- libc/sysdeps/linux/common/dup2.c | 17 +++++++++++++++++ libc/sysdeps/linux/common/dup3.c | 1 + 2 files changed, 18 insertions(+) (limited to 'libc/sysdeps/linux/common') diff --git a/libc/sysdeps/linux/common/dup2.c b/libc/sysdeps/linux/common/dup2.c index 006f06b69..98ac4e305 100644 --- a/libc/sysdeps/linux/common/dup2.c +++ b/libc/sysdeps/linux/common/dup2.c @@ -9,7 +9,24 @@ #include #include +#if defined __NR_dup3 && !defined __NR_dup2 +# include +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 0472dd3be..5e8acdc8b 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 -- cgit v1.2.3