getcwd: fix mingw bugs
[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 6
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          /* If size is non-zero, allocation must fail if size is too small */
41          if (getcwd (NULL, 1))
42            return 5;
43          ]])],
44         [gl_cv_func_getcwd_null=yes],
45         [gl_cv_func_getcwd_null=no],
46         [[
47        case "$host_os" in
48                                # Guess yes on glibc systems.
49          *-gnu*)               gl_cv_func_getcwd_null="guessing yes";;
50                                # Guess yes on Cygwin.
51          cygwin*)              gl_cv_func_getcwd_null="guessing yes";;
52                                # If we don't know, assume the worst.
53          *)                    gl_cv_func_getcwd_null="guessing no";;
54        esac
55         ]])])
56 ])
57
58
59 dnl Guarantee that getcwd will malloc with a NULL first argument.  Assumes
60 dnl that either the system getcwd is robust, or that calling code is okay
61 dnl with spurious failures when run from a directory with an absolute name
62 dnl larger than 4k bytes.
63 dnl
64 dnl Assumes that getcwd exists; if you are worried about obsolete
65 dnl platforms that lacked getcwd(), then you need to use the GPL module.
66 AC_DEFUN([gl_FUNC_GETCWD_LGPL],
67 [
68   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
69   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
70
71   case $gl_cv_func_getcwd_null in
72   *yes) ;;
73   *)
74     dnl Minimal replacement
75     REPLACE_GETCWD=1
76     AC_LIBOBJ([getcwd-lgpl])
77     ;;
78   esac
79 ])
80
81 dnl Check for all known getcwd bugs; useful for a program likely to be
82 dnl executed from an arbitrary location.
83 AC_DEFUN([gl_FUNC_GETCWD],
84 [
85   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
86   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
87   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
88
89   gl_abort_bug=no
90   case $gl_cv_func_getcwd_null,$host_os in
91   *,mingw*)
92     gl_cv_func_getcwd_path_max=yes;;
93   yes,*)
94     gl_FUNC_GETCWD_PATH_MAX
95     gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes]);;
96   esac
97
98   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_path_max,$gl_abort_bug in
99   *yes,yes,no) ;;
100   *)
101     dnl Full replacement, overrides LGPL replacement.
102     REPLACE_GETCWD=1
103     AC_LIBOBJ([getcwd])
104     gl_PREREQ_GETCWD;;
105   esac
106 ])
107
108 # Prerequisites of lib/getcwd.c, when full replacement is in effect.
109 AC_DEFUN([gl_PREREQ_GETCWD],
110 [
111   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
112   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
113   :
114 ])