Blame m4/dirfd.m4

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