test-framework-sh: Fix typo in function invocation (regression 2024-06-11).
[gnulib.git] / m4 / canonicalize.m4
blobec7aac470893d35f56b83fc3c9648da5cec5a38b
1 # canonicalize.m4
2 # serial 40
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
22   else
23     case "$gl_cv_func_realpath_works" in
24       *yes) ;;
25       *)    REPLACE_CANONICALIZE_FILE_NAME=1 ;;
26     esac
27   fi
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
38       HAVE_REALPATH=0
39     else
40       case "$gl_cv_func_realpath_works" in
41         *yes) ;;
42         *)    REPLACE_REALPATH=1 ;;
43       esac
44     fi
45   else
46     case "$gl_cv_func_realpath_works" in
47       *yes)
48         ;;
49       *)
50         REPLACE_CANONICALIZE_FILE_NAME=1
51         REPLACE_REALPATH=1
52         ;;
53     esac
54   fi
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])
69   case "$host_os" in
70     mingw* | windows*) ;;
71     *) AC_CHECK_FUNCS([getcwd]) ;;
72   esac
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
81 # so is the latter.
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
88     touch conftest.a
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
92     fi
93     mkdir conftest.d
94     AC_RUN_IFELSE([
95       AC_LANG_PROGRAM([[
96         ]GL_NOCRASH[
97         #include <errno.h>
98         #include <stdlib.h>
99         #include <string.h>
100       ]], [[
101         int result = 0;
102         /* This test fails on Solaris 10.  */
103         {
104           char *name = realpath ("conftest.a", NULL);
105           if (!(name && *name == '/'))
106             result |= 1;
107           free (name);
108         }
109         /* This test fails on older versions of Cygwin.  */
110         {
111           char *name = realpath ("conftest.b/../conftest.a", NULL);
112           if (name != NULL)
113             result |= 2;
114           free (name);
115         }
116         /* This test fails on macOS 14, Cygwin 2.9.  */
117         #if HAVE_LSTAT
118         {
119           char *name = realpath ("conftest.l/../conftest.a", NULL);
120           if (name != NULL || errno != ENOTDIR)
121             result |= 4;
122           free (name);
123         }
124         #endif
125         /* This test fails on macOS 14, OpenBSD 6.0.  */
126         {
127           char *name = realpath ("conftest.a/", NULL);
128           if (name != NULL)
129             result |= 8;
130           free (name);
131         }
132         /* This test fails on AIX 7, Solaris 10.  */
133         {
134           char *name1 = realpath (".", NULL);
135           char *name2 = realpath ("conftest.d//./..", NULL);
136           if (! name1 || ! name2 || strcmp (name1, name2))
137             result |= 16;
138           free (name1);
139           free (name2);
140         }
141         #ifdef __linux__
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.  */
145         {
146           char *name = realpath ("//", NULL);
147           if (! name || strcmp (name, "/"))
148             result |= 32;
149           free (name);
150         }
151         #endif
152         return result;
153       ]])
154      ],
155      [gl_cv_func_realpath_works=yes],
156      [case $? in
157         32) gl_cv_func_realpath_works=nearly ;;
158         *)  gl_cv_func_realpath_works=no ;;
159       esac
160      ],
161      [case "$host_os" in
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" ;;
166                            # Guess no on macOS.
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" ;;
174       esac
175      ])
176     rm -rf conftest.a conftest.l conftest.d
177   ])
178   case "$gl_cv_func_realpath_works" in
179     *yes)
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.])
182       ;;
183     *nearly)
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.])
186       ;;
187   esac