1 /* Work around platform bugs in stat.
2 Copyright (C) 2009-2011 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 /* written by Eric Blake */
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_stat doesn't recurse to
22 #define __need_system_sys_stat_h
25 /* Get the original definition of stat. It might be defined as a macro. */
26 #include <sys/types.h>
28 #undef __need_system_sys_stat_h
31 orig_stat (const char *filename, struct stat *buf)
33 return stat (filename, buf);
37 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
38 eliminates this include because of the preliminary #include <sys/stat.h>
49 /* Store information about NAME into ST. Work around bugs with
50 trailing slashes. Mingw has other bugs (such as st_ino always
51 being 0 on success) which this wrapper does not work around. But
52 at least this implementation provides the ability to emulate fchdir
56 rpl_stat (char const *name, struct stat *st)
58 int result = orig_stat (name, st);
59 #if REPLACE_FUNC_STAT_FILE
60 /* Solaris 9 mistakenly succeeds when given a non-directory with a
62 if (result == 0 && !S_ISDIR (st->st_mode))
64 size_t len = strlen (name);
65 if (ISSLASH (name[len - 1]))
71 #endif /* REPLACE_FUNC_STAT_FILE */
72 #if REPLACE_FUNC_STAT_DIR
73 /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
74 have a constant PATH_MAX. */
76 # error "Please port this replacement to your platform"
79 if (result == -1 && errno == ENOENT)
81 /* Due to mingw's oddities, there are some directories (like
82 c:\) where stat() only succeeds with a trailing slash, and
83 other directories (like c:\windows) where stat() only
84 succeeds without a trailing slash. But we want the two to be
85 synonymous, since chdir() manages either style. Likewise, Mingw also
86 reports ENOENT for names longer than PATH_MAX, when we want
87 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
88 Fortunately, mingw PATH_MAX is small enough for stack
90 char fixed_name[PATH_MAX + 1] = {0};
91 size_t len = strlen (name);
92 bool check_dir = false;
93 verify (PATH_MAX <= 4096);
98 strcpy (fixed_name, name);
99 if (ISSLASH (fixed_name[len - 1]))
102 while (len && ISSLASH (fixed_name[len - 1]))
103 fixed_name[--len] = '\0';
108 fixed_name[len++] = '/';
109 result = orig_stat (fixed_name, st);
110 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
117 #endif /* REPLACE_FUNC_STAT_DIR */