openat: Ensure autoconf macro ordering.
[pspp] / m4 / openat.m4
1 # serial 31
2 # See if we need to use our replacement for Solaris' openat et al functions.
3
4 dnl Copyright (C) 2004-2010 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_REQUIRE([gl_FCNTL_H_DEFAULTS])
14   GNULIB_OPENAT=1
15
16   AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS])
17   GNULIB_FCHMODAT=1
18   GNULIB_FSTATAT=1
19   GNULIB_MKDIRAT=1
20
21   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
22   GNULIB_FCHOWNAT=1
23   GNULIB_UNLINKAT=1
24
25   AC_LIBOBJ([openat-proc])
26   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
27   AC_CHECK_FUNCS_ONCE([lchmod])
28   AC_REPLACE_FUNCS([fchmodat fstatat mkdirat openat unlinkat])
29   AC_REQUIRE([AC_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
30   AC_REQUIRE([gl_FUNC_UNLINK])
31   case $ac_cv_func_openat+$ac_cv_func_lstat_dereferences_slashed_symlink in
32   yes+yes)
33     # GNU/Hurd has unlinkat, but it has the same bug as unlink.
34     if test $REPLACE_UNLINK = 1; then
35       AC_LIBOBJ([unlinkat])
36       REPLACE_UNLINKAT=1
37     fi ;;
38   yes+*)
39     # Solaris 9 has *at functions, but uniformly mishandles trailing
40     # slash in all of them.
41     AC_LIBOBJ([openat])
42     REPLACE_OPENAT=1
43     AC_LIBOBJ([fstatat])
44     REPLACE_FSTATAT=1
45     AC_LIBOBJ([unlinkat])
46     REPLACE_UNLINKAT=1
47     ;;
48   *)
49     HAVE_OPENAT=0
50     HAVE_UNLINKAT=0 # No known system with unlinkat but not openat
51     HAVE_FSTATAT=0 # No known system with fstatat but not openat
52     gl_PREREQ_OPENAT;;
53   esac
54   if test $ac_cv_func_fchmodat != yes; then
55     HAVE_FCHMODAT=0
56   fi
57   if test $ac_cv_func_mkdirat != yes; then
58     HAVE_MKDIRAT=0
59   fi
60   gl_FUNC_FCHOWNAT
61 ])
62
63 # gl_FUNC_FCHOWNAT_DEREF_BUG([ACTION-IF-BUGGY[, ACTION-IF-NOT_BUGGY]])
64 AC_DEFUN([gl_FUNC_FCHOWNAT_DEREF_BUG],
65 [
66   dnl Persuade glibc's <unistd.h> to declare fchownat().
67   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
68
69   AC_CACHE_CHECK([whether fchownat works with AT_SYMLINK_NOFOLLOW],
70     gl_cv_func_fchownat_nofollow_works,
71     [
72      gl_dangle=conftest.dangle
73      # Remove any remnants of a previous test.
74      rm -f $gl_dangle
75      # Arrange for deletion of the temporary file this test creates.
76      ac_clean_files="$ac_clean_files $gl_dangle"
77      ln -s conftest.no-such $gl_dangle
78      AC_RUN_IFELSE(
79        [AC_LANG_SOURCE(
80           [[
81 #include <fcntl.h>
82 #include <unistd.h>
83 #include <stdlib.h>
84 #include <errno.h>
85 #include <sys/types.h>
86 int
87 main ()
88 {
89   return (fchownat (AT_FDCWD, "$gl_dangle", -1, getgid (),
90                     AT_SYMLINK_NOFOLLOW) != 0
91           && errno == ENOENT);
92 }
93           ]])],
94     [gl_cv_func_fchownat_nofollow_works=yes],
95     [gl_cv_func_fchownat_nofollow_works=no],
96     [gl_cv_func_fchownat_nofollow_works=no],
97     )
98   ])
99   AS_IF([test $gl_cv_func_fchownat_nofollow_works = no], [$1], [$2])
100 ])
101
102 # If we have the fchownat function, and it has the bug (in glibc-2.4)
103 # that it dereferences symlinks even with AT_SYMLINK_NOFOLLOW, then
104 # use the replacement function.
105 # Also if the fchownat function, like chown, has the trailing slash bug,
106 # use the replacement function.
107 # Also use the replacement function if fchownat is simply not available.
108 AC_DEFUN([gl_FUNC_FCHOWNAT],
109 [
110   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
111   AC_REQUIRE([gl_FUNC_CHOWN])
112   AC_CHECK_FUNC([fchownat],
113     [gl_FUNC_FCHOWNAT_DEREF_BUG(
114        [REPLACE_FCHOWNAT=1
115         AC_DEFINE([FCHOWNAT_NOFOLLOW_BUG], [1],
116                   [Define to 1 if your platform has fchownat, but it cannot
117                    perform lchown tasks.])
118        ])
119      if test $REPLACE_CHOWN = 1; then
120        REPLACE_FCHOWNAT=1
121      fi],
122     [HAVE_FCHOWNAT=0])
123   if test $HAVE_FCHOWNAT = 0 || test $REPLACE_FCHOWNAT = 1; then
124     AC_LIBOBJ([fchownat])
125   fi
126 ])
127
128 AC_DEFUN([gl_PREREQ_OPENAT],
129 [
130   AC_REQUIRE([AC_C_INLINE])
131   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
132   :
133 ])