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