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