getcwd.m4 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # getcwd.m4 - check for working getcwd that is compatible with glibc
  2. # Copyright (C) 2001, 2003-2007, 2009-2021 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. # Written by Paul Eggert.
  7. # serial 19
  8. AC_DEFUN([gl_FUNC_GETCWD_NULL],
  9. [
  10. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  11. AC_CHECK_HEADERS_ONCE([unistd.h])
  12. AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
  13. [gl_cv_func_getcwd_null],
  14. [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
  15. # include <stdlib.h>
  16. # if HAVE_UNISTD_H
  17. # include <unistd.h>
  18. # else /* on Windows with MSVC */
  19. # include <direct.h>
  20. # endif
  21. ]GL_MDA_DEFINES[
  22. # ifndef getcwd
  23. char *getcwd ();
  24. # endif
  25. ]], [[
  26. #if defined _WIN32 && ! defined __CYGWIN__
  27. /* mingw cwd does not start with '/', but _getcwd does allocate.
  28. However, mingw fails to honor non-zero size. */
  29. #else
  30. if (chdir ("/") != 0)
  31. return 1;
  32. else
  33. {
  34. char *f = getcwd (NULL, 0);
  35. if (! f)
  36. return 2;
  37. if (f[0] != '/')
  38. { free (f); return 3; }
  39. if (f[1] != '\0')
  40. { free (f); return 4; }
  41. free (f);
  42. return 0;
  43. }
  44. #endif
  45. ]])],
  46. [gl_cv_func_getcwd_null=yes],
  47. [gl_cv_func_getcwd_null=no],
  48. [[case "$host_os" in
  49. # Guess yes on glibc systems.
  50. *-gnu* | gnu*) gl_cv_func_getcwd_null="guessing yes";;
  51. # Guess yes on musl systems.
  52. *-musl*) gl_cv_func_getcwd_null="guessing yes";;
  53. # Guess yes on Cygwin.
  54. cygwin*) gl_cv_func_getcwd_null="guessing yes";;
  55. # If we don't know, obey --enable-cross-guesses.
  56. *) gl_cv_func_getcwd_null="$gl_cross_guess_normal";;
  57. esac
  58. ]])])
  59. ])
  60. AC_DEFUN([gl_FUNC_GETCWD_SIGNATURE],
  61. [
  62. AC_CACHE_CHECK([for getcwd with POSIX signature],
  63. [gl_cv_func_getcwd_posix_signature],
  64. [AC_COMPILE_IFELSE(
  65. [AC_LANG_PROGRAM(
  66. [[#include <unistd.h>
  67. ]GL_MDA_DEFINES],
  68. [[extern
  69. #ifdef __cplusplus
  70. "C"
  71. #endif
  72. char *getcwd (char *, size_t);
  73. ]])
  74. ],
  75. [gl_cv_func_getcwd_posix_signature=yes],
  76. [gl_cv_func_getcwd_posix_signature=no])
  77. ])
  78. ])
  79. dnl Guarantee that getcwd will malloc with a NULL first argument. Assumes
  80. dnl that either the system getcwd is robust, or that calling code is okay
  81. dnl with spurious failures when run from a directory with an absolute name
  82. dnl larger than 4k bytes.
  83. dnl
  84. dnl Assumes that getcwd exists; if you are worried about obsolete
  85. dnl platforms that lacked getcwd(), then you need to use the GPL module.
  86. AC_DEFUN([gl_FUNC_GETCWD_LGPL],
  87. [
  88. AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
  89. AC_REQUIRE([gl_FUNC_GETCWD_NULL])
  90. AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
  91. case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature in
  92. *yes,yes) ;;
  93. *)
  94. dnl Minimal replacement lib/getcwd-lgpl.c.
  95. REPLACE_GETCWD=1
  96. ;;
  97. esac
  98. ])
  99. dnl Check for all known getcwd bugs; useful for a program likely to be
  100. dnl executed from an arbitrary location.
  101. AC_DEFUN([gl_FUNC_GETCWD],
  102. [
  103. AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
  104. AC_REQUIRE([gl_FUNC_GETCWD_NULL])
  105. AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
  106. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  107. gl_abort_bug=no
  108. case "$host_os" in
  109. mingw*)
  110. gl_cv_func_getcwd_path_max=yes
  111. ;;
  112. *)
  113. gl_FUNC_GETCWD_PATH_MAX
  114. case "$gl_cv_func_getcwd_null" in
  115. *yes)
  116. gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes])
  117. ;;
  118. esac
  119. ;;
  120. esac
  121. dnl Define HAVE_MINIMALLY_WORKING_GETCWD and HAVE_PARTLY_WORKING_GETCWD
  122. dnl if appropriate.
  123. case "$gl_cv_func_getcwd_path_max" in
  124. *"no" | *"no, it has the AIX bug") ;;
  125. *)
  126. AC_DEFINE([HAVE_MINIMALLY_WORKING_GETCWD], [1],
  127. [Define to 1 if getcwd minimally works, that is, its result can be
  128. trusted when it succeeds.])
  129. ;;
  130. esac
  131. case "$gl_cv_func_getcwd_path_max" in
  132. *"no, but it is partly working")
  133. AC_DEFINE([HAVE_PARTLY_WORKING_GETCWD], [1],
  134. [Define to 1 if getcwd works, except it sometimes fails when it
  135. shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.])
  136. ;;
  137. *"yes, but with shorter paths")
  138. AC_DEFINE([HAVE_GETCWD_SHORTER], [1],
  139. [Define to 1 if getcwd works, but with shorter paths
  140. than is generally tested with the replacement.])
  141. ;;
  142. esac
  143. if { case "$gl_cv_func_getcwd_null" in *yes) false;; *) true;; esac; } \
  144. || test $gl_cv_func_getcwd_posix_signature != yes \
  145. || { case "$gl_cv_func_getcwd_path_max" in *yes*) false;; *) true;; esac; } \
  146. || test $gl_abort_bug = yes; then
  147. REPLACE_GETCWD=1
  148. fi
  149. ])
  150. # Prerequisites of lib/getcwd.c, when full replacement is in effect.
  151. AC_DEFUN([gl_PREREQ_GETCWD],
  152. [
  153. AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
  154. AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
  155. :
  156. ])