test-getcwd.c: avoid new set-but-not-used warning
[gnulib.git] / m4 / getcwd-abort-bug.m4
1 # serial 4
2 # Determine whether getcwd aborts when the length of the working directory
3 # name is unusually large.  Any length between 4k and 16k trigger the bug
4 # when using glibc-2.4.90-9 or older.
5
6 # Copyright (C) 2006, 2009-2011 Free Software Foundation, Inc.
7 # This file is free software; the Free Software Foundation
8 # gives unlimited permission to copy and/or distribute it,
9 # with or without modifications, as long as this notice is preserved.
10
11 # From Jim Meyering
12
13 # gl_FUNC_GETCWD_ABORT_BUG([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
14 AC_DEFUN([gl_FUNC_GETCWD_ABORT_BUG],
15 [
16   AC_CHECK_DECLS_ONCE([getcwd])
17   AC_CHECK_FUNCS([getpagesize])
18   AC_CACHE_CHECK([whether getcwd aborts when 4k < cwd_length < 16k],
19     gl_cv_func_getcwd_abort_bug,
20     [# Remove any remnants of a previous test.
21      rm -rf confdir-14B---
22      # Arrange for deletion of the temporary directory this test creates.
23      ac_clean_files="$ac_clean_files confdir-14B---"
24      dnl Please keep this in sync with tests/test-getcwd.c.
25      AC_RUN_IFELSE(
26        [AC_LANG_SOURCE(
27           [[
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <sys/stat.h>
33
34 /* Don't get link errors because mkdir is redefined to rpl_mkdir.  */
35 #undef mkdir
36
37 #ifndef S_IRWXU
38 # define S_IRWXU 0700
39 #endif
40
41 /* FIXME: skip the run-test altogether on systems without getpagesize.  */
42 #if ! HAVE_GETPAGESIZE
43 # define getpagesize() 0
44 #endif
45
46 /* This size is chosen to be larger than PATH_MAX (4k), yet smaller than
47    the 16kB pagesize on ia64 linux.  Those conditions make the code below
48    trigger a bug in glibc's getcwd implementation before 2.4.90-10.  */
49 #define TARGET_LEN (5 * 1024)
50
51 int
52 main ()
53 {
54   char const *dir_name = "confdir-14B---";
55   char *cwd;
56   size_t initial_cwd_len;
57   int fail = 0;
58   size_t desired_depth;
59   size_t d;
60
61   /* The bug is triggered when PATH_MAX < getpagesize (), so skip
62      this relatively expensive and invasive test if that's not true.  */
63   if (getpagesize () <= PATH_MAX)
64     return 0;
65
66   cwd = getcwd (NULL, 0);
67   if (cwd == NULL)
68     return 2;
69
70   initial_cwd_len = strlen (cwd);
71   free (cwd);
72   desired_depth = ((TARGET_LEN - 1 - initial_cwd_len)
73                    / (1 + strlen (dir_name)));
74   for (d = 0; d < desired_depth; d++)
75     {
76       if (mkdir (dir_name, S_IRWXU) < 0 || chdir (dir_name) < 0)
77         {
78           fail = 3; /* Unable to construct deep hierarchy.  */
79           break;
80         }
81     }
82
83   /* If libc has the bug in question, this invocation of getcwd
84      results in a failed assertion.  */
85   cwd = getcwd (NULL, 0);
86   if (cwd == NULL)
87     fail = 4; /* getcwd failed.  This is ok, and expected.  */
88   free (cwd);
89
90   /* Call rmdir first, in case the above chdir failed.  */
91   rmdir (dir_name);
92   while (0 < d--)
93     {
94       if (chdir ("..") < 0)
95         {
96           fail = 5;
97           break;
98         }
99       rmdir (dir_name);
100     }
101
102   return fail;
103 }
104           ]])],
105     [gl_cv_func_getcwd_abort_bug=no],
106     dnl A "regular" nonzero return does not indicate this bug.
107     dnl An abort will provoke an exit code of something like 134 (128 + 6).
108     [test $? -gt 128 \
109       && gl_cv_func_getcwd_abort_bug=yes \
110       || gl_cv_func_getcwd_abort_bug=no],
111     [gl_cv_func_getcwd_abort_bug=yes])
112   ])
113   AS_IF([test $gl_cv_func_getcwd_abort_bug = yes], [$1], [$2])
114 ])