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-2011 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 */
23 /* Get the original definition of stat. It might be defined as a macro. */
24 #define __need_system_sys_stat_h
25 #include <sys/types.h>
27 #undef __need_system_sys_stat_h
30 orig_stat (const char *filename, struct stat *buf)
32 return stat (filename, buf);
44 /* Store information about NAME into ST. Work around bugs with
45 trailing slashes. Mingw has other bugs (such as st_ino always
46 being 0 on success) which this wrapper does not work around. But
47 at least this implementation provides the ability to emulate fchdir
51 rpl_stat (char const *name, struct stat *st)
53 int result = orig_stat (name, st);
54 #if REPLACE_FUNC_STAT_FILE
55 /* Solaris 9 mistakenly succeeds when given a non-directory with a
57 if (result == 0 && !S_ISDIR (st->st_mode))
59 size_t len = strlen (name);
60 if (ISSLASH (name[len - 1]))
66 #endif /* REPLACE_FUNC_STAT_FILE */
67 #if REPLACE_FUNC_STAT_DIR
68 if (result == -1 && errno == ENOENT)
70 /* Due to mingw's oddities, there are some directories (like
71 c:\) where stat() only succeeds with a trailing slash, and
72 other directories (like c:\windows) where stat() only
73 succeeds without a trailing slash. But we want the two to be
74 synonymous, since chdir() manages either style. Likewise, Mingw also
75 reports ENOENT for names longer than PATH_MAX, when we want
76 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
77 Fortunately, mingw PATH_MAX is small enough for stack
79 char fixed_name[PATH_MAX + 1] = {0};
80 size_t len = strlen (name);
81 bool check_dir = false;
86 strcpy (fixed_name, name);
87 if (ISSLASH (fixed_name[len - 1]))
90 while (len && ISSLASH (fixed_name[len - 1]))
91 fixed_name[--len] = '\0';
96 fixed_name[len++] = '/';
97 result = orig_stat (fixed_name, st);
98 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
105 #endif /* REPLACE_FUNC_STAT_DIR */