2 dnl Copyright (C) 2002, 2005, 2007, 2009-2011 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.
7 AC_DEFUN([gl_FUNC_DUP2],
9 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10 AC_REQUIRE([AC_CANONICAL_HOST])
11 m4_ifdef([gl_FUNC_DUP2_OBSOLETE], [
12 AC_CHECK_FUNCS_ONCE([dup2])
13 if test $ac_cv_func_dup2 = no; then
18 AC_DEFINE([HAVE_DUP2], [1], [Define to 1 if you have the 'dup2' function.])
20 if test $HAVE_DUP2 = 1; then
21 AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
23 AC_LANG_PROGRAM([[#include <unistd.h>
28 if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
34 if (fcntl (1, F_GETFD) != FD_CLOEXEC)
38 if (dup2 (0, 0) != -1)
40 /* Many gnulib modules require POSIX conformance of EBADF. */
41 if (dup2 (2, 1000000) == -1 && errno != EBADF)
46 [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
48 mingw*) # on this platform, dup2 always returns 0 for success
49 gl_cv_func_dup2_works=no;;
50 cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
51 gl_cv_func_dup2_works=no;;
52 linux*) # On linux between 2008-07-27 and 2009-05-11, dup2 of a
53 # closed fd may yield -EBADF instead of -1 / errno=EBADF.
54 gl_cv_func_dup2_works=no;;
55 freebsd*) # on FreeBSD 6.1, dup2(1,1000000) gives EMFILE, not EBADF.
56 gl_cv_func_dup2_works=no;;
57 haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
58 gl_cv_func_dup2_works=no;;
59 *) gl_cv_func_dup2_works=yes;;
62 if test "$gl_cv_func_dup2_works" = no; then
66 dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
67 dnl to keep fchdir's bookkeeping up-to-date.
68 m4_ifdef([gl_FUNC_FCHDIR], [
70 if test $HAVE_FCHDIR = 0; then
76 AC_DEFUN([gl_REPLACE_DUP2],
78 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
79 AC_CHECK_FUNCS_ONCE([dup2])
80 if test $ac_cv_func_dup2 = yes; then