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