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