canonicalize.m4 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # canonicalize.m4 serial 36
  2. dnl Copyright (C) 2003-2007, 2009-2021 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])
  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. mkdir conftest.d
  79. AC_RUN_IFELSE([
  80. AC_LANG_PROGRAM([[
  81. ]GL_NOCRASH[
  82. #include <stdlib.h>
  83. #include <string.h>
  84. ]], [[
  85. int result = 0;
  86. /* This test fails on Solaris 10. */
  87. {
  88. char *name = realpath ("conftest.a", NULL);
  89. if (!(name && *name == '/'))
  90. result |= 1;
  91. free (name);
  92. }
  93. {
  94. char *name = realpath ("conftest.b/../conftest.a", NULL);
  95. if (name != NULL)
  96. result |= 2;
  97. free (name);
  98. }
  99. /* This test fails on Mac OS X 10.13, OpenBSD 6.0. */
  100. {
  101. char *name = realpath ("conftest.a/", NULL);
  102. if (name != NULL)
  103. result |= 4;
  104. free (name);
  105. }
  106. /* This test fails on AIX 7, Solaris 10. */
  107. {
  108. char *name1 = realpath (".", NULL);
  109. char *name2 = realpath ("conftest.d//./..", NULL);
  110. if (! name1 || ! name2 || strcmp (name1, name2))
  111. result |= 8;
  112. free (name1);
  113. free (name2);
  114. }
  115. #ifdef __linux__
  116. /* On Linux, // is the same as /. See also double-slash-root.m4.
  117. realpath() should respect this.
  118. This test fails on musl libc 1.2.2. */
  119. {
  120. char *name = realpath ("//", NULL);
  121. if (! name || strcmp (name, "/"))
  122. result |= 16;
  123. free (name);
  124. }
  125. #endif
  126. return result;
  127. ]])
  128. ],
  129. [gl_cv_func_realpath_works=yes],
  130. [case $? in
  131. 16) gl_cv_func_realpath_works=nearly ;;
  132. *) gl_cv_func_realpath_works=no ;;
  133. esac
  134. ],
  135. [case "$host_os" in
  136. # Guess yes on glibc systems.
  137. *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;;
  138. # Guess 'nearly' on musl systems.
  139. *-musl*) gl_cv_func_realpath_works="guessing nearly" ;;
  140. # Guess no on native Windows.
  141. mingw*) gl_cv_func_realpath_works="guessing no" ;;
  142. # If we don't know, obey --enable-cross-guesses.
  143. *) gl_cv_func_realpath_works="$gl_cross_guess_normal" ;;
  144. esac
  145. ])
  146. rm -rf conftest.a conftest.d
  147. ])
  148. case "$gl_cv_func_realpath_works" in
  149. *yes)
  150. AC_DEFINE([FUNC_REALPATH_WORKS], [1],
  151. [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles leading slashes and a trailing slash correctly.])
  152. ;;
  153. *nearly)
  154. AC_DEFINE([FUNC_REALPATH_NEARLY_WORKS], [1],
  155. [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles a trailing slash correctly.])
  156. ;;
  157. esac
  158. ])