4 dnl Copyright (C) 2003-2007, 2009-2024 Free Software Foundation, Inc.
6 dnl This file is free software; the Free Software Foundation
7 dnl gives unlimited permission to copy and/or distribute it,
8 dnl with or without modifications, as long as this notice is preserved.
10 # Provides canonicalize_file_name and canonicalize_filename_mode, but does
11 # not provide or fix realpath.
12 AC_DEFUN([gl_FUNC_CANONICALIZE_FILENAME_MODE],
14 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
15 AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
16 AC_CHECK_FUNCS_ONCE([canonicalize_file_name])
17 gl_CHECK_FUNCS_ANDROID([faccessat], [[#include <unistd.h>]])
18 AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
19 AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
20 if test $ac_cv_func_canonicalize_file_name = no; then
21 HAVE_CANONICALIZE_FILE_NAME=0
23 case "$gl_cv_func_realpath_works" in
25 *) REPLACE_CANONICALIZE_FILE_NAME=1 ;;
30 # Provides canonicalize_file_name and realpath.
31 AC_DEFUN([gl_CANONICALIZE_LGPL],
33 AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
34 AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
35 if test $ac_cv_func_canonicalize_file_name = no; then
36 HAVE_CANONICALIZE_FILE_NAME=0
37 if test $ac_cv_func_realpath = no; then
40 case "$gl_cv_func_realpath_works" in
42 *) REPLACE_REALPATH=1 ;;
46 case "$gl_cv_func_realpath_works" in
50 REPLACE_CANONICALIZE_FILE_NAME=1
57 # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
58 # (no REPLACE_CANONICALIZE_FILE_NAME, no REPLACE_REALPATH, no AC_LIBOBJ).
59 AC_DEFUN([gl_CANONICALIZE_LGPL_SEPARATE],
61 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
62 AC_REQUIRE([gl_FUNC_LSTAT_FOLLOWS_SLASHED_SYMLINK])
63 AC_CHECK_FUNCS_ONCE([canonicalize_file_name])
64 gl_CHECK_FUNCS_ANDROID([faccessat], [[#include <unistd.h>]])
66 dnl On native Windows, we use _getcwd(), regardless whether getcwd() is
67 dnl available through the linker option '-loldnames'.
68 AC_REQUIRE([AC_CANONICAL_HOST])
71 *) AC_CHECK_FUNCS([getcwd]) ;;
74 AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
75 AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
76 AC_CHECK_HEADERS_ONCE([sys/param.h])
79 # Check whether realpath works. Assume that if a platform has both
80 # realpath and canonicalize_file_name, but the former is broken, then
82 AC_DEFUN([gl_FUNC_REALPATH_WORKS],
84 AC_CHECK_FUNCS_ONCE([realpath lstat])
85 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
86 AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
87 rm -rf conftest.a conftest.d
89 # Assume that if we have lstat, we can also check symlinks.
90 if test $ac_cv_func_lstat = yes; then
91 ln -s conftest.a conftest.l
102 /* This test fails on Solaris 10. */
104 char *name = realpath ("conftest.a", NULL);
105 if (!(name && *name == '/'))
109 /* This test fails on older versions of Cygwin. */
111 char *name = realpath ("conftest.b/../conftest.a", NULL);
116 /* This test fails on macOS 14, Cygwin 2.9. */
119 char *name = realpath ("conftest.l/../conftest.a", NULL);
120 if (name != NULL || errno != ENOTDIR)
125 /* This test fails on macOS 14, OpenBSD 6.0. */
127 char *name = realpath ("conftest.a/", NULL);
132 /* This test fails on AIX 7, Solaris 10. */
134 char *name1 = realpath (".", NULL);
135 char *name2 = realpath ("conftest.d//./..", NULL);
136 if (! name1 || ! name2 || strcmp (name1, name2))
142 /* On Linux, // is the same as /. See also double-slash-root.m4.
143 realpath() should respect this.
144 This test fails on musl libc 1.2.2. */
146 char *name = realpath ("//", NULL);
147 if (! name || strcmp (name, "/"))
155 [gl_cv_func_realpath_works=yes],
157 32) gl_cv_func_realpath_works=nearly ;;
158 *) gl_cv_func_realpath_works=no ;;
162 # Guess yes on glibc systems.
163 *-gnu* | gnu*) gl_cv_func_realpath_works="guessing yes" ;;
164 # Guess 'nearly' on musl systems.
165 *-musl*) gl_cv_func_realpath_works="guessing nearly" ;;
167 darwin*) gl_cv_func_realpath_works="guessing no" ;;
168 # Guess no on Cygwin.
169 cygwin*) gl_cv_func_realpath_works="guessing no" ;;
170 # Guess no on native Windows.
171 mingw* | windows*) gl_cv_func_realpath_works="guessing no" ;;
172 # If we don't know, obey --enable-cross-guesses.
173 *) gl_cv_func_realpath_works="$gl_cross_guess_normal" ;;
176 rm -rf conftest.a conftest.l conftest.d
178 case "$gl_cv_func_realpath_works" in
180 AC_DEFINE([FUNC_REALPATH_WORKS], [1],
181 [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles leading slashes and a trailing slash correctly.])
184 AC_DEFINE([FUNC_REALPATH_NEARLY_WORKS], [1],
185 [Define to 1 if realpath() can malloc memory, always gives an absolute path, and handles a trailing slash correctly.])