* backupfile.c, dirfd.h, fts.c, getcwd.c, glob.c, glob_.h:
[gnulib.git] / m4 / dirfd.m4
1 #serial 13   -*- 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_CHECK_FUNCS(dirfd)
21   AC_CHECK_DECLS([dirfd], , ,
22     [#include <sys/types.h>
23      #include <dirent.h>])
24
25   AC_CACHE_CHECK([whether dirfd is a macro],
26     gl_cv_func_dirfd_macro,
27     [AC_EGREP_CPP([dirent_header_defines_dirfd], [
28 #include <sys/types.h>
29 #include <dirent.h>
30 #ifdef dirfd
31  dirent_header_defines_dirfd
32 #endif],
33        gl_cv_func_dirfd_macro=yes,
34        gl_cv_func_dirfd_macro=no)])
35
36   # Use the replacement only if we have no function, macro,
37   # or declaration with that name.
38   if test $ac_cv_func_dirfd,$ac_cv_have_decl_dirfd,$gl_cv_func_dirfd_macro \
39       = no,no,no; then
40     AC_REPLACE_FUNCS([dirfd])
41     AC_CACHE_CHECK(
42               [how to get the file descriptor associated with an open DIR*],
43                    gl_cv_sys_dir_fd_member_name,
44       [
45         dirfd_save_CFLAGS=$CFLAGS
46         for ac_expr in d_fd dd_fd; do
47
48           CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
49           AC_TRY_COMPILE(
50             [#include <sys/types.h>
51              #include <dirent.h>],
52             [DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;],
53             dir_fd_found=yes
54           )
55           CFLAGS=$dirfd_save_CFLAGS
56           test "$dir_fd_found" = yes && break
57         done
58         test "$dir_fd_found" = yes || ac_expr=no_such_member
59
60         gl_cv_sys_dir_fd_member_name=$ac_expr
61       ]
62     )
63     if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
64       AC_DEFINE_UNQUOTED(DIR_FD_MEMBER_NAME,
65         $gl_cv_sys_dir_fd_member_name,
66         [the name of the file descriptor member of DIR])
67     fi
68     AH_VERBATIM(DIR_TO_FD,
69                 [#ifdef DIR_FD_MEMBER_NAME
70 # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
71 #else
72 # define DIR_TO_FD(Dir_p) -1
73 #endif
74 ])
75   fi
76 ])