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