rename.m4 7.2 KB

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