dup2: work around mingw and cygwin 1.5 bug
[gnulib.git] / m4 / dup2.m4
1 #serial 6
2 dnl Copyright (C) 2002, 2005, 2007, 2009 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_DUP2],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_CHECK_FUNCS_ONCE([dup2])
12   if test $ac_cv_func_dup2 = no; then
13     HAVE_DUP2=0
14     AC_LIBOBJ([dup2])
15   else
16     AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
17       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
18 #include <unistd.h>
19 ]], [return 1 - dup2 (1, 1);])],
20         [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
21         [case "$host_os" in
22            mingw*) # on this platform, dup2 always returns 0 for success
23              gl_cv_func_dup2_works=no;;
24            cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
25              gl_cv_func_dup2_works=no;;
26            *) gl_cv_func_dup2_works=yes;;
27          esac])])
28     if test "$gl_cv_func_dup2_works" = no; then
29       REPLACE_DUP2=1
30       AC_LIBOBJ([dup2])
31     fi
32   fi
33   AC_DEFINE_UNQUOTED([REPLACE_DUP2], [$REPLACE_DUP2],
34     [Define to 1 if dup2 returns 0 instead of the target fd.])
35 ])