1# unlink.m4 serial 15
2dnl Copyright (C) 2009-2020 Free Software Foundation, Inc.
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_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         ]GL_MDA_DEFINES],
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 Linux systems.
47         linux-* | linux) gl_cv_func_unlink_honors_slashes="guessing yes" ;;
48                          # Guess yes on glibc systems.
49         *-gnu*)          gl_cv_func_unlink_honors_slashes="guessing yes" ;;
50                          # Guess no on native Windows.
51         mingw*)          gl_cv_func_unlink_honors_slashes="guessing no" ;;
52                          # If we don't know, obey --enable-cross-guesses.
53         *)               gl_cv_func_unlink_honors_slashes="$gl_cross_guess_normal" ;;
54       esac
55      ])
56     rm -f conftest.file conftest.lnk])
57  case "$gl_cv_func_unlink_honors_slashes" in
58    *no)
59      REPLACE_UNLINK=1
60      ;;
61  esac
62
63  dnl Detect Mac OS X 10.5.6 bug: On read-write HFS mounts, unlink("..") or
64  dnl unlink("../..") succeeds without doing anything.
65  AC_CACHE_CHECK([whether unlink of a parent directory fails as it should],
66    [gl_cv_func_unlink_parent_fails],
67    [case "$host_os" in
68       darwin*)
69         dnl Try to unlink a subdirectory of /tmp, because /tmp is usually on a
70         dnl HFS mount on Mac OS X. Use a subdirectory, owned by the current
71         dnl user, because otherwise unlink() may fail due to permissions
72         dnl reasons, and because when running as root we don't want to risk
73         dnl destroying the entire /tmp.
74         if {
75              # Use the mktemp program if available. If not available, hide the error
76              # message.
77              tmp=`(umask 077 && mktemp -d /tmp/gtXXXXXX) 2>/dev/null` &&
78              test -n "$tmp" && test -d "$tmp"
79            } ||
80            {
81              # Use a simple mkdir command. It is guaranteed to fail if the directory
82              # already exists.  $RANDOM is bash specific and expands to empty in shells
83              # other than bash, ksh and zsh.  Its use does not increase security;
84              # rather, it minimizes the probability of failure in a very cluttered /tmp
85              # directory.
86              tmp=/tmp/gt$$-$RANDOM
87              (umask 077 && mkdir "$tmp")
88            }; then
89           mkdir "$tmp/subdir"
90           GL_SUBDIR_FOR_UNLINK="$tmp/subdir"
91           export GL_SUBDIR_FOR_UNLINK
92           AC_RUN_IFELSE(
93             [AC_LANG_SOURCE([[
94                #include <stdlib.h>
95                #if HAVE_UNISTD_H
96                # include <unistd.h>
97                #else /* on Windows with MSVC */
98                # include <direct.h>
99                # include <io.h>
100                #endif
101                ]GL_MDA_DEFINES[
102                int main ()
103                {
104                  int result = 0;
105                  if (chdir (getenv ("GL_SUBDIR_FOR_UNLINK")) != 0)
106                    result |= 1;
107                  else if (unlink ("..") == 0)
108                    result |= 2;
109                  return result;
110                }
111              ]])],
112             [gl_cv_func_unlink_parent_fails=yes],
113             [gl_cv_func_unlink_parent_fails=no],
114             [# If we don't know, obey --enable-cross-guesses.
115              gl_cv_func_unlink_parent_fails="$gl_cross_guess_normal"
116             ])
117           unset GL_SUBDIR_FOR_UNLINK
118           rm -rf "$tmp"
119         else
120           gl_cv_func_unlink_parent_fails="guessing no"
121         fi
122         ;;
123       *)
124         gl_cv_func_unlink_parent_fails="guessing yes"
125         ;;
126     esac
127    ])
128  case "$gl_cv_func_unlink_parent_fails" in
129    *no)
130      REPLACE_UNLINK=1
131      AC_DEFINE([UNLINK_PARENT_BUG], [1],
132        [Define to 1 if unlink() on a parent directory may succeed])
133      ;;
134  esac
135])
136