Blame m4/getcwd.m4

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