2 # Check for several getcwd bugs with long paths.
3 # If so, arrange to compile the wrapper function.
5 # This is necessary for at least GNU libc on linux-2.4.19 and 2.4.20.
6 # I've heard that this is due to a Linux kernel bug, and that it has
7 # been fixed between 2.4.21-pre3 and 2.4.21-pre4. */
9 # Copyright (C) 2003, 2004 Free Software Foundation, Inc.
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2, or (at your option)
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software Foundation,
23 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 AC_DEFUN([gl_FUNC_GETCWD_PATH_MAX],
29 AC_CHECK_DECLS_ONCE(getcwd)
30 AC_CHECK_HEADERS_ONCE(fcntl.h)
31 AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
32 AC_CACHE_CHECK([whether getcwd handles long paths properly],
33 gl_cv_func_getcwd_path_max,
35 # Arrange for deletion of the temporary directory this test creates.
36 ac_clean_files="$ac_clean_files confdir3"
37 AC_RUN_IFELSE([AC_LANG_SOURCE([[
44 #include <sys/types.h>
53 # define is_ENAMETOOLONG(x) ((x) == ENAMETOOLONG)
55 # define is_ENAMETOOLONG(x) 0
58 /* Don't get link errors because mkdir is redefined to rpl_mkdir. */
65 /* The length of this name must be 8. */
66 #define DIR_NAME "confdir3"
67 #define DIR_NAME_LEN 8
68 #define DIR_NAME_SIZE (DIR_NAME_LEN + 1)
70 /* The length of "../". */
71 #define DOTDOTSLASH_LEN 3
73 /* Leftover bytes in the buffer, to work around library or OS bugs. */
80 /* The Hurd doesn't define this, so getcwd can't exhibit the bug --
81 at least not on a local file system. And if we were to start worrying
82 about remote file systems, we'd have to enable the wrapper function
83 all of the time, just to be safe. That's not worth the cost. */
85 #elif ((INT_MAX / (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1) \
86 - DIR_NAME_SIZE - BUF_SLOP) \
88 /* FIXME: Assuming there's a system for which this is true,
89 this should be done in a compile test. */
92 char buf[PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN + 1)
93 + DIR_NAME_SIZE + BUF_SLOP];
94 char *cwd = getcwd (buf, PATH_MAX);
95 size_t initial_cwd_len;
103 cwd_len = initial_cwd_len = strlen (cwd);
107 size_t dotdot_max = PATH_MAX * (DIR_NAME_SIZE / DOTDOTSLASH_LEN);
110 cwd_len += DIR_NAME_SIZE;
111 /* If mkdir or chdir fails, be pessimistic and consider that
112 as a failure, too. */
113 if (mkdir (DIR_NAME, S_IRWXU) < 0 || chdir (DIR_NAME) < 0)
119 if (PATH_MAX <= cwd_len && cwd_len < PATH_MAX + DIR_NAME_SIZE)
121 c = getcwd (buf, PATH_MAX);
122 if (c || errno != ERANGE)
129 if (dotdot_max <= cwd_len - initial_cwd_len)
131 if (dotdot_max + DIR_NAME_SIZE < cwd_len - initial_cwd_len)
133 c = getcwd (buf, cwd_len + 1);
134 if (!c && (AT_FDCWD || !is_ENAMETOOLONG (errno)))
141 if (c && strlen (c) != cwd_len)
149 /* Leaving behind such a deep directory is not polite.
150 So clean up here, right away, even though the driving
151 shell script would also clean up. */
155 /* Unlink first, in case the chdir failed. */
157 for (i = 0; i <= n_chdirs; i++)
159 if (chdir ("..") < 0)
169 [gl_cv_func_getcwd_path_max=yes],
170 [gl_cv_func_getcwd_path_max=no],
171 [gl_cv_func_getcwd_path_max=no])])