libguile/Makefile.am (snarfcppopts): Remove CFLAGS
[bpt/guile.git] / lib / stat.c
CommitLineData
8912421c 1/* Work around platform bugs in stat.
5e69ceb7 2 Copyright (C) 2009-2014 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
35428fb6
LC
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
21 rpl_stat. */
22#define __need_system_sys_stat_h
8912421c
LC
23#include <config.h>
24
25/* Get the original definition of stat. It might be defined as a macro. */
8912421c
LC
26#include <sys/types.h>
27#include <sys/stat.h>
28#undef __need_system_sys_stat_h
29
005de2e8
LC
30#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
31# if _GL_WINDOWS_64_BIT_ST_SIZE
32# undef stat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
33# define stat _stati64
34# define REPLACE_FUNC_STAT_DIR 1
35# undef REPLACE_FUNC_STAT_FILE
36# elif REPLACE_FUNC_STAT_FILE
37/* mingw64 has a broken stat() function, based on _stat(), in libmingwex.a.
38 Bypass it. */
39# define stat _stat
40# define REPLACE_FUNC_STAT_DIR 1
41# undef REPLACE_FUNC_STAT_FILE
42# endif
43#endif
44
af07e104 45static int
8912421c
LC
46orig_stat (const char *filename, struct stat *buf)
47{
48 return stat (filename, buf);
49}
50
51/* Specification. */
35428fb6
LC
52/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
53 eliminates this include because of the preliminary #include <sys/stat.h>
54 above. */
55#include "sys/stat.h"
8912421c
LC
56
57#include <errno.h>
58#include <limits.h>
59#include <stdbool.h>
60#include <string.h>
dd36ce77 61#include "dosname.h"
231c0e0e 62#include "verify.h"
8912421c 63
35428fb6
LC
64#if REPLACE_FUNC_STAT_DIR
65# include "pathmax.h"
66 /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
67 have a constant PATH_MAX. */
68# ifndef PATH_MAX
69# error "Please port this replacement to your platform"
70# endif
71#endif
72
8912421c
LC
73/* Store information about NAME into ST. Work around bugs with
74 trailing slashes. Mingw has other bugs (such as st_ino always
75 being 0 on success) which this wrapper does not work around. But
76 at least this implementation provides the ability to emulate fchdir
77 correctly. */
78
79int
80rpl_stat (char const *name, struct stat *st)
81{
82 int result = orig_stat (name, st);
83#if REPLACE_FUNC_STAT_FILE
84 /* Solaris 9 mistakenly succeeds when given a non-directory with a
85 trailing slash. */
86 if (result == 0 && !S_ISDIR (st->st_mode))
87 {
88 size_t len = strlen (name);
89 if (ISSLASH (name[len - 1]))
1cd4fffc
LC
90 {
91 errno = ENOTDIR;
92 return -1;
93 }
8912421c
LC
94 }
95#endif /* REPLACE_FUNC_STAT_FILE */
96#if REPLACE_FUNC_STAT_DIR
231c0e0e 97
8912421c
LC
98 if (result == -1 && errno == ENOENT)
99 {
100 /* Due to mingw's oddities, there are some directories (like
101 c:\) where stat() only succeeds with a trailing slash, and
102 other directories (like c:\windows) where stat() only
103 succeeds without a trailing slash. But we want the two to be
104 synonymous, since chdir() manages either style. Likewise, Mingw also
105 reports ENOENT for names longer than PATH_MAX, when we want
106 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
107 Fortunately, mingw PATH_MAX is small enough for stack
108 allocation. */
109 char fixed_name[PATH_MAX + 1] = {0};
110 size_t len = strlen (name);
111 bool check_dir = false;
231c0e0e 112 verify (PATH_MAX <= 4096);
8912421c
LC
113 if (PATH_MAX <= len)
114 errno = ENAMETOOLONG;
115 else if (len)
116 {
117 strcpy (fixed_name, name);
118 if (ISSLASH (fixed_name[len - 1]))
119 {
120 check_dir = true;
121 while (len && ISSLASH (fixed_name[len - 1]))
122 fixed_name[--len] = '\0';
123 if (!len)
124 fixed_name[0] = '/';
125 }
126 else
127 fixed_name[len++] = '/';
128 result = orig_stat (fixed_name, st);
129 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
130 {
131 result = -1;
132 errno = ENOTDIR;
133 }
134 }
135 }
136#endif /* REPLACE_FUNC_STAT_DIR */
137 return result;
138}