(UTILS_FUNC_DIRFD): Also detect when dirfd is a macro.
[gnulib.git] / m4 / dirfd.m4
1 #serial 2
2
3 dnl Find out how to get the file descriptor associated with an open DIR*.
4 dnl From Jim Meyering
5
6 AC_DEFUN([UTILS_FUNC_DIRFD],
7 [
8   AC_HEADER_DIRENT
9   dirfd_headers='
10 #if HAVE_DIRENT_H
11 # include <dirent.h>
12 #else /* not HAVE_DIRENT_H */
13 # define dirent direct
14 # if HAVE_SYS_NDIR_H
15 #  include <sys/ndir.h>
16 # endif /* HAVE_SYS_NDIR_H */
17 # if HAVE_SYS_DIR_H
18 #  include <sys/dir.h>
19 # endif /* HAVE_SYS_DIR_H */
20 # if HAVE_NDIR_H
21 #  include <ndir.h>
22 # endif /* HAVE_NDIR_H */
23 #endif /* HAVE_DIRENT_H */
24 '
25   AC_CHECK_FUNCS(dirfd)
26   AC_CHECK_DECLS([dirfd], , , $dirfd_headers)
27
28   AC_CACHE_CHECK([whether dirfd is a macro],
29     jm_cv_func_dirfd_macro,
30     AC_EGREP_CPP([dirent_header_defines_dirfd], [$dirfd_headers
31 #ifdef dirfd
32  dirent_header_defines_dirfd
33 #endif],
34       jm_cv_func_dirfd_macro=yes,
35       jm_cv_func_dirfd_macro=no))
36
37   # Use the replacement only if we have no function, macro,
38   # or declaration with that name.
39   if test $ac_cv_func_dirfd,$ac_cv_have_decl_dirfd,$jm_cv_func_dirfd_macro \
40       = no,no,no; then
41     AC_REPLACE_FUNCS([dirfd])
42     AC_CACHE_CHECK(
43               [how to get the file descriptor associated with an open DIR*],
44                    ac_cv_sys_dir_fd_member_name,
45       [
46         dirfd_save_CFLAGS=$CFLAGS
47         for ac_expr in d_fd dd_fd; do
48
49           CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
50           AC_TRY_COMPILE(
51             [$dirfd_headers
52             ],
53             [DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;],
54             dir_fd_found=yes
55           )
56           CFLAGS=$dirfd_save_CFLAGS
57           test "$dir_fd_found" = yes && break
58         done
59         test "$dir_fd_found" = yes || ac_expr=-1
60
61         ac_cv_sys_dir_fd_member_name=$ac_expr
62       ]
63     )
64     if test $ac_cv_have_decl_dirfd = -1; then
65       AC_DEFINE_UNQUOTED(DIR_FD_MEMBER_NAME,
66         $ac_cv_sys_dir_fd_member_name,
67         [the name of the file descriptor member of DIR])
68     fi
69     AH_VERBATIM(DIR_TO_FD,
70                 [#ifdef DIR_FD_MEMBER_NAME
71 # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
72 #else
73 # define DIR_TO_FD(Dir_p) -1
74 #endif
75 ]
76     )
77   fi
78 ])