fstat.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* fstat() replacement.
  2. Copyright (C) 2011-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* If the user's config.h happens to include <sys/stat.h>, let it include only
  14. the system's <sys/stat.h> here, so that orig_fstat doesn't recurse to
  15. rpl_fstat. */
  16. #define __need_system_sys_stat_h
  17. #include <config.h>
  18. /* Get the original definition of fstat. It might be defined as a macro. */
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #if _GL_WINDOWS_64_BIT_ST_SIZE
  22. # undef stat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
  23. # define stat _stati64
  24. # undef fstat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
  25. # define fstat _fstati64
  26. #endif
  27. #undef __need_system_sys_stat_h
  28. static int
  29. orig_fstat (int fd, struct stat *buf)
  30. {
  31. return fstat (fd, buf);
  32. }
  33. /* Specification. */
  34. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  35. eliminates this include because of the preliminary #include <sys/stat.h>
  36. above. */
  37. #include "sys/stat.h"
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  41. # include "msvc-inval.h"
  42. #endif
  43. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  44. static int
  45. fstat_nothrow (int fd, struct stat *buf)
  46. {
  47. int result;
  48. TRY_MSVC_INVAL
  49. {
  50. result = orig_fstat (fd, buf);
  51. }
  52. CATCH_MSVC_INVAL
  53. {
  54. result = -1;
  55. errno = EBADF;
  56. }
  57. DONE_MSVC_INVAL;
  58. return result;
  59. }
  60. #else
  61. # define fstat_nothrow orig_fstat
  62. #endif
  63. int
  64. rpl_fstat (int fd, struct stat *buf)
  65. {
  66. #if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
  67. /* Handle the case when rpl_open() used a dummy file descriptor to work
  68. around an open() that can't normally visit directories. */
  69. const char *name = _gl_directory_name (fd);
  70. if (name != NULL)
  71. return stat (name, buf);
  72. #endif
  73. return fstat_nothrow (fd, buf);
  74. }