| 1 |
# rename.m4 |
| 2 |
# serial 37 |
| 3 |
dnl Copyright (C) 2001, 2003, 2005-2006, 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 |
dnl From Volker Borchert. |
| 10 |
dnl Determine whether rename works for source file names with a trailing slash. |
| 11 |
dnl The rename from SunOS 4.1.1_U1 doesn't. |
| 12 |
dnl |
| 13 |
dnl If it doesn't, arrange to compile the wrapper function and define |
| 14 |
dnl macros for use by that function. |
| 15 |
dnl |
| 16 |
|
| 17 |
AC_DEFUN([gl_FUNC_RENAME], |
| 18 |
[ |
| 19 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 20 |
AC_REQUIRE([gl_STDIO_H_DEFAULTS]) |
| 21 |
AC_CHECK_FUNCS_ONCE([lstat]) |
| 22 |
|
| 23 |
dnl Solaris 11.3, AIX 7.1 mistakenly allow rename("file","name/"). |
| 24 |
dnl NetBSD 1.6 mistakenly forbids rename("dir","name/"). |
| 25 |
dnl FreeBSD 7.2 mistakenly allows rename("file","link-to-file/"). |
| 26 |
dnl The Solaris bug can be worked around without stripping |
| 27 |
dnl trailing slash, while the NetBSD bug requires stripping; |
| 28 |
dnl the two conditions can be distinguished by whether hard |
| 29 |
dnl links are also broken. |
| 30 |
AC_CACHE_CHECK([whether rename honors trailing slash on destination], |
| 31 |
[gl_cv_func_rename_slash_dst_works], |
| 32 |
[rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk |
| 33 |
touch conftest.f && touch conftest.f1 && mkdir conftest.d1 || |
| 34 |
AC_MSG_ERROR([cannot create temporary files]) |
| 35 |
# Assume that if we have lstat, we can also check symlinks. |
| 36 |
if test $ac_cv_func_lstat = yes; then |
| 37 |
ln -s conftest.f conftest.lnk |
| 38 |
fi |
| 39 |
AC_RUN_IFELSE( |
| 40 |
[AC_LANG_PROGRAM([[ |
| 41 |
# include <stdio.h> |
| 42 |
# include <stdlib.h> |
| 43 |
]], |
| 44 |
[[int result = 0; |
| 45 |
if (rename ("conftest.f1", "conftest.f2/") == 0) |
| 46 |
result |= 1; |
| 47 |
if (rename ("conftest.d1", "conftest.d2/") != 0) |
| 48 |
result |= 2; |
| 49 |
#if HAVE_LSTAT |
| 50 |
if (rename ("conftest.f", "conftest.lnk/") == 0) |
| 51 |
result |= 4; |
| 52 |
#endif |
| 53 |
return result; |
| 54 |
]])], |
| 55 |
[gl_cv_func_rename_slash_dst_works=yes], |
| 56 |
[gl_cv_func_rename_slash_dst_works=no], |
| 57 |
dnl When crosscompiling, assume rename is broken. |
| 58 |
[case "$host_os" in |
| 59 |
# Guess yes on Linux systems |
| 60 |
# and on systems that emulate the Linux system calls. |
| 61 |
linux* | midipix*) gl_cv_func_rename_slash_dst_works="guessing yes" ;; |
| 62 |
# Guess yes on glibc systems. |
| 63 |
*-gnu*) gl_cv_func_rename_slash_dst_works="guessing yes" ;; |
| 64 |
# Guess no on native Windows. |
| 65 |
mingw* | windows*) gl_cv_func_rename_slash_dst_works="guessing no" ;; |
| 66 |
# If we don't know, obey --enable-cross-guesses. |
| 67 |
*) gl_cv_func_rename_slash_dst_works="$gl_cross_guess_normal" ;; |
| 68 |
esac |
| 69 |
]) |
| 70 |
rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk |
| 71 |
]) |
| 72 |
case "$gl_cv_func_rename_slash_dst_works" in |
| 73 |
*yes) ;; |
| 74 |
*) |
| 75 |
REPLACE_RENAME=1 |
| 76 |
AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1], |
| 77 |
[Define if rename does not correctly handle slashes on the destination |
| 78 |
argument, such as on Solaris 11 or NetBSD 1.6.]) |
| 79 |
;; |
| 80 |
esac |
| 81 |
|
| 82 |
dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name"). |
| 83 |
dnl Solaris 9 mistakenly allows rename("file/","name"). |
| 84 |
dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name"). |
| 85 |
dnl These bugs require stripping trailing slash to avoid corrupting |
| 86 |
dnl symlinks with a trailing slash. |
| 87 |
AC_CACHE_CHECK([whether rename honors trailing slash on source], |
| 88 |
[gl_cv_func_rename_slash_src_works], |
| 89 |
[rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk |
| 90 |
touch conftest.f && touch conftest.f1 && mkdir conftest.d1 || |
| 91 |
AC_MSG_ERROR([cannot create temporary files]) |
| 92 |
# Assume that if we have lstat, we can also check symlinks. |
| 93 |
if test $ac_cv_func_lstat = yes; then |
| 94 |
ln -s conftest.f conftest.lnk |
| 95 |
fi |
| 96 |
AC_RUN_IFELSE( |
| 97 |
[AC_LANG_PROGRAM([[ |
| 98 |
# include <stdio.h> |
| 99 |
# include <stdlib.h> |
| 100 |
]], |
| 101 |
[[int result = 0; |
| 102 |
if (rename ("conftest.f1/", "conftest.d3") == 0) |
| 103 |
result |= 1; |
| 104 |
if (rename ("conftest.d1/", "conftest.d2") != 0) |
| 105 |
result |= 2; |
| 106 |
#if HAVE_LSTAT |
| 107 |
if (rename ("conftest.lnk/", "conftest.f") == 0) |
| 108 |
result |= 4; |
| 109 |
#endif |
| 110 |
return result; |
| 111 |
]])], |
| 112 |
[gl_cv_func_rename_slash_src_works=yes], |
| 113 |
[gl_cv_func_rename_slash_src_works=no], |
| 114 |
dnl When crosscompiling, assume rename is broken. |
| 115 |
[case "$host_os" in |
| 116 |
# Guess yes on Linux systems |
| 117 |
# and on systems that emulate the Linux system calls. |
| 118 |
linux* | midipix*) gl_cv_func_rename_slash_src_works="guessing yes" ;; |
| 119 |
# Guess yes on glibc systems. |
| 120 |
*-gnu*) gl_cv_func_rename_slash_src_works="guessing yes" ;; |
| 121 |
# Guess yes on native Windows. |
| 122 |
mingw* | windows*) gl_cv_func_rename_slash_src_works="guessing yes" ;; |
| 123 |
# If we don't know, obey --enable-cross-guesses. |
| 124 |
*) gl_cv_func_rename_slash_src_works="$gl_cross_guess_normal" ;; |
| 125 |
esac |
| 126 |
]) |
| 127 |
rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk |
| 128 |
]) |
| 129 |
case "$gl_cv_func_rename_slash_src_works" in |
| 130 |
*yes) ;; |
| 131 |
*) |
| 132 |
REPLACE_RENAME=1 |
| 133 |
AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1], |
| 134 |
[Define if rename does not correctly handle slashes on the source |
| 135 |
argument, such as on Solaris 9 or cygwin 1.5.]) |
| 136 |
;; |
| 137 |
esac |
| 138 |
|
| 139 |
dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count |
| 140 |
dnl on rename("h1","h2"). |
| 141 |
dnl This bug requires stat'ting targets prior to attempting rename. |
| 142 |
AC_CHECK_FUNCS_ONCE([link]) |
| 143 |
AC_CACHE_CHECK([whether rename manages hard links correctly], |
| 144 |
[gl_cv_func_rename_link_works], |
| 145 |
[if test $ac_cv_func_link = yes; then |
| 146 |
if test $cross_compiling != yes; then |
| 147 |
rm -rf conftest.f conftest.f1 conftest.f2 |
| 148 |
if touch conftest.f conftest.f2 && ln conftest.f conftest.f1 && |
| 149 |
set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then |
| 150 |
AC_RUN_IFELSE( |
| 151 |
[AC_LANG_PROGRAM([[ |
| 152 |
# include <errno.h> |
| 153 |
# include <stdio.h> |
| 154 |
# include <stdlib.h> |
| 155 |
# include <unistd.h> |
| 156 |
]GL_MDA_DEFINES], |
| 157 |
[[int result = 0; |
| 158 |
if (rename ("conftest.f", "conftest.f1")) |
| 159 |
result |= 1; |
| 160 |
if (unlink ("conftest.f1")) |
| 161 |
result |= 2; |
| 162 |
|
| 163 |
/* Allow either the POSIX-required behavior, where the |
| 164 |
previous rename kept conftest.f, or the (better) NetBSD |
| 165 |
behavior, where it removed conftest.f. */ |
| 166 |
if (rename ("conftest.f", "conftest.f") != 0 |
| 167 |
&& errno != ENOENT) |
| 168 |
result |= 4; |
| 169 |
|
| 170 |
if (rename ("conftest.f1", "conftest.f1") == 0) |
| 171 |
result |= 8; |
| 172 |
if (rename ("conftest.f2", "conftest.f2") != 0) |
| 173 |
result |= 16; |
| 174 |
return result; |
| 175 |
]])], |
| 176 |
[gl_cv_func_rename_link_works=yes], |
| 177 |
[gl_cv_func_rename_link_works=no], |
| 178 |
[dnl We don't get here. |
| 179 |
: |
| 180 |
]) |
| 181 |
else |
| 182 |
gl_cv_func_rename_link_works="guessing no" |
| 183 |
fi |
| 184 |
rm -rf conftest.f conftest.f1 conftest.f2 |
| 185 |
else |
| 186 |
dnl When crosscompiling, assume rename is broken. |
| 187 |
case "$host_os" in |
| 188 |
# Guess yes on Linux systems |
| 189 |
# and on systems that emulate the Linux system calls. |
| 190 |
linux* | midipix*) gl_cv_func_rename_link_works="guessing yes" ;; |
| 191 |
# Guess yes on glibc systems. |
| 192 |
*-gnu*) gl_cv_func_rename_link_works="guessing yes" ;; |
| 193 |
# Guess yes on native Windows. |
| 194 |
mingw* | windows*) gl_cv_func_rename_link_works="guessing yes" ;; |
| 195 |
# If we don't know, obey --enable-cross-guesses. |
| 196 |
*) gl_cv_func_rename_link_works="$gl_cross_guess_normal" ;; |
| 197 |
esac |
| 198 |
fi |
| 199 |
else |
| 200 |
gl_cv_func_rename_link_works=yes |
| 201 |
fi |
| 202 |
]) |
| 203 |
case "$gl_cv_func_rename_link_works" in |
| 204 |
*yes) ;; |
| 205 |
*) |
| 206 |
REPLACE_RENAME=1 |
| 207 |
AC_DEFINE([RENAME_HARD_LINK_BUG], [1], |
| 208 |
[Define if rename fails to leave hard links alone, as on NetBSD 1.6 |
| 209 |
or Cygwin 1.5.]) |
| 210 |
;; |
| 211 |
esac |
| 212 |
|
| 213 |
dnl Cygwin 1.5.x mistakenly allows rename("dir","file"). |
| 214 |
dnl mingw mistakenly forbids rename("dir1","dir2"). |
| 215 |
dnl These bugs require stripping trailing slash to avoid corrupting |
| 216 |
dnl symlinks with a trailing slash. |
| 217 |
AC_CACHE_CHECK([whether rename manages existing destinations correctly], |
| 218 |
[gl_cv_func_rename_dest_works], |
| 219 |
[rm -rf conftest.f conftest.d1 conftest.d2 |
| 220 |
touch conftest.f && mkdir conftest.d1 conftest.d2 || |
| 221 |
AC_MSG_ERROR([cannot create temporary files]) |
| 222 |
AC_RUN_IFELSE( |
| 223 |
[AC_LANG_PROGRAM([[ |
| 224 |
# include <stdio.h> |
| 225 |
# include <stdlib.h> |
| 226 |
]], |
| 227 |
[[int result = 0; |
| 228 |
if (rename ("conftest.d1", "conftest.d2") != 0) |
| 229 |
result |= 1; |
| 230 |
if (rename ("conftest.d2", "conftest.f") == 0) |
| 231 |
result |= 2; |
| 232 |
return result; |
| 233 |
]])], |
| 234 |
[gl_cv_func_rename_dest_works=yes], |
| 235 |
[gl_cv_func_rename_dest_works=no], |
| 236 |
dnl When crosscompiling, assume rename is broken. |
| 237 |
[case "$host_os" in |
| 238 |
# Guess yes on Linux systems |
| 239 |
# and on systems that emulate the Linux system calls. |
| 240 |
linux* | midipix*) gl_cv_func_rename_dest_works="guessing yes" ;; |
| 241 |
# Guess yes on glibc systems. |
| 242 |
*-gnu*) gl_cv_func_rename_dest_works="guessing yes" ;; |
| 243 |
# Guess no on native Windows. |
| 244 |
mingw* | windows*) gl_cv_func_rename_dest_works="guessing no" ;; |
| 245 |
# If we don't know, obey --enable-cross-guesses. |
| 246 |
*) gl_cv_func_rename_dest_works="$gl_cross_guess_normal" ;; |
| 247 |
esac |
| 248 |
]) |
| 249 |
rm -rf conftest.f conftest.d1 conftest.d2 |
| 250 |
]) |
| 251 |
case "$gl_cv_func_rename_dest_works" in |
| 252 |
*yes) ;; |
| 253 |
*) |
| 254 |
REPLACE_RENAME=1 |
| 255 |
AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1], |
| 256 |
[Define if rename does not work when the destination file exists, |
| 257 |
as on Cygwin 1.5 or Windows.]) |
| 258 |
;; |
| 259 |
esac |
| 260 |
]) |