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