fstat.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* fstat() replacement.
  2. Copyright (C) 2011-2012 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 Lesser 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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. #undef __need_system_sys_stat_h
  22. static inline int
  23. orig_fstat (int fd, struct stat *buf)
  24. {
  25. return fstat (fd, buf);
  26. }
  27. /* Specification. */
  28. /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
  29. eliminates this include because of the preliminary #include <sys/stat.h>
  30. above. */
  31. #include "sys/stat.h"
  32. #include <errno.h>
  33. #include <unistd.h>
  34. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  35. # include "msvc-inval.h"
  36. #endif
  37. #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
  38. static inline int
  39. fstat_nothrow (int fd, struct stat *buf)
  40. {
  41. int result;
  42. TRY_MSVC_INVAL
  43. {
  44. result = orig_fstat (fd, buf);
  45. }
  46. CATCH_MSVC_INVAL
  47. {
  48. result = -1;
  49. errno = EBADF;
  50. }
  51. DONE_MSVC_INVAL;
  52. return result;
  53. }
  54. #else
  55. # define fstat_nothrow orig_fstat
  56. #endif
  57. int
  58. rpl_fstat (int fd, struct stat *buf)
  59. {
  60. #if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
  61. /* Handle the case when rpl_open() used a dummy file descriptor to work
  62. around an open() that can't normally visit directories. */
  63. const char *name = _gl_directory_name (fd);
  64. if (name != NULL)
  65. return stat (name, buf);
  66. #endif
  67. return fstat_nothrow (fd, buf);
  68. }