free-posix: New module, renamed from 'free'.
[gnulib.git] / m4 / fchmodat.m4
blobcf5c87999c5e907c713a3121acad4731a2228851
1 # fchmodat.m4 serial 5
2 dnl Copyright (C) 2004-2020 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.
7 # Written by Jim Meyering.
9 AC_DEFUN([gl_FUNC_FCHMODAT],
11   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
12   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
13   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14   AC_CHECK_FUNCS_ONCE([fchmodat lchmod])
15   if test $ac_cv_func_fchmodat != yes; then
16     HAVE_FCHMODAT=0
17   else
18     AC_CACHE_CHECK(
19       [whether fchmodat+AT_SYMLINK_NOFOLLOW works on non-symlinks],
20       [gl_cv_func_fchmodat_works],
21       [dnl This test fails on GNU/Linux with glibc 2.31 (but not on
22        dnl GNU/kFreeBSD nor GNU/Hurd) and Cygwin 2.9.
23        AC_RUN_IFELSE(
24          [AC_LANG_PROGRAM(
25             [
26               AC_INCLUDES_DEFAULT[
27               #include <fcntl.h>
28               #ifndef S_IRUSR
29                #define S_IRUSR 0400
30               #endif
31               #ifndef S_IWUSR
32                #define S_IWUSR 0200
33               #endif
34               #ifndef S_IRWXU
35                #define S_IRWXU 0700
36               #endif
37               #ifndef S_IRWXG
38                #define S_IRWXG 0070
39               #endif
40               #ifndef S_IRWXO
41                #define S_IRWXO 0007
42               #endif
43             ]GL_MDA_DEFINES],
44             [[
45               int permissive = S_IRWXU | S_IRWXG | S_IRWXO;
46               int desired = S_IRUSR | S_IWUSR;
47               static char const f[] = "conftest.fchmodat";
48               struct stat st;
49               if (creat (f, permissive) < 0)
50                 return 1;
51               if (fchmodat (AT_FDCWD, f, desired, AT_SYMLINK_NOFOLLOW) != 0)
52                 return 1;
53               if (stat (f, &st) != 0)
54                 return 1;
55               return ! ((st.st_mode & permissive) == desired);
56             ]])],
57          [gl_cv_func_fchmodat_works=yes],
58          [gl_cv_func_fchmodat_works=no],
59          [case "$host_os" in
60             dnl Guess no on Linux with glibc and Cygwin, yes otherwise.
61             linux-gnu* | cygwin*) gl_cv_func_fchmodat_works="guessing no" ;;
62             *)                    gl_cv_func_fchmodat_works="$gl_cross_guess_normal" ;;
63           esac
64          ])
65        rm -f conftest.fchmodat])
66     case $gl_cv_func_fchmodat_works in
67       *yes) ;;
68       *)
69         AC_DEFINE([NEED_FCHMODAT_NONSYMLINK_FIX], [1],
70           [Define to 1 if fchmodat+AT_SYMLINK_NOFOLLOW does not work right on non-symlinks.])
71         REPLACE_FCHMODAT=1
72         ;;
73     esac
74   fi
77 # Prerequisites of lib/fchmodat.c.
78 AC_DEFUN([gl_PREREQ_FCHMODAT],
80   AC_CHECK_FUNCS_ONCE([lchmod])
81   :