dirfd.m4 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # serial 24 -*- Autoconf -*-
  2. dnl Find out how to get the file descriptor associated with an open DIR*.
  3. # Copyright (C) 2001-2006, 2008-2017 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. dnl Persuade glibc <dirent.h> to declare dirfd().
  12. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  13. AC_CHECK_FUNCS([dirfd])
  14. AC_CHECK_DECLS([dirfd], , ,
  15. [[#include <sys/types.h>
  16. #include <dirent.h>]])
  17. if test $ac_cv_have_decl_dirfd = no; then
  18. HAVE_DECL_DIRFD=0
  19. fi
  20. AC_CACHE_CHECK([whether dirfd is a macro],
  21. gl_cv_func_dirfd_macro,
  22. [AC_EGREP_CPP([dirent_header_defines_dirfd], [
  23. #include <sys/types.h>
  24. #include <dirent.h>
  25. #ifdef dirfd
  26. dirent_header_defines_dirfd
  27. #endif],
  28. gl_cv_func_dirfd_macro=yes,
  29. gl_cv_func_dirfd_macro=no)])
  30. # Use the replacement if we have no function or macro with that name,
  31. # or if OS/2 kLIBC whose dirfd() does not work.
  32. # Replace only if the system declares dirfd already.
  33. case $ac_cv_func_dirfd,$gl_cv_func_dirfd_macro,$host_os,$ac_cv_have_decl_dirfd in
  34. no,no,*,yes | *,*,os2*,yes)
  35. REPLACE_DIRFD=1
  36. AC_DEFINE([REPLACE_DIRFD], [1],
  37. [Define to 1 if gnulib's dirfd() replacement is used.]);;
  38. esac
  39. ])
  40. dnl Prerequisites of lib/dirfd.c.
  41. AC_DEFUN([gl_PREREQ_DIRFD],
  42. [
  43. AC_CACHE_CHECK([how to get the file descriptor associated with an open DIR*],
  44. [gl_cv_sys_dir_fd_member_name],
  45. [
  46. dirfd_save_CFLAGS=$CFLAGS
  47. for ac_expr in d_fd dd_fd; do
  48. CFLAGS="$CFLAGS -DDIR_FD_MEMBER_NAME=$ac_expr"
  49. AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
  50. #include <sys/types.h>
  51. #include <dirent.h>]],
  52. [[DIR *dir_p = opendir("."); (void) dir_p->DIR_FD_MEMBER_NAME;]])],
  53. [dir_fd_found=yes]
  54. )
  55. CFLAGS=$dirfd_save_CFLAGS
  56. test "$dir_fd_found" = yes && break
  57. done
  58. test "$dir_fd_found" = yes || ac_expr=no_such_member
  59. gl_cv_sys_dir_fd_member_name=$ac_expr
  60. ]
  61. )
  62. if test $gl_cv_sys_dir_fd_member_name != no_such_member; then
  63. AC_DEFINE_UNQUOTED([DIR_FD_MEMBER_NAME],
  64. [$gl_cv_sys_dir_fd_member_name],
  65. [the name of the file descriptor member of DIR])
  66. fi
  67. AH_VERBATIM([DIR_TO_FD],
  68. [#ifdef DIR_FD_MEMBER_NAME
  69. # define DIR_TO_FD(Dir_p) ((Dir_p)->DIR_FD_MEMBER_NAME)
  70. #else
  71. # define DIR_TO_FD(Dir_p) -1
  72. #endif
  73. ])
  74. ])