annotate lib/fstat.c @ 15781:16f47458947e

New module 'fstat'. * lib/sys_stat.in.h (fstat): Declare only if GNULIB_FSTAT is set. * lib/fstat.c: New file, based on a piece of lib/fchdir.c. * lib/fchdir.c (rpl_fstat): Remove function. * m4/fstat.m4: New file. * m4/fchdir.m4 (gl_FUNC_FCHDIR): Don't set REPLACE_FSTAT. * m4/sys_stat_h.m4 (gl_HEADER_SYS_STAT_H): Test whether fstat is declared. (gl_SYS_STAT_H_DEFAULTS): Initialize GNULIB_FSTAT. * modules/sys_stat (Makefile.am): Substitute GNULIB_FSTAT. * modules/fstat: New file. * modules/sys_stat-tests (Depends-on): Remove fstat-tests. * tests/test-sys_stat-c++.cc (fstat): Check only if GNULIB_TEST_FSTAT is set. * doc/posix-functions/fstat.texi: Mention the new module and the problem on MSVC. * NEWS: Mention the change. * modules/acl (Depends-on): Add fstat. * modules/chdir-safer (Depends-on): Likewise. * modules/chown (Depends-on): Likewise. * modules/copy-file (Depends-on): Likewise. * modules/fchdir (Depends-on): Likewise. * modules/fdopendir (Depends-on): Likewise. * modules/fopen (Depends-on): Likewise. * modules/fts (Depends-on): Likewise. * modules/getcwd (Depends-on): Likewise. * modules/isapipe (Depends-on): Likewise. * modules/linkat (Depends-on): Likewise. * modules/lseek (Depends-on): Likewise. * modules/mkdir-p (Depends-on): Likewise. * modules/open (Depends-on): Likewise. * modules/openat (Depends-on): Likewise. * modules/read-file (Depends-on): Likewise. * modules/renameat (Depends-on): Likewise. * modules/utimens (Depends-on): Likewise.
author Bruno Haible <bruno@clisp.org>
date Sun, 25 Sep 2011 13:42:08 +0200
parents
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15781
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* fstat() replacement.
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2011 Free Software Foundation, Inc.
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* If the user's config.h happens to include <sys/stat.h>, let it include only
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 the system's <sys/stat.h> here, so that orig_fstat doesn't recurse to
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 rpl_fstat. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #define __need_system_sys_stat_h
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <config.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Get the original definition of fstat. It might be defined as a macro. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <sys/types.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <sys/stat.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #undef __need_system_sys_stat_h
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 static inline int
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 orig_fstat (int fd, struct stat *buf)
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 {
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 return fstat (fd, buf);
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 }
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Specification. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 eliminates this include because of the preliminary #include <sys/stat.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 above. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #include "sys/stat.h"
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include <errno.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include <unistd.h>
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 # include "msvc-inval.h"
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #endif
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 static inline int
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 fstat_nothrow (int fd, struct stat *buf)
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 {
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int result;
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 TRY_MSVC_INVAL
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 {
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 result = orig_fstat (fd, buf);
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 }
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 CATCH_MSVC_INVAL
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 result = -1;
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 errno = EBADF;
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 }
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 DONE_MSVC_INVAL;
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 return result;
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 }
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #else
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 # define fstat_nothrow orig_fstat
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 #endif
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 int
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 rpl_fstat (int fd, struct stat *buf)
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 {
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 #if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* Handle the case when rpl_open() used a dummy file descriptor to work
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 around an open() that can't normally visit directories. */
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 const char *name = _gl_directory_name (fd);
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 if (name != NULL)
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 return stat (name, buf);
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 #endif
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 return fstat_nothrow (fd, buf);
16f47458947e New module 'fstat'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 }