Debian Codegraph @

m4/getcwd.m4

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