* argz.m4 (gl_FUNC_ARGZ): If we define `error_t', also define
[gnulib.git] / m4 / dirfd.m4
1 #serial 11   -*- Autoconf -*-
2
3 dnl Find out how to get the file descriptor associated with an open DIR*.
4
5 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
6 # Foundation, Inc.
7 # This file is free software; the Free Software Foundation
8 # gives unlimited permission to copy and/or distribute it,
9 # with or without modifications, as long as this notice is preserved.
10
11 dnl From Jim Meyering
12
13 AC_DEFUN([gl_FUNC_DIRFD],
14 [
15   AC_LIBSOURCES([dirfd.c, dirfd.h])
16
17   dnl Work around a bug of AC_EGREP_CPP in autoconf-2.57.
18   AC_REQUIRE([AC_PROG_CPP])
19   AC_REQUIRE([AC_PROG_EGREP])
20   AC_REQUIRE([AC_HEADER_DIRENT])
21
22   dirfd_headers='
23 #if HAVE_DIRENT_H
24 # include <dirent.h>
25 #else /* not HAVE_DIRENT_H */
26 # define dirent direct
27 # if HAVE_SYS_NDIR_H
28 #  include <sys/ndir.h>
29 # endif /* HAVE_SYS_NDIR_H */
30 # if HAVE_SYS_DIR_H
31 #  include <sys/dir.h>
32 # endif /* HAVE_SYS_DIR_H */
33 # if HAVE_NDIR_H
34 #  include <ndir.h>
35 # endif /* HAVE_NDIR_H */
36 #endif /* HAVE_DIRENT_H */
37 '
38   AC_CHECK_FUNCS(dirfd)
39   AC_CHECK_DECLS([dirfd], , , $dirfd_headers)
40
41   AC_CACHE_CHECK([whether dirfd is a macro],
42     gl_cv_func_dirfd_macro,
43     [AC_EGREP_CPP([dirent_header_defines_dirfd], [$dirfd_headers
44 #ifdef dirfd
45  dirent_header_defines_dirfd
46 #endif],
47        gl_cv_func_dirfd_macro=yes,
48        gl_cv_func_dirfd_macro=no)])
49
50   # Use the replacement only if we have no function, macro,
51   # or declaration with that name.
52   if test $ac_cv_func_dirfd,$ac_cv_have_decl_dirfd,$gl_cv_func_dirfd_macro \
53       = no,no,no; then
54     AC_REPLACE_FUNCS([dirfd])
55     AC_CACHE_CHECK(
56               [how to get the file descriptor associated with an open DIR*],
57                    gl_cv_sys_dir_fd_member_name,
58       [
59         dirfd_save_CFLAGS=$CFLAGS
60         for ac_expr in d_fd dd_fd; do
61
62           CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
63           AC_TRY_COMPILE(
64             [$dirfd_headers
65             ],
66             [DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;],
67             dir_fd_found=yes
68           )
69           CFLAGS=$dirfd_save_CFLAGS
70           test "$dir_fd_found" = yes && break
71         done
72         test "$dir_fd_found" = yes || ac_expr=no_such_member
73
74         gl_cv_sys_dir_fd_member_name=$ac_expr
75       ]
76     )
77     if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
78       AC_DEFINE_UNQUOTED(DIR_FD_MEMBER_NAME,
79         $gl_cv_sys_dir_fd_member_name,
80         [the name of the file descriptor member of DIR])
81     fi
82     AH_VERBATIM(DIR_TO_FD,
83                 [#ifdef DIR_FD_MEMBER_NAME
84 # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
85 #else
86 # define DIR_TO_FD(Dir_p) -1
87 #endif
88 ])
89   fi
90 ])