Avoid endless recursions if config.h includes some header files.
[gnulib.git] / lib / stat.c
1 /* Work around platform bugs in stat.
2    Copyright (C) 2009-2011 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 static inline int
31 orig_stat (const char *filename, struct stat *buf)
32 {
33   return stat (filename, buf);
34 }
35
36 /* Specification.  */
37 #include <sys/stat.h>
38
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdbool.h>
42 #include <string.h>
43 #include "dosname.h"
44 #include "verify.h"
45
46 /* Store information about NAME into ST.  Work around bugs with
47    trailing slashes.  Mingw has other bugs (such as st_ino always
48    being 0 on success) which this wrapper does not work around.  But
49    at least this implementation provides the ability to emulate fchdir
50    correctly.  */
51
52 int
53 rpl_stat (char const *name, struct stat *st)
54 {
55   int result = orig_stat (name, st);
56 #if REPLACE_FUNC_STAT_FILE
57   /* Solaris 9 mistakenly succeeds when given a non-directory with a
58      trailing slash.  */
59   if (result == 0 && !S_ISDIR (st->st_mode))
60     {
61       size_t len = strlen (name);
62       if (ISSLASH (name[len - 1]))
63         {
64           errno = ENOTDIR;
65           return -1;
66         }
67     }
68 #endif /* REPLACE_FUNC_STAT_FILE */
69 #if REPLACE_FUNC_STAT_DIR
70   /* The only known systems where REPLACE_FUNC_STAT_DIR is needed also
71      have a constant PATH_MAX.  */
72 # ifndef PATH_MAX
73 #  error "Please port this replacement to your platform"
74 # endif
75
76   if (result == -1 && errno == ENOENT)
77     {
78       /* Due to mingw's oddities, there are some directories (like
79          c:\) where stat() only succeeds with a trailing slash, and
80          other directories (like c:\windows) where stat() only
81          succeeds without a trailing slash.  But we want the two to be
82          synonymous, since chdir() manages either style.  Likewise, Mingw also
83          reports ENOENT for names longer than PATH_MAX, when we want
84          ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
85          Fortunately, mingw PATH_MAX is small enough for stack
86          allocation.  */
87       char fixed_name[PATH_MAX + 1] = {0};
88       size_t len = strlen (name);
89       bool check_dir = false;
90       verify (PATH_MAX <= 4096);
91       if (PATH_MAX <= len)
92         errno = ENAMETOOLONG;
93       else if (len)
94         {
95           strcpy (fixed_name, name);
96           if (ISSLASH (fixed_name[len - 1]))
97             {
98               check_dir = true;
99               while (len && ISSLASH (fixed_name[len - 1]))
100                 fixed_name[--len] = '\0';
101               if (!len)
102                 fixed_name[0] = '/';
103             }
104           else
105             fixed_name[len++] = '/';
106           result = orig_stat (fixed_name, st);
107           if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
108             {
109               result = -1;
110               errno = ENOTDIR;
111             }
112         }
113     }
114 #endif /* REPLACE_FUNC_STAT_DIR */
115   return result;
116 }