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