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