1 # getcwd.m4 - check for working getcwd that is compatible with glibc
3 # Copyright (C) 2001, 2003-2007, 2009-2021 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
8 # Written by Paul Eggert.
11 AC_DEFUN([gl_FUNC_GETCWD_NULL],
13 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14 AC_CHECK_HEADERS_ONCE([unistd.h])
15 AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
16 [gl_cv_func_getcwd_null],
17 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
21 # else /* on Windows with MSVC */
29 #if defined _WIN32 && ! defined __CYGWIN__
30 /* mingw cwd does not start with '/', but _getcwd does allocate.
31 However, mingw fails to honor non-zero size. */
37 char *f = getcwd (NULL, 0);
41 { free (f); return 3; }
43 { free (f); return 4; }
49 [gl_cv_func_getcwd_null=yes],
50 [gl_cv_func_getcwd_null=no],
52 # Guess yes on glibc systems.
53 *-gnu* | gnu*) gl_cv_func_getcwd_null="guessing yes";;
54 # Guess yes on musl systems.
55 *-musl*) gl_cv_func_getcwd_null="guessing yes";;
56 # Guess yes on Cygwin.
57 cygwin*) gl_cv_func_getcwd_null="guessing yes";;
58 # If we don't know, obey --enable-cross-guesses.
59 *) gl_cv_func_getcwd_null="$gl_cross_guess_normal";;
64 AC_DEFUN([gl_FUNC_GETCWD_SIGNATURE],
66 AC_CACHE_CHECK([for getcwd with POSIX signature],
67 [gl_cv_func_getcwd_posix_signature],
76 char *getcwd (char *, size_t);
79 [gl_cv_func_getcwd_posix_signature=yes],
80 [gl_cv_func_getcwd_posix_signature=no])
84 dnl Guarantee that getcwd will malloc with a NULL first argument. Assumes
85 dnl that either the system getcwd is robust, or that calling code is okay
86 dnl with spurious failures when run from a directory with an absolute name
87 dnl larger than 4k bytes.
89 dnl Assumes that getcwd exists; if you are worried about obsolete
90 dnl platforms that lacked getcwd(), then you need to use the GPL module.
91 AC_DEFUN([gl_FUNC_GETCWD_LGPL],
93 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
94 AC_REQUIRE([gl_FUNC_GETCWD_NULL])
95 AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
97 case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature in
100 dnl Minimal replacement lib/getcwd-lgpl.c.
106 dnl Check for all known getcwd bugs; useful for a program likely to be
107 dnl executed from an arbitrary location.
108 AC_DEFUN([gl_FUNC_GETCWD],
110 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
111 AC_REQUIRE([gl_FUNC_GETCWD_NULL])
112 AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
113 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
118 gl_cv_func_getcwd_path_max=yes
121 gl_FUNC_GETCWD_PATH_MAX
122 case "$gl_cv_func_getcwd_null" in
124 gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes])
129 dnl Define HAVE_MINIMALLY_WORKING_GETCWD and HAVE_PARTLY_WORKING_GETCWD
131 case "$gl_cv_func_getcwd_path_max" in
132 *"no" | *"no, it has the AIX bug") ;;
134 AC_DEFINE([HAVE_MINIMALLY_WORKING_GETCWD], [1],
135 [Define to 1 if getcwd minimally works, that is, its result can be
136 trusted when it succeeds.])
139 case "$gl_cv_func_getcwd_path_max" in
140 *"no, but it is partly working")
141 AC_DEFINE([HAVE_PARTLY_WORKING_GETCWD], [1],
142 [Define to 1 if getcwd works, except it sometimes fails when it
143 shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.])
145 *"yes, but with shorter paths")
146 AC_DEFINE([HAVE_GETCWD_SHORTER], [1],
147 [Define to 1 if getcwd works, but with shorter paths
148 than is generally tested with the replacement.])
152 if { case "$gl_cv_func_getcwd_null" in *yes) false;; *) true;; esac; } \
153 || test $gl_cv_func_getcwd_posix_signature != yes \
154 || { case "$gl_cv_func_getcwd_path_max" in *yes*) false;; *) true;; esac; } \
155 || test $gl_abort_bug = yes; then
160 # Prerequisites of lib/getcwd.c, when full replacement is in effect.
161 AC_DEFUN([gl_PREREQ_GETCWD],
163 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
164 AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])