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