canonicalize.m4 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # canonicalize.m4 serial 37
  2. dnl Copyright (C) 2003-2007, 2009-2023 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. # Provides canonicalize_file_name and canonicalize_filename_mode, but does
  7. # not provide or fix realpath.
  8. AC_DEFUN([gl_FUNC_CANONICALIZE_FILENAME_MODE],
  9. [
  10. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
  11. AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
  12. AC_CHECK_FUNCS_ONCE([canonicalize_file_name faccessat])
  13. AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
  14. AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
  15. if test $ac_cv_func_canonicalize_file_name = no; then
  16. HAVE_CANONICALIZE_FILE_NAME=0
  17. else
  18. case "$gl_cv_func_realpath_works" in
  19. *yes) ;;
  20. *) REPLACE_CANONICALIZE_FILE_NAME=1 ;;
  21. esac
  22. fi
  23. ])
  24. # Provides canonicalize_file_name and realpath.
  25. AC_DEFUN([gl_CANONICALIZE_LGPL],
  26. [
  27. AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
  28. AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
  29. if test $ac_cv_func_canonicalize_file_name = no; then
  30. HAVE_CANONICALIZE_FILE_NAME=0
  31. if test $ac_cv_func_realpath = no; then
  32. HAVE_REALPATH=0
  33. else
  34. case "$gl_cv_func_realpath_works" in
  35. *yes) ;;
  36. *) REPLACE_REALPATH=1 ;;
  37. esac
  38. fi
  39. else
  40. case "$gl_cv_func_realpath_works" in
  41. *yes)
  42. ;;
  43. *)
  44. REPLACE_CANONICALIZE_FILE_NAME=1
  45. REPLACE_REALPATH=1
  46. ;;
  47. esac
  48. fi
  49. ])
  50. # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
  51. # (no REPLACE_CANONICALIZE_FILE_NAME, no REPLACE_REALPATH, no AC_LIBOBJ).
  52. AC_DEFUN([gl_CANONICALIZE_LGPL_SEPARATE],
  53. [
  54. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
  55. AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
  56. AC_CHECK_FUNCS_ONCE([canonicalize_file_name faccessat])
  57. dnl On native Windows, we use _getcwd(), regardless whether getcwd() is
  58. dnl available through the linker option '-loldnames'.
  59. AC_REQUIRE([AC_CANONICAL_HOST])
  60. case "$host_os" in
  61. mingw*) ;;
  62. *) AC_CHECK_FUNCS([getcwd]) ;;
  63. esac
  64. AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
  65. AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
  66. AC_CHECK_HEADERS_ONCE([sys/param.h])
  67. ])
  68. # Check whether realpath works. Assume that if a platform has both
  69. # realpath and canonicalize_file_name, but the former is broken, then
  70. # so is the latter.
  71. AC_DEFUN([gl_FUNC_REALPATH_WORKS],
  72. [
  73. AC_CHECK_FUNCS_ONCE([realpath lstat])
  74. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  75. AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
  76. rm -rf conftest.a conftest.d
  77. touch conftest.a
  78. # Assume that if we have lstat, we can also check symlinks.
  79. if test $ac_cv_func_lstat = yes; then
  80. ln -s conftest.a conftest.l
  81. fi
  82. mkdir conftest.d
  83. AC_RUN_IFELSE([
  84. AC_LANG_PROGRAM([[
  85. ]GL_NOCRASH[
  86. #include <errno.h>
  87. #include <stdlib.h>
  88. #include <string.h>
  89. ]], [[
  90. int result = 0;
  91. /* This test fails on Solaris 10. */
  92. {
  93. char *name = realpath ("conftest.a", NULL);
  94. if (!(name && *name == '/'))
  95. result |= 1;
  96. free (name);
  97. }
  98. /* This test fails on older versions of Cygwin. */
  99. {
  100. char *name = realpath ("conftest.b/../conftest.a", NULL);
  101. if (name != NULL)
  102. result |= 2;
  103. free (name);
  104. }
  105. /* This test fails on Cygwin 2.9. */
  106. #if HAVE_LSTAT
  107. {
  108. char *name = realpath ("conftest.l/../conftest.a", NULL);
  109. if (name != NULL || errno != ENOTDIR)
  110. result |= 4;
  111. free (name);
  112. }
  113. #endif
  114. /* This test fails on Mac OS X 10.13, OpenBSD 6.0. */
  115. {
  116. char *name = realpath ("conftest.a/", NULL);
  117. if (name != NULL)
  118. result |= 8;
  119. free (name);
  120. }
  121. /* This test fails on AIX 7, Solaris 10. */
  122. {
  123. char *name1 = realpath (".", NULL);
  124. char *name2 = realpath ("conftest.d//./..", NULL);
  125. if (! name1 || ! name2 || strcmp (name1, name2))
  126. result |= 16;
  127. free (name1);
  128. free (name2);
  129. }
  130. #ifdef __linux__
  131. /* On Linux, // is the same as /. See also double-slash-root.m4.
  132. realpath() should respect this.
  133. This test fails on musl libc 1.2.2. */
  134. {
  135. char *name = realpath ("//", NULL);
  136. if (! name || strcmp (name, "/"))
  137. result |= 32;
  138. free (name);
  139. }
  140. #endif
  141. return result;
  142. ]])
  143. ],
  144. [gl_cv_func_realpath_works=yes],
  145. [case $? in
  146. 32) gl_cv_func_realpath_works=nearly ;;
  147. *) gl_cv_func_realpath_works=no ;;
  148. esac
  149. ],
  150. [case "$host_os" in
  151. # Guess yes on glibc systems.
  152. *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;;
  153. # Guess 'nearly' on musl systems.
  154. *-musl*) gl_cv_func_realpath_works="guessing nearly" ;;
  155. # Guess no on Cygwin.
  156. cygwin*) gl_cv_func_realpath_works="guessing no" ;;
  157. # Guess no on native Windows.
  158. mingw*) gl_cv_func_realpath_works="guessing no" ;;
  159. # If we don't know, obey --enable-cross-guesses.
  160. *) gl_cv_func_realpath_works="$gl_cross_guess_normal" ;;
  161. esac
  162. ])
  163. rm -rf conftest.a conftest.l conftest.d
  164. ])
  165. case "$gl_cv_func_realpath_works" in
  166. *yes)
  167. AC_DEFINE([FUNC_REALPATH_WORKS], [1],
  168. [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles leading slashes and a trailing slash correctly.])
  169. ;;
  170. *nearly)
  171. AC_DEFINE([FUNC_REALPATH_NEARLY_WORKS], [1],
  172. [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles a trailing slash correctly.])
  173. ;;
  174. esac
  175. ])