summaryrefslogtreecommitdiff
path: root/lib/dup2.c
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2011-09-23 20:55:00 +0200
committerBruno Haible <bruno@clisp.org>2011-09-23 20:55:00 +0200
commitfeb7da5e51db75738e91cce135c1906f7dc10948 (patch)
tree9de740f4f714829d6ae7fba33a570870ed9e1a9f /lib/dup2.c
parent27ba0d3030c9932731e6b55549733ae0090a1b62 (diff)
downloadgnulib-feb7da5e51db75738e91cce135c1906f7dc10948.tar.gz
dup2: Make code more maintainable.
* lib/dup2.c (dup2_nothrow): New function, extracted from rpl_dup2. (rpl_dup2): Use it. * m4/dup2.m4 (gl_PREREQ_DUP2): New macro. * modules/dup2 (configure.ac): Invoke it. Reported by Paul Eggert.
Diffstat (limited to 'lib/dup2.c')
-rw-r--r--lib/dup2.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/dup2.c b/lib/dup2.c
index 234bdfe50b..b8798f3f0a 100644
--- a/lib/dup2.c
+++ b/lib/dup2.c
@@ -37,6 +37,29 @@
# undef dup2
+# if HAVE_MSVC_INVALID_PARAMETER_HANDLER
+static inline int
+dup2_nothrow (int fd, int desired_fd)
+{
+ int result;
+
+ TRY_MSVC_INVAL
+ {
+ result = dup2 (fd, desired_fd);
+ }
+ CATCH_MSVC_INVAL
+ {
+ result = -1;
+ errno = EBADF;
+ }
+ DONE_MSVC_INVAL;
+
+ return result;
+}
+# else
+# define dup2_nothrow dup2
+# endif
+
int
rpl_dup2 (int fd, int desired_fd)
{
@@ -79,16 +102,7 @@ rpl_dup2 (int fd, int desired_fd)
return fcntl (fd, F_GETFL) == -1 ? -1 : fd;
# endif
- TRY_MSVC_INVAL
- {
- result = dup2 (fd, desired_fd);
- }
- CATCH_MSVC_INVAL
- {
- result = -1;
- errno = EBADF;
- }
- DONE_MSVC_INVAL;
+ result = dup2_nothrow (fd, desired_fd);
# ifdef __linux__
/* Correct a Linux return value.