dup2.m4 2.9 KB

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