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