rename.m4 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # serial 26
  2. # Copyright (C) 2001, 2003, 2005-2006, 2009-2017 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 10, 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 glibc systems.
  55. *-gnu*) gl_cv_func_rename_slash_dst_works="guessing yes" ;;
  56. # If we don't know, assume the worst.
  57. *) gl_cv_func_rename_slash_dst_works="guessing no" ;;
  58. esac
  59. ])
  60. rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
  61. ])
  62. case "$gl_cv_func_rename_slash_dst_works" in
  63. *yes) ;;
  64. *)
  65. REPLACE_RENAME=1
  66. AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
  67. [Define if rename does not correctly handle slashes on the destination
  68. argument, such as on Solaris 10 or NetBSD 1.6.])
  69. ;;
  70. esac
  71. dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
  72. dnl Solaris 9 mistakenly allows rename("file/","name").
  73. dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name").
  74. dnl These bugs require stripping trailing slash to avoid corrupting
  75. dnl symlinks with a trailing slash.
  76. AC_CACHE_CHECK([whether rename honors trailing slash on source],
  77. [gl_cv_func_rename_slash_src_works],
  78. [rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
  79. touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
  80. AC_MSG_ERROR([cannot create temporary files])
  81. # Assume that if we have lstat, we can also check symlinks.
  82. if test $ac_cv_func_lstat = yes; then
  83. ln -s conftest.f conftest.lnk
  84. fi
  85. AC_RUN_IFELSE(
  86. [AC_LANG_PROGRAM([[
  87. # include <stdio.h>
  88. # include <stdlib.h>
  89. ]],
  90. [[int result = 0;
  91. if (rename ("conftest.f1/", "conftest.d3") == 0)
  92. result |= 1;
  93. if (rename ("conftest.d1/", "conftest.d2") != 0)
  94. result |= 2;
  95. #if HAVE_LSTAT
  96. if (rename ("conftest.lnk/", "conftest.f") == 0)
  97. result |= 4;
  98. #endif
  99. return result;
  100. ]])],
  101. [gl_cv_func_rename_slash_src_works=yes],
  102. [gl_cv_func_rename_slash_src_works=no],
  103. dnl When crosscompiling, assume rename is broken.
  104. [case "$host_os" in
  105. # Guess yes on glibc systems.
  106. *-gnu*) gl_cv_func_rename_slash_src_works="guessing yes" ;;
  107. # If we don't know, assume the worst.
  108. *) gl_cv_func_rename_slash_src_works="guessing no" ;;
  109. esac
  110. ])
  111. rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
  112. ])
  113. case "$gl_cv_func_rename_slash_src_works" in
  114. *yes) ;;
  115. *)
  116. REPLACE_RENAME=1
  117. AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
  118. [Define if rename does not correctly handle slashes on the source
  119. argument, such as on Solaris 9 or cygwin 1.5.])
  120. ;;
  121. esac
  122. dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
  123. dnl on rename("h1","h2").
  124. dnl This bug requires stat'ting targets prior to attempting rename.
  125. AC_CHECK_FUNCS_ONCE([link])
  126. AC_CACHE_CHECK([whether rename manages hard links correctly],
  127. [gl_cv_func_rename_link_works],
  128. [if test $ac_cv_func_link = yes; then
  129. rm -rf conftest.f conftest.f1
  130. if touch conftest.f && ln conftest.f conftest.f1 &&
  131. set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
  132. AC_RUN_IFELSE(
  133. [AC_LANG_PROGRAM([[
  134. # include <stdio.h>
  135. # include <stdlib.h>
  136. # include <unistd.h>
  137. ]],
  138. [[int result = 0;
  139. if (rename ("conftest.f", "conftest.f1"))
  140. result |= 1;
  141. if (unlink ("conftest.f1"))
  142. result |= 2;
  143. if (rename ("conftest.f", "conftest.f"))
  144. result |= 4;
  145. if (rename ("conftest.f1", "conftest.f1") == 0)
  146. result |= 8;
  147. return result;
  148. ]])],
  149. [gl_cv_func_rename_link_works=yes],
  150. [gl_cv_func_rename_link_works=no],
  151. dnl When crosscompiling, assume rename is broken.
  152. [case "$host_os" in
  153. # Guess yes on glibc systems.
  154. *-gnu*) gl_cv_func_rename_link_works="guessing yes" ;;
  155. # If we don't know, assume the worst.
  156. *) gl_cv_func_rename_link_works="guessing no" ;;
  157. esac
  158. ])
  159. else
  160. gl_cv_func_rename_link_works="guessing no"
  161. fi
  162. rm -rf conftest.f conftest.f1
  163. else
  164. gl_cv_func_rename_link_works=yes
  165. fi
  166. ])
  167. case "$gl_cv_func_rename_link_works" in
  168. *yes) ;;
  169. *)
  170. REPLACE_RENAME=1
  171. AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
  172. [Define if rename fails to leave hard links alone, as on NetBSD 1.6
  173. or Cygwin 1.5.])
  174. ;;
  175. esac
  176. dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
  177. dnl mingw mistakenly forbids rename("dir1","dir2").
  178. dnl These bugs require stripping trailing slash to avoid corrupting
  179. dnl symlinks with a trailing slash.
  180. AC_CACHE_CHECK([whether rename manages existing destinations correctly],
  181. [gl_cv_func_rename_dest_works],
  182. [rm -rf conftest.f conftest.d1 conftest.d2
  183. touch conftest.f && mkdir conftest.d1 conftest.d2 ||
  184. AC_MSG_ERROR([cannot create temporary files])
  185. AC_RUN_IFELSE(
  186. [AC_LANG_PROGRAM([[
  187. # include <stdio.h>
  188. # include <stdlib.h>
  189. ]],
  190. [[int result = 0;
  191. if (rename ("conftest.d1", "conftest.d2") != 0)
  192. result |= 1;
  193. if (rename ("conftest.d2", "conftest.f") == 0)
  194. result |= 2;
  195. return result;
  196. ]])],
  197. [gl_cv_func_rename_dest_works=yes],
  198. [gl_cv_func_rename_dest_works=no],
  199. dnl When crosscompiling, assume rename is broken.
  200. [case "$host_os" in
  201. # Guess yes on glibc systems.
  202. *-gnu*) gl_cv_func_rename_dest_works="guessing yes" ;;
  203. # If we don't know, assume the worst.
  204. *) gl_cv_func_rename_dest_works="guessing no" ;;
  205. esac
  206. ])
  207. rm -rf conftest.f conftest.d1 conftest.d2
  208. ])
  209. case "$gl_cv_func_rename_dest_works" in
  210. *yes) ;;
  211. *)
  212. REPLACE_RENAME=1
  213. AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
  214. [Define if rename does not work when the destination file exists,
  215. as on Cygwin 1.5 or Windows.])
  216. ;;
  217. esac
  218. ])