1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around platform bugs in stat.
4 Copyright (C) 2009-2013 Free Software Foundation, Inc.
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.
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.
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/>. */
19 /* written by Eric Blake */
21 /* If the user's config.h happens to include <sys/stat.h>, let it include only
22 the system's <sys/stat.h> here, so that orig_stat doesn't recurse to
24 #define __need_system_sys_stat_h
27 /* Get the original definition of stat. It might be defined as a macro. */
28 #include <sys/types.h>
30 #undef __need_system_sys_stat_h
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 # if _GL_WINDOWS_64_BIT_ST_SIZE
34 # undef stat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
35 # define stat _stati64
36 # define REPLACE_FUNC_STAT_DIR 1
37 # undef REPLACE_FUNC_STAT_FILE
38 # elif REPLACE_FUNC_STAT_FILE
39 /* mingw64 has a broken stat() function, based on _stat(), in libmingwex.a.
42 # define REPLACE_FUNC_STAT_DIR 1
43 # undef REPLACE_FUNC_STAT_FILE
48 orig_stat (const char *filename, struct stat *buf)
50 return stat (filename, buf);
54 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
55 eliminates this include because of the preliminary #include <sys/stat.h>
66 #if REPLACE_FUNC_STAT_DIR
68 /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
69 have a constant PATH_MAX. */
71 # error "Please port this replacement to your platform"
75 /* Store information about NAME into ST. Work around bugs with
76 trailing slashes. Mingw has other bugs (such as st_ino always
77 being 0 on success) which this wrapper does not work around. But
78 at least this implementation provides the ability to emulate fchdir
82 rpl_stat (char const *name, struct stat *st)
84 int result = orig_stat (name, st);
85 #if REPLACE_FUNC_STAT_FILE
86 /* Solaris 9 mistakenly succeeds when given a non-directory with a
88 if (result == 0 && !S_ISDIR (st->st_mode))
90 size_t len = strlen (name);
91 if (ISSLASH (name[len - 1]))
97 #endif /* REPLACE_FUNC_STAT_FILE */
98 #if REPLACE_FUNC_STAT_DIR
100 if (result == -1 && errno == ENOENT)
102 /* Due to mingw's oddities, there are some directories (like
103 c:\) where stat() only succeeds with a trailing slash, and
104 other directories (like c:\windows) where stat() only
105 succeeds without a trailing slash. But we want the two to be
106 synonymous, since chdir() manages either style. Likewise, Mingw also
107 reports ENOENT for names longer than PATH_MAX, when we want
108 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
109 Fortunately, mingw PATH_MAX is small enough for stack
111 char fixed_name[PATH_MAX + 1] = {0};
112 size_t len = strlen (name);
113 bool check_dir = false;
114 verify (PATH_MAX <= 4096);
116 errno = ENAMETOOLONG;
119 strcpy (fixed_name, name);
120 if (ISSLASH (fixed_name[len - 1]))
123 while (len && ISSLASH (fixed_name[len - 1]))
124 fixed_name[--len] = '\0';
129 fixed_name[len++] = '/';
130 result = orig_stat (fixed_name, st);
131 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
138 #endif /* REPLACE_FUNC_STAT_DIR */