Blame m4/dup2.m4

Packit Service 4684c1
#serial 25
Packit Service 4684c1
dnl Copyright (C) 2002, 2005, 2007, 2009-2020 Free Software Foundation, Inc.
Packit Service 4684c1
dnl This file is free software; the Free Software Foundation
Packit Service 4684c1
dnl gives unlimited permission to copy and/or distribute it,
Packit Service 4684c1
dnl with or without modifications, as long as this notice is preserved.
Packit Service 4684c1
Packit Service 4684c1
AC_DEFUN([gl_FUNC_DUP2],
Packit Service 4684c1
[
Packit Service 4684c1
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
Packit Service 4684c1
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit Service 4684c1
  m4_ifdef([gl_FUNC_DUP2_OBSOLETE], [
Packit Service 4684c1
    AC_CHECK_FUNCS_ONCE([dup2])
Packit Service 4684c1
    if test $ac_cv_func_dup2 = no; then
Packit Service 4684c1
      HAVE_DUP2=0
Packit Service 4684c1
    fi
Packit Service 4684c1
  ], [
Packit Service 4684c1
    AC_DEFINE([HAVE_DUP2], [1], [Define to 1 if you have the 'dup2' function.])
Packit Service 4684c1
  ])
Packit Service 4684c1
  if test $HAVE_DUP2 = 1; then
Packit Service 4684c1
    AC_CACHE_CHECK([whether dup2 works], [gl_cv_func_dup2_works],
Packit Service 4684c1
      [AC_RUN_IFELSE([
Packit Service 4684c1
         AC_LANG_PROGRAM(
Packit Service 4684c1
           [[#include <errno.h>
Packit Service 4684c1
             #include <fcntl.h>
Packit Service 4684c1
             #include <limits.h>
Packit Service 4684c1
             #include <sys/resource.h>
Packit Service 4684c1
             #include <unistd.h>
Packit Service 4684c1
             #ifndef RLIM_SAVED_CUR
Packit Service 4684c1
             # define RLIM_SAVED_CUR RLIM_INFINITY
Packit Service 4684c1
             #endif
Packit Service 4684c1
             #ifndef RLIM_SAVED_MAX
Packit Service 4684c1
             # define RLIM_SAVED_MAX RLIM_INFINITY
Packit Service 4684c1
             #endif
Packit Service 4684c1
           ]],
Packit Service 4684c1
           [[int result = 0;
Packit Service 4684c1
             int bad_fd = INT_MAX;
Packit Service 4684c1
             struct rlimit rlim;
Packit Service 4684c1
             if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
Packit Service 4684c1
                 && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
Packit Service 4684c1
                 && rlim.rlim_cur != RLIM_INFINITY
Packit Service 4684c1
                 && rlim.rlim_cur != RLIM_SAVED_MAX
Packit Service 4684c1
                 && rlim.rlim_cur != RLIM_SAVED_CUR)
Packit Service 4684c1
               bad_fd = rlim.rlim_cur;
Packit Service 4684c1
             #ifdef FD_CLOEXEC
Packit Service 4684c1
               if (fcntl (1, F_SETFD, FD_CLOEXEC) == -1)
Packit Service 4684c1
                 result |= 1;
Packit Service 4684c1
             #endif
Packit Service 4684c1
             if (dup2 (1, 1) != 1)
Packit Service 4684c1
               result |= 2;
Packit Service 4684c1
             #ifdef FD_CLOEXEC
Packit Service 4684c1
               if (fcntl (1, F_GETFD) != FD_CLOEXEC)
Packit Service 4684c1
                 result |= 4;
Packit Service 4684c1
             #endif
Packit Service 4684c1
             close (0);
Packit Service 4684c1
             if (dup2 (0, 0) != -1)
Packit Service 4684c1
               result |= 8;
Packit Service 4684c1
             /* Many gnulib modules require POSIX conformance of EBADF.  */
Packit Service 4684c1
             if (dup2 (2, bad_fd) == -1 && errno != EBADF)
Packit Service 4684c1
               result |= 16;
Packit Service 4684c1
             /* Flush out some cygwin core dumps.  */
Packit Service 4684c1
             if (dup2 (2, -1) != -1 || errno != EBADF)
Packit Service 4684c1
               result |= 32;
Packit Service 4684c1
             dup2 (2, 255);
Packit Service 4684c1
             dup2 (2, 256);
Packit Service 4684c1
             /* On OS/2 kLIBC, dup2() does not work on a directory fd.  */
Packit Service 4684c1
             {
Packit Service 4684c1
               int fd = open (".", O_RDONLY);
Packit Service 4684c1
               if (fd == -1)
Packit Service 4684c1
                 result |= 64;
Packit Service 4684c1
               else if (dup2 (fd, fd + 1) == -1)
Packit Service 4684c1
                 result |= 128;
Packit Service 4684c1
Packit Service 4684c1
               close (fd);
Packit Service 4684c1
             }
Packit Service 4684c1
             return result;]])
Packit Service 4684c1
        ],
Packit Service 4684c1
        [gl_cv_func_dup2_works=yes], [gl_cv_func_dup2_works=no],
Packit Service 4684c1
        [case "$host_os" in
Packit Service 4684c1
           mingw*) # on this platform, dup2 always returns 0 for success
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           cygwin*) # on cygwin 1.5.x, dup2(1,1) returns 0
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           aix* | freebsd*)
Packit Service 4684c1
                   # on AIX 7.1 and FreeBSD 6.1, dup2 (1,toobig) gives EMFILE,
Packit Service 4684c1
                   # not EBADF.
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           haiku*) # on Haiku alpha 2, dup2(1, 1) resets FD_CLOEXEC.
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           *-android*) # implemented using dup3(), which fails if oldfd == newfd
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           os2*) # on OS/2 kLIBC, dup2() does not work on a directory fd.
Packit Service 4684c1
             gl_cv_func_dup2_works="guessing no" ;;
Packit Service 4684c1
           *) gl_cv_func_dup2_works="guessing yes" ;;
Packit Service 4684c1
         esac])
Packit Service 4684c1
      ])
Packit Service 4684c1
    case "$gl_cv_func_dup2_works" in
Packit Service 4684c1
      *yes) ;;
Packit Service 4684c1
      *)
Packit Service 4684c1
        REPLACE_DUP2=1
Packit Service 4684c1
        AC_CHECK_FUNCS([setdtablesize])
Packit Service 4684c1
        ;;
Packit Service 4684c1
    esac
Packit Service 4684c1
  fi
Packit Service 4684c1
  dnl Replace dup2() for supporting the gnulib-defined fchdir() function,
Packit Service 4684c1
  dnl to keep fchdir's bookkeeping up-to-date.
Packit Service 4684c1
  m4_ifdef([gl_FUNC_FCHDIR], [
Packit Service 4684c1
    gl_TEST_FCHDIR
Packit Service 4684c1
    if test $HAVE_FCHDIR = 0; then
Packit Service 4684c1
      if test $HAVE_DUP2 = 1; then
Packit Service 4684c1
        REPLACE_DUP2=1
Packit Service 4684c1
      fi
Packit Service 4684c1
    fi
Packit Service 4684c1
  ])
Packit Service 4684c1
])
Packit Service 4684c1
Packit Service 4684c1
# Prerequisites of lib/dup2.c.
Packit Service 4684c1
AC_DEFUN([gl_PREREQ_DUP2], [])