4adf76238b21cb64d406e9cdcd0b68243910407f
[gnulib.git] / m4 / unlink.m4
1 # unlink.m4 serial 9
2 dnl Copyright (C) 2009-2012 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.
6
7 AC_DEFUN([gl_FUNC_UNLINK],
8 [
9   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
10   AC_REQUIRE([AC_CANONICAL_HOST])
11   AC_CHECK_HEADERS_ONCE([unistd.h])
12   dnl Detect FreeBSD 7.2, AIX 7.1, Solaris 9 bug.
13   AC_CACHE_CHECK([whether unlink honors trailing slashes],
14     [gl_cv_func_unlink_honors_slashes],
15     [touch conftest.file
16      # Assume that if we have lstat, we can also check symlinks.
17      if test $ac_cv_func_lstat = yes; then
18        ln -s conftest.file conftest.lnk
19      fi
20      AC_RUN_IFELSE(
21        [AC_LANG_PROGRAM(
22          [[#if HAVE_UNISTD_H
23            # include <unistd.h>
24            #else /* on Windows with MSVC */
25            # include <io.h>
26            #endif
27            #include <errno.h>
28          ]],
29          [[int result = 0;
30            if (!unlink ("conftest.file/"))
31              result |= 1;
32            else if (errno != ENOTDIR)
33              result |= 2;
34 #if HAVE_LSTAT
35            if (!unlink ("conftest.lnk/"))
36              result |= 4;
37            else if (errno != ENOTDIR)
38              result |= 8;
39 #endif
40            return result;
41          ]])],
42       [gl_cv_func_unlink_honors_slashes=yes],
43       [gl_cv_func_unlink_honors_slashes=no],
44       [gl_cv_func_unlink_honors_slashes="guessing no"])
45      rm -f conftest.file conftest.lnk])
46   dnl Detect MacOS X 10.5.6 bug: On read-write HFS mounts, unlink("..") or
47   dnl unlink("../..") succeeds without doing anything.
48   AC_CACHE_CHECK([whether unlink of a parent directory fails as it should],
49     [gl_cv_func_unlink_parent_fails],
50     [case "$host_os" in
51        darwin*)
52          dnl Try to unlink a subdirectory of /tmp, because /tmp is usually on a
53          dnl HFS mount on MacOS X. Use a subdirectory, owned by the current
54          dnl user, because otherwise unlink() may fail due to permissions
55          dnl reasons, and because when running as root we don't want to risk
56          dnl destroying the entire /tmp.
57          if {
58               # Use the mktemp program if available. If not available, hide the error
59               # message.
60               tmp=`(umask 077 && mktemp -d /tmp/gtXXXXXX) 2>/dev/null` &&
61               test -n "$tmp" && test -d "$tmp"
62             } ||
63             {
64               # Use a simple mkdir command. It is guaranteed to fail if the directory
65               # already exists.  $RANDOM is bash specific and expands to empty in shells
66               # other than bash, ksh and zsh.  Its use does not increase security;
67               # rather, it minimizes the probability of failure in a very cluttered /tmp
68               # directory.
69               tmp=/tmp/gt$$-$RANDOM
70               (umask 077 && mkdir "$tmp")
71             }; then
72            mkdir "$tmp/subdir"
73            GL_SUBDIR_FOR_UNLINK="$tmp/subdir"
74            export GL_SUBDIR_FOR_UNLINK
75            AC_RUN_IFELSE(
76              [AC_LANG_SOURCE([[
77                 #include <stdlib.h>
78                 #if HAVE_UNISTD_H
79                 # include <unistd.h>
80                 #else /* on Windows with MSVC */
81                 # include <direct.h>
82                 # include <io.h>
83                 #endif
84                 int main ()
85                 {
86                   int result = 0;
87                   if (chdir (getenv ("GL_SUBDIR_FOR_UNLINK")) != 0)
88                     result |= 1;
89                   else if (unlink ("..") == 0)
90                     result |= 2;
91                   return result;
92                 }
93               ]])],
94              [gl_cv_func_unlink_parent_fails=yes],
95              [gl_cv_func_unlink_parent_fails=no],
96              [gl_cv_func_unlink_parent_fails="guessing no"])
97            unset GL_SUBDIR_FOR_UNLINK
98            rm -rf "$tmp"
99          else
100            gl_cv_func_unlink_parent_fails="guessing no"
101          fi
102          ;;
103        *)
104          gl_cv_func_unlink_parent_fails="guessing yes"
105          ;;
106      esac
107     ])
108   case "$gl_cv_func_unlink_parent_fails" in
109     *no)
110       AC_DEFINE([UNLINK_PARENT_BUG], [1],
111         [Define to 1 if unlink() on a parent directory may succeed])
112       ;;
113   esac
114   if test "$gl_cv_func_unlink_honors_slashes" != yes \
115      || { case "$gl_cv_func_unlink_parent_fails" in
116             *yes) false;;
117             *no) true;;
118           esac
119         }; then
120     REPLACE_UNLINK=1
121   fi
122 ])