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