use AC_HEADER_DIRECT and all the associated cruft
[gnulib.git] / m4 / dirfd.m4
1 #serial 1
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   AC_REPLACE_FUNCS([dirfd])
10   dirfd_headers='
11 #if HAVE_DIRENT_H
12 # include <dirent.h>
13 #else /* not HAVE_DIRENT_H */
14 # define dirent direct
15 # if HAVE_SYS_NDIR_H
16 #  include <sys/ndir.h>
17 # endif /* HAVE_SYS_NDIR_H */
18 # if HAVE_SYS_DIR_H
19 #  include <sys/dir.h>
20 # endif /* HAVE_SYS_DIR_H */
21 # if HAVE_NDIR_H
22 #  include <ndir.h>
23 # endif /* HAVE_NDIR_H */
24 #endif /* HAVE_DIRENT_H */
25 '
26   AC_CHECK_DECLS([dirfd], , , $dirfd_headers)
27   if test $ac_cv_func_dirfd = no; then
28     AC_CACHE_CHECK(
29               [how to get the file descriptor associated with an open DIR*],
30                    ac_cv_sys_dir_to_fd,
31       [
32         dirfd_save_DEFS=$DEFS
33         for ac_expr in                                          \
34                                                                 \
35             '# Solaris'                                         \
36             'dir_p->d_fd'                                       \
37                                                                 \
38             '# Solaris'                                         \
39             'dir_p->dd_fd'                                      \
40                                                                 \
41             '# systems for which the info is not available'     \
42             -1                                                  \
43             ; do
44
45           # Skip each embedded comment.
46           case "$ac_expr" in '#'*) continue;; esac
47
48           DEFS="$DEFS -DDIR_TO_FD=$ac_expr"
49           AC_TRY_COMPILE(
50             [$dirfd_headers
51             ],
52             [DIR *dir_p = opendir("."); (void) ($ac_expr);],
53             dir_fd_done=yes
54           )
55           DEFS=$dirfd_save_DEFS
56           test "$dir_fd_done" = yes && break
57         done
58
59         ac_cv_sys_dir_to_fd=$ac_expr
60       ]
61     )
62     AC_DEFINE_UNQUOTED(DIR_TO_FD,
63       $ac_cv_sys_dir_to_fd,
64       [the file descriptor associated with `dir_p'])
65   fi
66 ])