1*56bb7041Schristos# fchdir.m4 serial 25
2*56bb7041Schristosdnl Copyright (C) 2006-2020 Free Software Foundation, Inc.
3*56bb7041Schristosdnl This file is free software; the Free Software Foundation
4*56bb7041Schristosdnl gives unlimited permission to copy and/or distribute it,
5*56bb7041Schristosdnl with or without modifications, as long as this notice is preserved.
6*56bb7041Schristos
7*56bb7041SchristosAC_DEFUN([gl_FUNC_FCHDIR],
8*56bb7041Schristos[
9*56bb7041Schristos  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10*56bb7041Schristos  AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
11*56bb7041Schristos  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12*56bb7041Schristos
13*56bb7041Schristos  AC_CHECK_DECLS_ONCE([fchdir])
14*56bb7041Schristos  if test $ac_cv_have_decl_fchdir = no; then
15*56bb7041Schristos    HAVE_DECL_FCHDIR=0
16*56bb7041Schristos  fi
17*56bb7041Schristos
18*56bb7041Schristos  AC_REQUIRE([gl_TEST_FCHDIR])
19*56bb7041Schristos  if test $HAVE_FCHDIR = 0; then
20*56bb7041Schristos    AC_LIBOBJ([fchdir])
21*56bb7041Schristos    gl_PREREQ_FCHDIR
22*56bb7041Schristos    AC_DEFINE([REPLACE_FCHDIR], [1],
23*56bb7041Schristos      [Define to 1 if gnulib's fchdir() replacement is used.])
24*56bb7041Schristos    dnl We must also replace anything that can manipulate a directory fd,
25*56bb7041Schristos    dnl to keep our bookkeeping up-to-date.  We don't have to replace
26*56bb7041Schristos    dnl fstatat, since no platform has fstatat but lacks fchdir.
27*56bb7041Schristos    AC_CACHE_CHECK([whether open can visit directories],
28*56bb7041Schristos      [gl_cv_func_open_directory_works],
29*56bb7041Schristos      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <fcntl.h>
30*56bb7041Schristos]], [return open(".", O_RDONLY) < 0;])],
31*56bb7041Schristos        [gl_cv_func_open_directory_works=yes],
32*56bb7041Schristos        [gl_cv_func_open_directory_works=no],
33*56bb7041Schristos        [case "$host_os" in
34*56bb7041Schristos                            # Guess yes on Linux systems.
35*56bb7041Schristos           linux-* | linux) gl_cv_func_open_directory_works="guessing yes" ;;
36*56bb7041Schristos                            # Guess yes on glibc systems.
37*56bb7041Schristos           *-gnu* | gnu*)   gl_cv_func_open_directory_works="guessing yes" ;;
38*56bb7041Schristos                            # Guess no on native Windows.
39*56bb7041Schristos           mingw*)          gl_cv_func_open_directory_works="guessing no" ;;
40*56bb7041Schristos                            # If we don't know, obey --enable-cross-guesses.
41*56bb7041Schristos           *)               gl_cv_func_open_directory_works="$gl_cross_guess_normal" ;;
42*56bb7041Schristos         esac
43*56bb7041Schristos        ])])
44*56bb7041Schristos    case "$gl_cv_func_open_directory_works" in
45*56bb7041Schristos      *yes) ;;
46*56bb7041Schristos      *)
47*56bb7041Schristos        AC_DEFINE([REPLACE_OPEN_DIRECTORY], [1], [Define to 1 if open() should
48*56bb7041Schristoswork around the inability to open a directory.])
49*56bb7041Schristos        ;;
50*56bb7041Schristos    esac
51*56bb7041Schristos  fi
52*56bb7041Schristos])
53*56bb7041Schristos
54*56bb7041Schristos# Determine whether to use the overrides in lib/fchdir.c.
55*56bb7041SchristosAC_DEFUN([gl_TEST_FCHDIR],
56*56bb7041Schristos[
57*56bb7041Schristos  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
58*56bb7041Schristos  AC_CHECK_FUNCS_ONCE([fchdir])
59*56bb7041Schristos  if test $ac_cv_func_fchdir = no; then
60*56bb7041Schristos    HAVE_FCHDIR=0
61*56bb7041Schristos  fi
62*56bb7041Schristos])
63*56bb7041Schristos
64*56bb7041Schristos# Prerequisites of lib/fchdir.c.
65*56bb7041SchristosAC_DEFUN([gl_PREREQ_FCHDIR], [:])
66