]> git.cworth.org Git - tar/blob - m4/dirfd.m4
Imported Upstream version 1.24
[tar] / m4 / dirfd.m4
1 # serial 18   -*- Autoconf -*-
2
3 dnl Find out how to get the file descriptor associated with an open DIR*.
4
5 # Copyright (C) 2001-2006, 2008-2010 Free Software Foundation, Inc.
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 dnl From Jim Meyering
11
12 AC_DEFUN([gl_FUNC_DIRFD],
13 [
14   AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
15   gl_REPLACE_DIRENT_H
16
17   dnl Persuade glibc <dirent.h> to declare dirfd().
18   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
19
20   dnl Work around a bug of AC_EGREP_CPP in autoconf-2.57.
21   AC_REQUIRE([AC_PROG_CPP])
22   AC_REQUIRE([AC_PROG_EGREP])
23
24   AC_CHECK_FUNCS([dirfd])
25   AC_CHECK_DECLS([dirfd], , ,
26     [#include <sys/types.h>
27      #include <dirent.h>])
28   if test $ac_cv_have_decl_dirfd = no; then
29     HAVE_DECL_DIRFD=0
30   fi
31
32   AC_CACHE_CHECK([whether dirfd is a macro],
33     gl_cv_func_dirfd_macro,
34     [AC_EGREP_CPP([dirent_header_defines_dirfd], [
35 #include <sys/types.h>
36 #include <dirent.h>
37 #ifdef dirfd
38  dirent_header_defines_dirfd
39 #endif],
40        gl_cv_func_dirfd_macro=yes,
41        gl_cv_func_dirfd_macro=no)])
42
43   # Use the replacement only if we have no function or macro with that name.
44   if test $ac_cv_func_dirfd,$gl_cv_func_dirfd_macro = no,no; then
45     if test $ac_cv_have_decl_dirfd = yes; then
46       # If the system declares dirfd already, let's declare rpl_dirfd instead.
47       REPLACE_DIRFD=1
48     fi
49     AC_LIBOBJ([dirfd])
50     AC_CACHE_CHECK(
51               [how to get the file descriptor associated with an open DIR*],
52                    gl_cv_sys_dir_fd_member_name,
53       [
54         dirfd_save_CFLAGS=$CFLAGS
55         for ac_expr in d_fd dd_fd; do
56
57           CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
58           AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
59              #include <sys/types.h>
60              #include <dirent.h>]],
61             [[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])],
62             [dir_fd_found=yes]
63           )
64           CFLAGS=$dirfd_save_CFLAGS
65           test "$dir_fd_found" = yes && break
66         done
67         test "$dir_fd_found" = yes || ac_expr=no_such_member
68
69         gl_cv_sys_dir_fd_member_name=$ac_expr
70       ]
71     )
72     if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
73       AC_DEFINE_UNQUOTED([DIR_FD_MEMBER_NAME],
74         $gl_cv_sys_dir_fd_member_name,
75         [the name of the file descriptor member of DIR])
76     fi
77     AH_VERBATIM(DIR_TO_FD,
78                 [#ifdef DIR_FD_MEMBER_NAME
79 # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
80 #else
81 # define DIR_TO_FD(Dir_p) -1
82 #endif
83 ])
84   fi
85 ])