NEWS.stable: log cherry-pick [a5a35f0]->[c4596b1] freopen: Documentation.
[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-2011 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 8
10
11 AC_DEFUN([gl_FUNC_GETCWD_NULL],
12   [
13    AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
14    AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
15      [gl_cv_func_getcwd_null],
16      [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
17 #        include <unistd.h>
18 #        ifndef getcwd
19          char *getcwd ();
20 #        endif
21 ]], [[
22 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
23 /* mingw cwd does not start with '/', but getcwd does allocate.
24    However, mingw fails to honor non-zero size.  */
25 #else
26            if (chdir ("/") != 0)
27              return 1;
28            else
29              {
30                char *f = getcwd (NULL, 0);
31                if (! f)
32                  return 2;
33                if (f[0] != '/')
34                  return 3;
35                if (f[1] != '\0')
36                  return 4;
37                return 0;
38              }
39 #endif
40          ]])],
41         [gl_cv_func_getcwd_null=yes],
42         [gl_cv_func_getcwd_null=no],
43         [[
44        case "$host_os" in
45                                # Guess yes on glibc systems.
46          *-gnu*)               gl_cv_func_getcwd_null="guessing yes";;
47                                # Guess yes on Cygwin.
48          cygwin*)              gl_cv_func_getcwd_null="guessing yes";;
49                                # If we don't know, assume the worst.
50          *)                    gl_cv_func_getcwd_null="guessing no";;
51        esac
52         ]])])
53 ])
54
55 AC_DEFUN([gl_FUNC_GETCWD_SIGNATURE],
56 [
57   AC_CACHE_CHECK([for getcwd with POSIX signature],
58     [gl_cv_func_getcwd_posix_signature],
59     [AC_COMPILE_IFELSE(
60       [AC_LANG_PROGRAM(
61          [[#include <unistd.h>]],
62          [[extern
63            #ifdef __cplusplus
64            "C"
65            #endif
66            char *getcwd (char *, size_t);
67          ]])
68       ],
69       [gl_cv_func_getcwd_posix_signature=yes],
70       [gl_cv_func_getcwd_posix_signature=no])
71    ])
72 ])
73
74 dnl Guarantee that getcwd will malloc with a NULL first argument.  Assumes
75 dnl that either the system getcwd is robust, or that calling code is okay
76 dnl with spurious failures when run from a directory with an absolute name
77 dnl larger than 4k bytes.
78 dnl
79 dnl Assumes that getcwd exists; if you are worried about obsolete
80 dnl platforms that lacked getcwd(), then you need to use the GPL module.
81 AC_DEFUN([gl_FUNC_GETCWD_LGPL],
82 [
83   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
84   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
85   AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
86
87   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature in
88   *yes,yes) ;;
89   *)
90     dnl Minimal replacement lib/getcwd-lgpl.c.
91     REPLACE_GETCWD=1
92     ;;
93   esac
94 ])
95
96 dnl Check for all known getcwd bugs; useful for a program likely to be
97 dnl executed from an arbitrary location.
98 AC_DEFUN([gl_FUNC_GETCWD],
99 [
100   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
101   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
102   AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
103   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
104
105   gl_abort_bug=no
106   case $gl_cv_func_getcwd_null,$host_os in
107   *,mingw*)
108     gl_cv_func_getcwd_path_max=yes;;
109   yes,*)
110     gl_FUNC_GETCWD_PATH_MAX
111     gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes]);;
112   esac
113
114   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature$gl_cv_func_getcwd_path_max,$gl_abort_bug in
115   *yes,yes,yes,no) ;;
116   *)
117     dnl Full replacement lib/getcwd.c, overrides LGPL replacement.
118     REPLACE_GETCWD=1;;
119   esac
120 ])
121
122 # Prerequisites of lib/getcwd.c, when full replacement is in effect.
123 AC_DEFUN([gl_PREREQ_GETCWD],
124 [
125   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
126   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
127   :
128 ])