1 # serial 26 -*- Autoconf -*-
3 dnl Find out how to get the file descriptor associated with an open DIR*.
5 # Copyright (C) 2001-2006, 2008-2018 Free Software Foundation, Inc.
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
12 AC_DEFUN([gl_FUNC_DIRFD],
14 AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
15 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 dnl Persuade glibc <dirent.h> to declare dirfd().
18 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
20 AC_CHECK_FUNCS([dirfd])
21 AC_CHECK_DECLS([dirfd], , ,
22 [[#include <sys/types.h>
23 #include <dirent.h>]])
24 if test $ac_cv_have_decl_dirfd = no; then
28 AC_CACHE_CHECK([whether dirfd is a macro],
29 [gl_cv_func_dirfd_macro],
30 [AC_EGREP_CPP([dirent_header_defines_dirfd], [
31 #include <sys/types.h>
34 dirent_header_defines_dirfd
36 [gl_cv_func_dirfd_macro=yes],
37 [gl_cv_func_dirfd_macro=no])])
39 # Use the replacement if we have no function or macro with that name,
40 # or if OS/2 kLIBC whose dirfd() does not work.
41 # Replace only if the system declares dirfd already.
42 case $ac_cv_func_dirfd,$gl_cv_func_dirfd_macro,$host_os,$ac_cv_have_decl_dirfd in
43 no,no,*,yes | *,*,os2*,yes)
45 AC_DEFINE([REPLACE_DIRFD], [1],
46 [Define to 1 if gnulib's dirfd() replacement is used.]);;
50 dnl Prerequisites of lib/dirfd.c.
51 AC_DEFUN([gl_PREREQ_DIRFD],
53 AC_CACHE_CHECK([how to get the file descriptor associated with an open DIR*],
54 [gl_cv_sys_dir_fd_member_name],
56 dirfd_save_CFLAGS=$CFLAGS
57 for ac_expr in d_fd dd_fd; do
59 CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
60 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
61 #include <sys/types.h>
62 #include <dirent.h>]],
63 [[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])],
66 CFLAGS=$dirfd_save_CFLAGS
67 test "$dir_fd_found" = yes && break
69 test "$dir_fd_found" = yes || ac_expr=no_such_member
71 gl_cv_sys_dir_fd_member_name=$ac_expr
74 if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
75 AC_DEFINE_UNQUOTED([DIR_FD_MEMBER_NAME],
76 [$gl_cv_sys_dir_fd_member_name],
77 [the name of the file descriptor member of DIR])
79 AH_VERBATIM([DIR_TO_FD],
80 [#ifdef DIR_FD_MEMBER_NAME
81 # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
83 # define DIR_TO_FD(Dir_p) -1