getcwd-lgpl: new module
[pspp] / 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 5
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 #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
57 dnl Guarantee that getcwd will malloc with a NULL first argument.  Assumes
58 dnl that either the system getcwd is robust, or that calling code is okay
59 dnl with spurious failures when run from a directory with an absolute name
60 dnl larger than 4k bytes.
61 dnl
62 dnl Assumes that getcwd exists; if you are worried about obsolete
63 dnl platforms that lacked getcwd(), then you need to use the GPL module.
64 AC_DEFUN([gl_FUNC_GETCWD_LGPL],
65 [
66   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
67   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
68
69   case $gl_cv_func_getcwd_null in
70   *yes) ;;
71   *)
72     dnl Minimal replacement
73     REPLACE_GETCWD=1
74     AC_LIBOBJ([getcwd-lgpl])
75     ;;
76   esac
77 ])
78
79 dnl Check for all known getcwd bugs; useful for a program likely to be
80 dnl executed from an arbitrary location.
81 AC_DEFUN([gl_FUNC_GETCWD],
82 [
83   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
84   AC_REQUIRE([gl_FUNC_GETCWD_NULL])
85   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
86
87   gl_abort_bug=no
88   case $gl_cv_func_getcwd_null,$host_os in
89   *,mingw*)
90     gl_cv_func_getcwd_path_max=yes;;
91   yes,*)
92     gl_FUNC_GETCWD_PATH_MAX
93     gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes]);;
94   esac
95
96   case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_path_max,$gl_abort_bug in
97   *yes,yes,no) ;;
98   *)
99     dnl Full replacement, overrides LGPL replacement.
100     REPLACE_GETCWD=1
101     AC_LIBOBJ([getcwd])
102     gl_PREREQ_GETCWD;;
103   esac
104 ])
105
106 # Prerequisites of lib/getcwd.c, when full replacement is in effect.
107 AC_DEFUN([gl_PREREQ_GETCWD],
108 [
109   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
110   AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
111   :
112 ])