| 1 |
# dirfd.m4 |
| 2 |
# serial 30 -*- Autoconf -*- |
| 3 |
dnl Copyright (C) 2001-2006, 2008-2024 Free Software Foundation, Inc. |
| 4 |
dnl This file is free software; the Free Software Foundation |
| 5 |
dnl gives unlimited permission to copy and/or distribute it, |
| 6 |
dnl with or without modifications, as long as this notice is preserved. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
dnl Find out how to get the file descriptor associated with an open DIR*. |
| 10 |
|
| 11 |
dnl From Jim Meyering |
| 12 |
|
| 13 |
AC_DEFUN([gl_FUNC_DIRFD], |
| 14 |
[ |
| 15 |
AC_REQUIRE([gl_DIRENT_H_DEFAULTS]) |
| 16 |
AC_REQUIRE([AC_CANONICAL_HOST]) |
| 17 |
|
| 18 |
dnl Persuade glibc <dirent.h> to declare dirfd(). |
| 19 |
AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) |
| 20 |
|
| 21 |
AC_CHECK_FUNCS([dirfd]) |
| 22 |
AC_CHECK_DECLS([dirfd], , , |
| 23 |
[[#include <sys/types.h> |
| 24 |
#include <dirent.h>]]) |
| 25 |
if test $ac_cv_have_decl_dirfd = no; then |
| 26 |
HAVE_DECL_DIRFD=0 |
| 27 |
fi |
| 28 |
|
| 29 |
AC_CACHE_CHECK([whether dirfd is a macro], |
| 30 |
[gl_cv_func_dirfd_macro], |
| 31 |
[AC_EGREP_CPP([dirent_header_defines_dirfd], [ |
| 32 |
#include <sys/types.h> |
| 33 |
#include <dirent.h> |
| 34 |
#ifdef dirfd |
| 35 |
dirent_header_defines_dirfd |
| 36 |
#endif], |
| 37 |
[gl_cv_func_dirfd_macro=yes], |
| 38 |
[gl_cv_func_dirfd_macro=no])]) |
| 39 |
|
| 40 |
if test $ac_cv_func_dirfd = no && test $gl_cv_func_dirfd_macro = no; then |
| 41 |
HAVE_DIRFD=0 |
| 42 |
else |
| 43 |
HAVE_DIRFD=1 |
| 44 |
dnl Replace dirfd() on native Windows and OS/2 kLIBC, |
| 45 |
dnl to support fdopendir(). |
| 46 |
AC_REQUIRE([gl_DIRENT_DIR]) |
| 47 |
if test $DIR_HAS_FD_MEMBER = 0; then |
| 48 |
REPLACE_DIRFD=1 |
| 49 |
fi |
| 50 |
fi |
| 51 |
]) |
| 52 |
|
| 53 |
dnl Prerequisites of lib/dirfd.c. |
| 54 |
AC_DEFUN([gl_PREREQ_DIRFD], |
| 55 |
[ |
| 56 |
AC_CACHE_CHECK([how to get the file descriptor associated with an open DIR*], |
| 57 |
[gl_cv_sys_dir_fd_member_name], |
| 58 |
[ |
| 59 |
gl_saved_CFLAGS=$CFLAGS |
| 60 |
for ac_expr in d_fd dd_fd; do |
| 61 |
|
| 62 |
CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr" |
| 63 |
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ |
| 64 |
#include <sys/types.h> |
| 65 |
#include <dirent.h>]], |
| 66 |
[[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])], |
| 67 |
[dir_fd_found=yes] |
| 68 |
) |
| 69 |
CFLAGS=$gl_saved_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 |
]) |