2 dnl Copyright (C) 2002, 2005, 2007, 2009-2015 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
17 AC_DEFINE([HAVE_DUP2], [1], [Define to 1 if you have the 'dup2' function.])
19 if test $HAVE_DUP2 = 1; then
20 AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
26 #include <sys/resource.h>
28 #ifndef RLIM_SAVED_CUR
29 # define RLIM_SAVED_CUR RLIM_INFINITY
31 #ifndef RLIM_SAVED_MAX
32 # define RLIM_SAVED_MAX RLIM_INFINITY
38 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
39 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
40 && rlim.rlim_cur != RLIM_INFINITY
41 && rlim.rlim_cur != RLIM_SAVED_MAX
42 && rlim.rlim_cur != RLIM_SAVED_CUR)
43 bad_fd = rlim.rlim_cur;
45 if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
51 if (fcntl (1, F_GETFD) != FD_CLOEXEC)
55 if (dup2 (0, 0) != -1)
57 /* Many gnulib modules require POSIX conformance of EBADF. */
58 if (dup2 (2, bad_fd) == -1 && errno != EBADF)
60 /* Flush out some cygwin core dumps. */
61 if (dup2 (2, -1) != -1 || errno != EBADF)
67 [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
69 mingw*) # on this platform, dup2 always returns 0 for success
70 gl_cv_func_dup2_works="guessing no" ;;
71 cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
72 gl_cv_func_dup2_works="guessing no" ;;
74 # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
76 gl_cv_func_dup2_works="guessing no" ;;
77 haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
78 gl_cv_func_dup2_works="guessing no" ;;
79 *-android*) # implemented using dup3(), which fails if oldfd == newfd
80 gl_cv_func_dup2_works="guessing no" ;;
81 *) gl_cv_func_dup2_works="guessing yes" ;;
84 case "$gl_cv_func_dup2_works" in
88 AC_CHECK_FUNCS([setdtablesize])
92 dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
93 dnl to keep fchdir's bookkeeping up-to-date.
94 m4_ifdef([gl_FUNC_FCHDIR], [
96 if test $HAVE_FCHDIR = 0; then
97 if test $HAVE_DUP2 = 1; then
104 # Prerequisites of lib/dup2.c.
105 AC_DEFUN([gl_PREREQ_DUP2], [])