]> code.delx.au - gnu-emacs/blob - m4/dup2.m4
Merge from gnulib
[gnu-emacs] / m4 / dup2.m4
1 #serial 22
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.
6
7 AC_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
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(
23 [[#include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <sys/resource.h>
27 #include <unistd.h>
28 ]],
29 [[int result = 0;
30 int bad_fd = INT_MAX;
31 struct rlimit rlim;
32 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
33 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
34 && rlim.rlim_cur != RLIM_INFINITY
35 && rlim.rlim_cur != RLIM_SAVED_MAX
36 && rlim.rlim_cur != RLIM_SAVED_CUR)
37 bad_fd = rlim.rlim_cur;
38 #ifdef FD_CLOEXEC
39 if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
40 result |= 1;
41 #endif
42 if (dup2 (1, 1) == 0)
43 result |= 2;
44 #ifdef FD_CLOEXEC
45 if (fcntl (1, F_GETFD) != FD_CLOEXEC)
46 result |= 4;
47 #endif
48 close (0);
49 if (dup2 (0, 0) != -1)
50 result |= 8;
51 /* Many gnulib modules require POSIX conformance of EBADF. */
52 if (dup2 (2, bad_fd) == -1 && errno != EBADF)
53 result |= 16;
54 /* Flush out some cygwin core dumps. */
55 if (dup2 (2, -1) != -1 || errno != EBADF)
56 result |= 32;
57 dup2 (2, 255);
58 dup2 (2, 256);
59 return result;]])
60 ],
61 [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
62 [case "$host_os" in
63 mingw*) # on this platform, dup2 always returns 0 for success
64 gl_cv_func_dup2_works="guessing no" ;;
65 cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
66 gl_cv_func_dup2_works="guessing no" ;;
67 aix* | freebsd*)
68 # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
69 # not EBADF.
70 gl_cv_func_dup2_works="guessing no" ;;
71 haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
72 gl_cv_func_dup2_works="guessing no" ;;
73 *) gl_cv_func_dup2_works="guessing yes" ;;
74 esac])
75 ])
76 case "$gl_cv_func_dup2_works" in
77 *yes) ;;
78 *)
79 REPLACE_DUP2=1
80 AC_CHECK_FUNCS([setdtablesize])
81 ;;
82 esac
83 fi
84 dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
85 dnl to keep fchdir's bookkeeping up-to-date.
86 m4_ifdef([gl_FUNC_FCHDIR], [
87 gl_TEST_FCHDIR
88 if test $HAVE_FCHDIR = 0; then
89 if test $HAVE_DUP2 = 1; then
90 REPLACE_DUP2=1
91 fi
92 fi
93 ])
94 ])
95
96 # Prerequisites of lib/dup2.c.
97 AC_DEFUN([gl_PREREQ_DUP2], [])