Blame m4/getcwd.m4

Packit 709fb3
# getcwd.m4 - check for working getcwd that is compatible with glibc
Packit 709fb3
Packit 709fb3
# Copyright (C) 2001, 2003-2007, 2009-2017 Free Software Foundation, Inc.
Packit 709fb3
# This file is free software; the Free Software Foundation
Packit 709fb3
# gives unlimited permission to copy and/or distribute it,
Packit 709fb3
# with or without modifications, as long as this notice is preserved.
Packit 709fb3
Packit 709fb3
# Written by Paul Eggert.
Packit 709fb3
# serial 14
Packit 709fb3
Packit 709fb3
AC_DEFUN([gl_FUNC_GETCWD_NULL],
Packit 709fb3
  [
Packit 709fb3
   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 709fb3
   AC_CHECK_HEADERS_ONCE([unistd.h])
Packit 709fb3
   AC_CACHE_CHECK([whether getcwd (NULL, 0) allocates memory for result],
Packit 709fb3
     [gl_cv_func_getcwd_null],
Packit 709fb3
     [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
Packit 709fb3
#	 include <stdlib.h>
Packit 709fb3
#        if HAVE_UNISTD_H
Packit 709fb3
#         include <unistd.h>
Packit 709fb3
#        else /* on Windows with MSVC */
Packit 709fb3
#         include <direct.h>
Packit 709fb3
#        endif
Packit 709fb3
#        ifndef getcwd
Packit 709fb3
         char *getcwd ();
Packit 709fb3
#        endif
Packit 709fb3
]], [[
Packit 709fb3
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit 709fb3
/* mingw cwd does not start with '/', but getcwd does allocate.
Packit 709fb3
   However, mingw fails to honor non-zero size.  */
Packit 709fb3
#else
Packit 709fb3
           if (chdir ("/") != 0)
Packit 709fb3
             return 1;
Packit 709fb3
           else
Packit 709fb3
             {
Packit 709fb3
               char *f = getcwd (NULL, 0);
Packit 709fb3
               if (! f)
Packit 709fb3
                 return 2;
Packit 709fb3
               if (f[0] != '/')
Packit 709fb3
                 { free (f); return 3; }
Packit 709fb3
               if (f[1] != '\0')
Packit 709fb3
                 { free (f); return 4; }
Packit 709fb3
               free (f);
Packit 709fb3
               return 0;
Packit 709fb3
             }
Packit 709fb3
#endif
Packit 709fb3
         ]])],
Packit 709fb3
        [gl_cv_func_getcwd_null=yes],
Packit 709fb3
        [gl_cv_func_getcwd_null=no],
Packit 709fb3
        [[case "$host_os" in
Packit 709fb3
                     # Guess yes on glibc systems.
Packit 709fb3
            *-gnu*)  gl_cv_func_getcwd_null="guessing yes";;
Packit 709fb3
                     # Guess yes on Cygwin.
Packit 709fb3
            cygwin*) gl_cv_func_getcwd_null="guessing yes";;
Packit 709fb3
                     # If we don't know, assume the worst.
Packit 709fb3
            *)       gl_cv_func_getcwd_null="guessing no";;
Packit 709fb3
          esac
Packit 709fb3
        ]])])
Packit 709fb3
])
Packit 709fb3
Packit 709fb3
AC_DEFUN([gl_FUNC_GETCWD_SIGNATURE],
Packit 709fb3
[
Packit 709fb3
  AC_CACHE_CHECK([for getcwd with POSIX signature],
Packit 709fb3
    [gl_cv_func_getcwd_posix_signature],
Packit 709fb3
    [AC_COMPILE_IFELSE(
Packit 709fb3
      [AC_LANG_PROGRAM(
Packit 709fb3
         [[#include <unistd.h>]],
Packit 709fb3
         [[extern
Packit 709fb3
           #ifdef __cplusplus
Packit 709fb3
           "C"
Packit 709fb3
           #endif
Packit 709fb3
           char *getcwd (char *, size_t);
Packit 709fb3
         ]])
Packit 709fb3
      ],
Packit 709fb3
      [gl_cv_func_getcwd_posix_signature=yes],
Packit 709fb3
      [gl_cv_func_getcwd_posix_signature=no])
Packit 709fb3
   ])
Packit 709fb3
])
Packit 709fb3
Packit 709fb3
dnl Guarantee that getcwd will malloc with a NULL first argument.  Assumes
Packit 709fb3
dnl that either the system getcwd is robust, or that calling code is okay
Packit 709fb3
dnl with spurious failures when run from a directory with an absolute name
Packit 709fb3
dnl larger than 4k bytes.
Packit 709fb3
dnl
Packit 709fb3
dnl Assumes that getcwd exists; if you are worried about obsolete
Packit 709fb3
dnl platforms that lacked getcwd(), then you need to use the GPL module.
Packit 709fb3
AC_DEFUN([gl_FUNC_GETCWD_LGPL],
Packit 709fb3
[
Packit 709fb3
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
Packit 709fb3
  AC_REQUIRE([gl_FUNC_GETCWD_NULL])
Packit 709fb3
  AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
Packit 709fb3
Packit 709fb3
  case $gl_cv_func_getcwd_null,$gl_cv_func_getcwd_posix_signature in
Packit 709fb3
  *yes,yes) ;;
Packit 709fb3
  *)
Packit 709fb3
    dnl Minimal replacement lib/getcwd-lgpl.c.
Packit 709fb3
    REPLACE_GETCWD=1
Packit 709fb3
    ;;
Packit 709fb3
  esac
Packit 709fb3
])
Packit 709fb3
Packit 709fb3
dnl Check for all known getcwd bugs; useful for a program likely to be
Packit 709fb3
dnl executed from an arbitrary location.
Packit 709fb3
AC_DEFUN([gl_FUNC_GETCWD],
Packit 709fb3
[
Packit 709fb3
  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
Packit 709fb3
  AC_REQUIRE([gl_FUNC_GETCWD_NULL])
Packit 709fb3
  AC_REQUIRE([gl_FUNC_GETCWD_SIGNATURE])
Packit 709fb3
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 709fb3
Packit 709fb3
  gl_abort_bug=no
Packit 709fb3
  case "$host_os" in
Packit 709fb3
    mingw*)
Packit 709fb3
      gl_cv_func_getcwd_path_max=yes
Packit 709fb3
      ;;
Packit 709fb3
    *)
Packit 709fb3
      gl_FUNC_GETCWD_PATH_MAX
Packit 709fb3
      case "$gl_cv_func_getcwd_null" in
Packit 709fb3
        *yes)
Packit 709fb3
          gl_FUNC_GETCWD_ABORT_BUG([gl_abort_bug=yes])
Packit 709fb3
          ;;
Packit 709fb3
      esac
Packit 709fb3
      ;;
Packit 709fb3
  esac
Packit 709fb3
  dnl Define HAVE_MINIMALLY_WORKING_GETCWD and HAVE_PARTLY_WORKING_GETCWD
Packit 709fb3
  dnl if appropriate.
Packit 709fb3
  case "$gl_cv_func_getcwd_path_max" in
Packit 709fb3
    "no"|"no, it has the AIX bug") ;;
Packit 709fb3
    *)
Packit 709fb3
      AC_DEFINE([HAVE_MINIMALLY_WORKING_GETCWD], [1],
Packit 709fb3
        [Define to 1 if getcwd minimally works, that is, its result can be
Packit 709fb3
         trusted when it succeeds.])
Packit 709fb3
      ;;
Packit 709fb3
  esac
Packit 709fb3
  case "$gl_cv_func_getcwd_path_max" in
Packit 709fb3
    "no, but it is partly working")
Packit 709fb3
      AC_DEFINE([HAVE_PARTLY_WORKING_GETCWD], [1],
Packit 709fb3
        [Define to 1 if getcwd works, except it sometimes fails when it
Packit 709fb3
         shouldn't, setting errno to ERANGE, ENAMETOOLONG, or ENOENT.])
Packit 709fb3
      ;;
Packit 709fb3
    "yes, but with shorter paths")
Packit 709fb3
      AC_DEFINE([HAVE_GETCWD_SHORTER], [1],
Packit 709fb3
        [Define to 1 if getcwd works, but with shorter paths
Packit 709fb3
         than is generally tested with the replacement.])
Packit 709fb3
      ;;
Packit 709fb3
  esac
Packit 709fb3
Packit 709fb3
  if { case "$gl_cv_func_getcwd_null" in *yes) false;; *) true;; esac; } \
Packit 709fb3
     || test $gl_cv_func_getcwd_posix_signature != yes \
Packit 709fb3
     || { case "$gl_cv_func_getcwd_path_max" in *yes*) false;; *) true;; esac; } \
Packit 709fb3
     || test $gl_abort_bug = yes; then
Packit 709fb3
    REPLACE_GETCWD=1
Packit 709fb3
  fi
Packit 709fb3
])
Packit 709fb3
Packit 709fb3
# Prerequisites of lib/getcwd.c, when full replacement is in effect.
Packit 709fb3
AC_DEFUN([gl_PREREQ_GETCWD],
Packit 709fb3
[
Packit 709fb3
  AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
Packit 709fb3
  AC_REQUIRE([gl_CHECK_TYPE_STRUCT_DIRENT_D_INO])
Packit 709fb3
  :
Packit 709fb3
])