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