Import upstream version 1.26
[debian/tar] / m4 / dup2.m4
1 #serial 12
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.
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 fcntl])
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([
18          AC_LANG_PROGRAM([[#include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>]],
21            [int result = 0;
22 #if HAVE_FCNTL
23             if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
24               result |= 1;
25 #endif HAVE_FCNTL
26             if (dup2 (1, 1) == 0)
27               result |= 2;
28 #if HAVE_FCNTL
29             if (fcntl (1, F_GETFD) != FD_CLOEXEC)
30               result |= 4;
31 #endif
32             close (0);
33             if (dup2 (0, 0) != -1)
34               result |= 8;
35             /* Many gnulib modules require POSIX conformance of EBADF.  */
36             if (dup2 (2, 1000000) == -1 && errno != EBADF)
37               result |= 16;
38             return result;
39            ])
40         ],
41         [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
42         [case "$host_os" in
43            mingw*) # on this platform, dup2 always returns 0 for success
44              gl_cv_func_dup2_works=no;;
45            cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
46              gl_cv_func_dup2_works=no;;
47            linux*) # On linux between 2008-07-27 and 2009-05-11, dup2 of a
48                    # closed fd may yield -EBADF instead of -1 / errno=EBADF.
49              gl_cv_func_dup2_works=no;;
50            freebsd*) # on FreeBSD 6.1, dup2(1,1000000) gives EMFILE, not EBADF.
51              gl_cv_func_dup2_works=no;;
52            haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
53              gl_cv_func_dup2_works=no;;
54            *) gl_cv_func_dup2_works=yes;;
55          esac])
56       ])
57     if test "$gl_cv_func_dup2_works" = no; then
58       gl_REPLACE_DUP2
59     fi
60   fi
61 ])
62
63 AC_DEFUN([gl_REPLACE_DUP2],
64 [
65   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
66   if test $ac_cv_func_dup2 = yes; then
67     REPLACE_DUP2=1
68   fi
69   AC_LIBOBJ([dup2])
70 ])