Blame m4/dirfd.m4

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