# HG changeset patch # User Bruno Haible # Date 1316804100 -7200 # Node ID 6cad6a3713b831981aaf1ee5378d39948d1504da # Parent dcb82e84f6fe1e5235bc19f8287d8e7d32b4e518 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. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2011-09-23 Bruno Haible + + 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. + 2011-09-23 Bruno Haible msvc-inval: Fix compilation error. diff --git a/lib/dup2.c b/lib/dup2.c --- 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 @@ 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. diff --git a/m4/dup2.m4 b/m4/dup2.m4 --- a/m4/dup2.m4 +++ b/m4/dup2.m4 @@ -1,4 +1,4 @@ -#serial 14 +#serial 15 dnl Copyright (C) 2002, 2005, 2007, 2009-2011 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -71,3 +71,9 @@ fi ]) ]) + +# Prerequisites of lib/dup2.c. +AC_DEFUN([gl_PREREQ_DUP2]. +[ + AC_REQUIRE([AC_C_INLINE]) +]) diff --git a/modules/dup2 b/modules/dup2 --- a/modules/dup2 +++ b/modules/dup2 @@ -14,6 +14,7 @@ gl_FUNC_DUP2 if test $HAVE_DUP2 = 0 || test $REPLACE_DUP2 = 1; then AC_LIBOBJ([dup2]) + gl_PREREQ_DUP2 fi gl_UNISTD_MODULE_INDICATOR([dup2])