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, 2010 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);
43 /* Store information about NAME into ST. Work around bugs with
44 trailing slashes. Mingw has other bugs (such as st_ino always
45 being 0 on success) which this wrapper does not work around. But
46 at least this implementation provides the ability to emulate fchdir
50 rpl_stat (char const *name, struct stat *st)
52 int result = orig_stat (name, st);
53 #if REPLACE_FUNC_STAT_FILE
54 /* Solaris 9 mistakenly succeeds when given a non-directory with a
56 if (result == 0 && !S_ISDIR (st->st_mode))
58 size_t len = strlen (name);
59 if (ISSLASH (name[len - 1]))
65 #endif /* REPLACE_FUNC_STAT_FILE */
66 #if REPLACE_FUNC_STAT_DIR
67 if (result == -1 && errno == ENOENT)
69 /* Due to mingw's oddities, there are some directories (like
70 c:\) where stat() only succeeds with a trailing slash, and
71 other directories (like c:\windows) where stat() only
72 succeeds without a trailing slash. But we want the two to be
73 synonymous, since chdir() manages either style. Likewise, Mingw also
74 reports ENOENT for names longer than PATH_MAX, when we want
75 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
76 Fortunately, mingw PATH_MAX is small enough for stack
78 char fixed_name[PATH_MAX + 1] = {0};
79 size_t len = strlen (name);
80 bool check_dir = false;
85 strcpy (fixed_name, name);
86 if (ISSLASH (fixed_name[len - 1]))
89 while (len && ISSLASH (fixed_name[len - 1]))
90 fixed_name[--len] = '\0';
95 fixed_name[len++] = '/';
96 result = orig_stat (fixed_name, st);
97 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
104 #endif /* REPLACE_FUNC_STAT_DIR */