rename.m4 10 KB

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