Use the replacement only if we have neither the function
[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   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   # Use the replacement only if we have neither the function
29   # nor a declaration.
30   if test $ac_cv_func_dirfd,$ac_cv_have_decl_dirfd = no,no; then
31     AC_REPLACE_FUNCS([dirfd])
32     AC_CACHE_CHECK(
33               [how to get the file descriptor associated with an open DIR*],
34                    ac_cv_sys_dir_to_fd,
35       [
36         dirfd_save_DEFS=$DEFS
37         for ac_expr in                                          \
38                                                                 \
39             'dir_p->d_fd'                                       \
40                                                                 \
41             'dir_p->dd_fd'                                      \
42                                                                 \
43             '# systems for which the info is not available'     \
44             -1                                                  \
45             ; do
46
47           # Skip each embedded comment.
48           case "$ac_expr" in '#'*) continue;; esac
49
50           DEFS="$DEFS -DDIR_TO_FD=$ac_expr"
51           AC_TRY_COMPILE(
52             [$dirfd_headers
53             ],
54             [DIR *dir_p = opendir("."); (void) ($ac_expr);],
55             dir_fd_done=yes
56           )
57           DEFS=$dirfd_save_DEFS
58           test "$dir_fd_done" = yes && break
59         done
60
61         ac_cv_sys_dir_to_fd=$ac_expr
62       ]
63     )
64     AC_DEFINE_UNQUOTED(DIR_TO_FD,
65       $ac_cv_sys_dir_to_fd,
66       [the file descriptor associated with `dir_p'])
67   fi
68 ])