Backport the :end-of-capability fix
[emacs.git] / m4 / dup2.m4
blobbae6d01cd53cf9c97af6a2f34fbc9c8ef38d248d
1 #serial 20
2 dnl Copyright (C) 2002, 2005, 2007, 2009-2015 Free Software Foundation,
3 dnl Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_DUP2],
10   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
11   AC_REQUIRE([AC_CANONICAL_HOST])
12   AC_CHECK_FUNCS_ONCE([getdtablesize])
13   m4_ifdef([gl_FUNC_DUP2_OBSOLETE], [
14     AC_CHECK_FUNCS_ONCE([dup2])
15     if test $ac_cv_func_dup2 = no; then
16       HAVE_DUP2=0
17     fi
18   ], [
19     AC_DEFINE([HAVE_DUP2], [1], [Define to 1 if you have the 'dup2' function.])
20   ])
21   if test $HAVE_DUP2 = 1; then
22     AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
23       [AC_RUN_IFELSE([
24          AC_LANG_PROGRAM([[#include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>]],
27            [int result = 0;
28 #ifdef HAVE_GETDTABLESIZE
29             int bad_fd = getdtablesize ();
30 #else
31             int bad_fd = 1000000;
32 #endif
33 #ifdef FD_CLOEXEC
34             if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
35               result |= 1;
36 #endif
37             if (dup2 (1, 1) == 0)
38               result |= 2;
39 #ifdef FD_CLOEXEC
40             if (fcntl (1, F_GETFD) != FD_CLOEXEC)
41               result |= 4;
42 #endif
43             close (0);
44             if (dup2 (0, 0) != -1)
45               result |= 8;
46             /* Many gnulib modules require POSIX conformance of EBADF.  */
47             if (dup2 (2, bad_fd) == -1 && errno != EBADF)
48               result |= 16;
49             /* Flush out some cygwin core dumps.  */
50             if (dup2 (2, -1) != -1 || errno != EBADF)
51               result |= 32;
52             dup2 (2, 255);
53             dup2 (2, 256);
54             return result;
55            ])
56         ],
57         [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
58         [case "$host_os" in
59            mingw*) # on this platform, dup2 always returns 0 for success
60              gl_cv_func_dup2_works="guessing no" ;;
61            cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
62              gl_cv_func_dup2_works="guessing no" ;;
63            linux*) # On linux between 2008-07-27 and 2009-05-11, dup2 of a
64                    # closed fd may yield -EBADF instead of -1 / errno=EBADF.
65              gl_cv_func_dup2_works="guessing no" ;;
66            aix* | freebsd*)
67                    # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
68                    # not EBADF.
69              gl_cv_func_dup2_works="guessing no" ;;
70            haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
71              gl_cv_func_dup2_works="guessing no" ;;
72            *) gl_cv_func_dup2_works="guessing yes" ;;
73          esac])
74       ])
75     case "$gl_cv_func_dup2_works" in
76       *yes) ;;
77       *)
78         REPLACE_DUP2=1
79         AC_CHECK_FUNCS([setdtablesize])
80         ;;
81     esac
82   fi
83   dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
84   dnl to keep fchdir's bookkeeping up-to-date.
85   m4_ifdef([gl_FUNC_FCHDIR], [
86     gl_TEST_FCHDIR
87     if test $HAVE_FCHDIR = 0; then
88       if test $HAVE_DUP2 = 1; then
89         REPLACE_DUP2=1
90       fi
91     fi
92   ])
95 # Prerequisites of lib/dup2.c.
96 AC_DEFUN([gl_PREREQ_DUP2], [])