openat: declare in POSIX headers
[gnulib.git] / m4 / openat.m4
1 # serial 20
2 # See if we need to use our replacement for Solaris' openat et al functions.
3
4 dnl Copyright (C) 2004-2009 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
8
9 # Written by Jim Meyering.
10
11 AC_DEFUN([gl_FUNC_OPENAT],
12 [
13   AC_LIBOBJ([openat-proc])
14   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15   AC_CHECK_FUNCS_ONCE([lchmod])
16   AC_REPLACE_FUNCS([fchmodat mkdirat openat])
17   AC_REQUIRE([AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
18   case $ac_cv_func_openat+$ac_cv_func_lstat_dereferences_slashed_symlink in
19   yes+yes) ;;
20   yes+*)
21     AC_LIBOBJ([fstatat])
22     REPLACE_FSTATAT=1
23     ;;
24   *)
25     HAVE_OPENAT=0
26     HAVE_UNLINKAT=0 # No known system with unlinkat but not openat
27     HAVE_FSTATAT=0 # No known system with fstatat but not openat
28     gl_PREREQ_OPENAT;;
29   esac
30   if test $ac_cv_func_fchmodat != yes; then
31     HAVE_FCHMODAT=0
32   fi
33   if test $ac_cv_func_mkdirat != yes; then
34     HAVE_MKDIRAT=0
35   fi
36   gl_FUNC_FCHOWNAT
37   GNULIB_OPENAT=1
38 ])
39
40 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
41 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
42 [
43   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
44     gl_cv_func_fchownat_nofollow_works,
45     [
46      gl_dangle=conftest.dangle
47      # Remove any remnants of a previous test.
48      rm -f $gl_dangle
49      # Arrange for deletion of the temporary file this test creates.
50      ac_clean_files="$ac_clean_files $gl_dangle"
51      ln -s conftest.no-such $gl_dangle
52      AC_RUN_IFELSE(
53        [AC_LANG_SOURCE(
54           [[
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <errno.h>
59 #include <sys/types.h>
60 int
61 main ()
62 {
63   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
64                     AT_SYMLINK_NOFOLLOW) != 0
65           && errno == ENOENT);
66 }
67           ]])],
68     [gl_cv_func_fchownat_nofollow_works=yes],
69     [gl_cv_func_fchownat_nofollow_works=no],
70     [gl_cv_func_fchownat_nofollow_works=no],
71     )
72   ])
73   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
74 ])
75
76 # If we have the fchownat function, and it has the bug (in glibc-2.4)
77 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
78 # use the replacement function.
79 # Also use the replacement function if fchownat is simply not available.
80 AC_DEFUN([gl_FUNC_FCHOWNAT],
81 [
82   AC_CHECK_FUNC([fchownat],
83     [gl_FUNC_FCHOWNAT_DEREF_BUG([REPLACE_FCHOWNAT=1])],
84     [HAVE_FCHOWNAT=0])
85   if test $HAVE_FCHOWNAT = 0 || test $REPLACE_FCHOWNAT = 1; then
86     AC_LIBOBJ([fchownat])
87   fi
88 ])
89
90 AC_DEFUN([gl_PREREQ_OPENAT],
91 [
92   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
93   :
94 ])
95
96 AC_DEFUN([gl_OPENAT_DEFAULTS],
97 [
98   GNULIB_OPENAT=0;            AC_SUBST([GNULIB_OPENAT])
99   dnl Assume proper GNU behavior unless another module says otherwise.
100   HAVE_FCHMODAT=1;            AC_SUBST([HAVE_FCHMODAT])
101   HAVE_FCHOWNAT=1;            AC_SUBST([HAVE_FCHOWNAT])
102   HAVE_FSTATAT=1;             AC_SUBST([HAVE_FSTATAT])
103   HAVE_MKDIRAT=1;             AC_SUBST([HAVE_MKDIRAT])
104   HAVE_OPENAT=1;              AC_SUBST([HAVE_OPENAT])
105   HAVE_UNLINKAT=1;            AC_SUBST([HAVE_UNLINKAT])
106   REPLACE_FCHOWNAT=0;         AC_SUBST([REPLACE_FCHOWNAT])
107   REPLACE_FSTATAT=0;          AC_SUBST([REPLACE_FSTATAT])
108 ])