dirfd.m4 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # serial 26 -*- Autoconf -*-
  2. dnl Find out how to get the file descriptor associated with an open DIR*.
  3. # Copyright (C) 2001-2006, 2008-2021 Free Software Foundation, Inc.
  4. # This file is free software; the Free Software Foundation
  5. # gives unlimited permission to copy and/or distribute it,
  6. # with or without modifications, as long as this notice is preserved.
  7. dnl From Jim Meyering
  8. AC_DEFUN([gl_FUNC_DIRFD],
  9. [
  10. AC_REQUIRE([gl_DIRENT_H_DEFAULTS])
  11. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  12. dnl Persuade glibc <dirent.h> to declare dirfd().
  13. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  14. AC_CHECK_FUNCS([dirfd])
  15. AC_CHECK_DECLS([dirfd], , ,
  16. [[#include <sys/types.h>
  17. #include <dirent.h>]])
  18. if test $ac_cv_have_decl_dirfd = no; then
  19. HAVE_DECL_DIRFD=0
  20. fi
  21. AC_CACHE_CHECK([whether dirfd is a macro],
  22. [gl_cv_func_dirfd_macro],
  23. [AC_EGREP_CPP([dirent_header_defines_dirfd], [
  24. #include <sys/types.h>
  25. #include <dirent.h>
  26. #ifdef dirfd
  27. dirent_header_defines_dirfd
  28. #endif],
  29. [gl_cv_func_dirfd_macro=yes],
  30. [gl_cv_func_dirfd_macro=no])])
  31. # Use the replacement if we have no function or macro with that name,
  32. # or if OS/2 kLIBC whose dirfd() does not work.
  33. # Replace only if the system declares dirfd already.
  34. case $ac_cv_func_dirfd,$gl_cv_func_dirfd_macro,$host_os,$ac_cv_have_decl_dirfd in
  35. no,no,*,yes | *,*,os2*,yes)
  36. REPLACE_DIRFD=1
  37. AC_DEFINE([REPLACE_DIRFD], [1],
  38. [Define to 1 if gnulib's dirfd() replacement is used.]);;
  39. esac
  40. ])
  41. dnl Prerequisites of lib/dirfd.c.
  42. AC_DEFUN([gl_PREREQ_DIRFD],
  43. [
  44. AC_CACHE_CHECK([how to get the file descriptor associated with an open DIR*],
  45. [gl_cv_sys_dir_fd_member_name],
  46. [
  47. dirfd_save_CFLAGS=$CFLAGS
  48. for ac_expr in d_fd dd_fd; do
  49. CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
  50. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
  51. #include <sys/types.h>
  52. #include <dirent.h>]],
  53. [[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])],
  54. [dir_fd_found=yes]
  55. )
  56. CFLAGS=$dirfd_save_CFLAGS
  57. test "$dir_fd_found" = yes && break
  58. done
  59. test "$dir_fd_found" = yes || ac_expr=no_such_member
  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. ])