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