(UTILS_FUNC_DIRFD): New macro.
[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_REPLACE_FUNCS([dirfd])
9   if test $ac_cv_func_dirfd = no; then
10     AC_CACHE_CHECK(
11               [how to get the file descriptor associated with an open DIR*],
12                    ac_cv_sys_dir_to_fd,
13       [
14         dirfd_save_DEFS=$DEFS
15         for ac_expr in                                          \
16                                                                 \
17             '# Solaris'                                         \
18             'dir_p->d_fd'                                       \
19                                                                 \
20             '# Solaris'                                         \
21             'dir_p->dd_fd'                                      \
22                                                                 \
23             '# systems for which the info is not available'     \
24             -1                                                  \
25             ; do
26
27           # Skip each embedded comment.
28           case "$ac_expr" in '#'*) continue;; esac
29
30           DEFS="$DEFS -DDIR_TO_FD=$ac_expr"
31           AC_TRY_COMPILE(
32             [#include <sys/types.h>
33              #include <dirent.h>
34             ],
35             [DIR *dir_p = opendir("."); (void) ($ac_expr);],
36             dir_fd_done=yes
37           )
38           DEFS=$dirfd_save_DEFS
39           test "$dir_fd_done" = yes && break
40         done
41
42         ac_cv_sys_dir_to_fd=$ac_expr
43       ]
44     )
45     AC_DEFINE_UNQUOTED(DIR_TO_FD,
46       $ac_cv_sys_dir_to_fd,
47       [the file descriptor associated with `dir_p'])
48   fi
49 ])