Blame m4/dirfd.m4

Packit 8f70b4
# serial 26   -*- Autoconf -*-
Packit 8f70b4
Packit 8f70b4
dnl Find out how to get the file descriptor associated with an open DIR*.
Packit 8f70b4
Packit 8f70b4
# Copyright (C) 2001-2006, 2008-2018 Free Software Foundation, Inc.
Packit 8f70b4
# This file is free software; the Free Software Foundation
Packit 8f70b4
# gives unlimited permission to copy and/or distribute it,
Packit 8f70b4
# with or without modifications, as long as this notice is preserved.
Packit 8f70b4
Packit 8f70b4
dnl From Jim Meyering
Packit 8f70b4
Packit 8f70b4
AC_DEFUN([gl_FUNC_DIRFD],
Packit 8f70b4
[
Packit 8f70b4
  AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
Packit 8f70b4
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 8f70b4
Packit 8f70b4
  dnl Persuade glibc <dirent.h> to declare dirfd().
Packit 8f70b4
  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
Packit 8f70b4
Packit 8f70b4
  AC_CHECK_FUNCS([dirfd])
Packit 8f70b4
  AC_CHECK_DECLS([dirfd], , ,
Packit 8f70b4
    [[#include <sys/types.h>
Packit 8f70b4
      #include <dirent.h>]])
Packit 8f70b4
  if test $ac_cv_have_decl_dirfd = no; then
Packit 8f70b4
    HAVE_DECL_DIRFD=0
Packit 8f70b4
  fi
Packit 8f70b4
Packit 8f70b4
  AC_CACHE_CHECK([whether dirfd is a macro],
Packit 8f70b4
    [gl_cv_func_dirfd_macro],
Packit 8f70b4
    [AC_EGREP_CPP([dirent_header_defines_dirfd], [
Packit 8f70b4
#include <sys/types.h>
Packit 8f70b4
#include <dirent.h>
Packit 8f70b4
#ifdef dirfd
Packit 8f70b4
 dirent_header_defines_dirfd
Packit 8f70b4
#endif],
Packit 8f70b4
       [gl_cv_func_dirfd_macro=yes],
Packit 8f70b4
       [gl_cv_func_dirfd_macro=no])])
Packit 8f70b4
Packit 8f70b4
  # Use the replacement if we have no function or macro with that name,
Packit 8f70b4
  # or if OS/2 kLIBC whose dirfd() does not work.
Packit 8f70b4
  # Replace only if the system declares dirfd already.
Packit 8f70b4
  case $ac_cv_func_dirfd,$gl_cv_func_dirfd_macro,$host_os,$ac_cv_have_decl_dirfd in
Packit 8f70b4
    no,no,*,yes | *,*,os2*,yes)
Packit 8f70b4
      REPLACE_DIRFD=1
Packit 8f70b4
      AC_DEFINE([REPLACE_DIRFD], [1],
Packit 8f70b4
        [Define to 1 if gnulib's dirfd() replacement is used.]);;
Packit 8f70b4
  esac
Packit 8f70b4
])
Packit 8f70b4
Packit 8f70b4
dnl Prerequisites of lib/dirfd.c.
Packit 8f70b4
AC_DEFUN([gl_PREREQ_DIRFD],
Packit 8f70b4
[
Packit 8f70b4
  AC_CACHE_CHECK([how to get the file descriptor associated with an open DIR*],
Packit 8f70b4
                 [gl_cv_sys_dir_fd_member_name],
Packit 8f70b4
    [
Packit 8f70b4
      dirfd_save_CFLAGS=$CFLAGS
Packit 8f70b4
      for ac_expr in d_fd dd_fd; do
Packit 8f70b4
Packit 8f70b4
        CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
Packit 8f70b4
        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
Packit 8f70b4
           #include <sys/types.h>
Packit 8f70b4
           #include <dirent.h>]],
Packit 8f70b4
          [[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])],
Packit 8f70b4
          [dir_fd_found=yes]
Packit 8f70b4
        )
Packit 8f70b4
        CFLAGS=$dirfd_save_CFLAGS
Packit 8f70b4
        test "$dir_fd_found" = yes && break
Packit 8f70b4
      done
Packit 8f70b4
      test "$dir_fd_found" = yes || ac_expr=no_such_member
Packit 8f70b4
Packit 8f70b4
      gl_cv_sys_dir_fd_member_name=$ac_expr
Packit 8f70b4
    ]
Packit 8f70b4
  )
Packit 8f70b4
  if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
Packit 8f70b4
    AC_DEFINE_UNQUOTED([DIR_FD_MEMBER_NAME],
Packit 8f70b4
      [$gl_cv_sys_dir_fd_member_name],
Packit 8f70b4
      [the name of the file descriptor member of DIR])
Packit 8f70b4
  fi
Packit 8f70b4
  AH_VERBATIM([DIR_TO_FD],
Packit 8f70b4
              [#ifdef DIR_FD_MEMBER_NAME
Packit 8f70b4
# define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
Packit 8f70b4
#else
Packit 8f70b4
# define DIR_TO_FD(Dir_p) -1
Packit 8f70b4
#endif
Packit 8f70b4
])
Packit 8f70b4
])