Merge branch 'upstream' into stable
[gnulib.git] / m4 / getcwd.m4
1 # getcwd.m4 - check for working getcwd that is compatible with glibc
2
3 # Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software
4 # Foundation, Inc.
5 # This file is free software; the Free Software Foundation
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
8
9 # Written by Paul Eggert.
10 # serial 3
11
12 AC_DEFUN([gl_FUNC_GETCWD_NULL],
13   [
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 #else
25            if (chdir ("/") != 0)
26              return 1;
27            else
28              {
29                char *f = getcwd (NULL, 0);
30                if (! f)
31                  return 2;
32                if (f[0] != '/')
33                  return 3;
34                if (f[1] != '\0')
35                  return 4;
36                return 0;
37              }
38 #endif
39          ]])],
40         [gl_cv_func_getcwd_null=yes],
41         [gl_cv_func_getcwd_null=no],
42         [[
43        case "$host_os" in
44                                # Guess yes on glibc systems.
45          *-gnu*)               gl_cv_func_getcwd_null="guessing yes";;
46                                # Guess yes on Cygwin.
47          cygwin*)              gl_cv_func_getcwd_null="guessing yes";;
48                                # Guess yes on mingw.
49          mingw*)               gl_cv_func_getcwd_null="guessing yes";;
50                                # If we don't know, assume the worst.
51          *)                    gl_cv_func_getcwd_null="guessing no";;
52        esac
53         ]])])
54 ])
55
56 AC_DEFUN([gl_FUNC_GETCWD],
57 [
58   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
59   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
60   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
61
62   gl_abort_bug=no
63   case $gl_cv_func_getcwd_null,$host_os in
64   *,mingw*)
65     gl_cv_func_getcwd_path_max=yes;;
66   yes,*)
67     gl_FUNC_GETCWD_PATH_MAX
68     gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes]);;
69   esac
70
71   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_path_max,$gl_abort_bug in
72   *yes,yes,no) ;;
73   *)
74     REPLACE_GETCWD=1
75     AC_LIBOBJ([getcwd])
76     gl_PREREQ_GETCWD;;
77   esac
78 ])
79
80 # Prerequisites of lib/getcwd.c.
81 AC_DEFUN([gl_PREREQ_GETCWD],
82 [
83   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
84   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
85   :
86 ])