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