Update Gnulib to v0.0-5158-g7d06b32; remove `strcase' and `version-etc-fsf'.
[bpt/guile.git] / lib / stat.c
CommitLineData
8912421c 1/* Work around platform bugs in stat.
49114fd4 2 Copyright (C) 2009-2011 Free Software Foundation, Inc.
8912421c
LC
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser 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.
8
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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17/* written by Eric Blake */
18
19#include <config.h>
20
21/* Get the original definition of stat. It might be defined as a macro. */
22#define __need_system_sys_stat_h
23#include <sys/types.h>
24#include <sys/stat.h>
25#undef __need_system_sys_stat_h
26
27static inline int
28orig_stat (const char *filename, struct stat *buf)
29{
30 return stat (filename, buf);
31}
32
33/* Specification. */
34#include <sys/stat.h>
35
36#include <errno.h>
37#include <limits.h>
38#include <stdbool.h>
39#include <string.h>
dd36ce77 40#include "dosname.h"
8912421c
LC
41
42/* Store information about NAME into ST. Work around bugs with
43 trailing slashes. Mingw has other bugs (such as st_ino always
44 being 0 on success) which this wrapper does not work around. But
45 at least this implementation provides the ability to emulate fchdir
46 correctly. */
47
48int
49rpl_stat (char const *name, struct stat *st)
50{
51 int result = orig_stat (name, st);
52#if REPLACE_FUNC_STAT_FILE
53 /* Solaris 9 mistakenly succeeds when given a non-directory with a
54 trailing slash. */
55 if (result == 0 && !S_ISDIR (st->st_mode))
56 {
57 size_t len = strlen (name);
58 if (ISSLASH (name[len - 1]))
1cd4fffc
LC
59 {
60 errno = ENOTDIR;
61 return -1;
62 }
8912421c
LC
63 }
64#endif /* REPLACE_FUNC_STAT_FILE */
65#if REPLACE_FUNC_STAT_DIR
66 if (result == -1 && errno == ENOENT)
67 {
68 /* Due to mingw's oddities, there are some directories (like
69 c:\) where stat() only succeeds with a trailing slash, and
70 other directories (like c:\windows) where stat() only
71 succeeds without a trailing slash. But we want the two to be
72 synonymous, since chdir() manages either style. Likewise, Mingw also
73 reports ENOENT for names longer than PATH_MAX, when we want
74 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
75 Fortunately, mingw PATH_MAX is small enough for stack
76 allocation. */
77 char fixed_name[PATH_MAX + 1] = {0};
78 size_t len = strlen (name);
79 bool check_dir = false;
80 if (PATH_MAX <= len)
81 errno = ENAMETOOLONG;
82 else if (len)
83 {
84 strcpy (fixed_name, name);
85 if (ISSLASH (fixed_name[len - 1]))
86 {
87 check_dir = true;
88 while (len && ISSLASH (fixed_name[len - 1]))
89 fixed_name[--len] = '\0';
90 if (!len)
91 fixed_name[0] = '/';
92 }
93 else
94 fixed_name[len++] = '/';
95 result = orig_stat (fixed_name, st);
96 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
97 {
98 result = -1;
99 errno = ENOTDIR;
100 }
101 }
102 }
103#endif /* REPLACE_FUNC_STAT_DIR */
104 return result;
105}