4 * Symbolic constants for opening and creating files, also stat, fstat and
9 * Written by Rob Savoye <rob@cygnus.com>
10 * Copyright (C) 1997-2001, 2003-2005, 2007, 2016, MinGW.org Project.
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
20 * The above copyright notice, this permission notice, and the following
21 * disclaimer shall be included in all copies or substantial portions of
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
30 * DEALINGS IN THE SOFTWARE.
34 #pragma GCC system_header
36 /* To support selective (partial) inclusion by <wchar.h>...
38 #ifndef __WCHAR_H_SOURCED__
39 /* ...we defer the definition of the normal multiple inclusion guard macro,
40 * until we know that this is NOT the <wchar.h> selective inclusion case.
44 /* All MinGW headers are required to include <_mingw.h>; however, Microsoft
45 * also stipulate that USERS MUST include <sys/types.h>, BEFORE they include
46 * <sys/stat.h>. This is not only appallingly bad software engineering, on
47 * Microsoft's part, but it is a potential obstacle to portability of POSIX
48 * source code; (POSIX requires that <sys/stat.h> should be self-contained,
49 * with no requirement for any specific header inclusion order). Although
50 * it is more inclusive that POSIX requires, we may mitigate the deficiency
51 * inherent in Microsoft's poor software engineering, by simply including
52 * <sys/types.h> here; in so doing, we may also delegate the inclusion of
53 * <_mingw.h>, and the definition of all data types required herein, to...
55 #include <sys/types.h>
57 /* Constants for the st_mode member of struct stat, and its Microsoft
60 #define _S_IFIFO 0x1000 /* FIFO */
61 #define _S_IFCHR 0x2000 /* Character */
62 #define _S_IFBLK 0x3000 /* Block: Is this ever set under w32? */
63 #define _S_IFDIR 0x4000 /* Directory */
64 #define _S_IFREG 0x8000 /* Regular */
66 #define _S_IFMT 0xF000 /* File type mask */
68 #define _S_IEXEC 0x0040
69 #define _S_IWRITE 0x0080
70 #define _S_IREAD 0x0100
72 #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
73 #define _S_IXUSR _S_IEXEC
74 #define _S_IWUSR _S_IWRITE
75 #define _S_IRUSR _S_IREAD
77 #define _S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
78 #define _S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
79 #define _S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR)
80 #define _S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK)
81 #define _S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
85 #define S_IFIFO _S_IFIFO
86 #define S_IFCHR _S_IFCHR
87 #define S_IFBLK _S_IFBLK
88 #define S_IFDIR _S_IFDIR
89 #define S_IFREG _S_IFREG
90 #define S_IFMT _S_IFMT
91 #define S_IEXEC _S_IEXEC
92 #define S_IWRITE _S_IWRITE
93 #define S_IREAD _S_IREAD
94 #define S_IRWXU _S_IRWXU
95 #define S_IXUSR _S_IXUSR
96 #define S_IWUSR _S_IWUSR
97 #define S_IRUSR _S_IRUSR
99 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
100 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
101 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
102 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
103 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
105 #endif /* !_NO_OLDNAMES */
106 #endif /* !__WCHAR_H_SOURCED__ */
109 #ifndef __struct_stat_defined
110 /* The structure manipulated and returned by stat() and fstat(); note that
111 * expansion of the macro provided below will yield variants of struct stat
112 * to conform with Microsoft's usage, (and POSIX usage up to and including
113 * POSIX.1-2001, but NOT the extended specification of POSIX.1-2008).
115 * NOTE: If called on a directory the values in the time fields are not only
116 * invalid, they will cause localtime et. al. to return NULL. And calling
117 * asctime with a NULL pointer causes an Invalid Page Fault. So watch it!
119 #define __struct_stat_defined(__st_off_t, __st_time_t) \
120 { _dev_t st_dev; /* Equivalent to drive number 0=A 1=B ... */ \
121 _ino_t st_ino; /* Always zero ? */ \
122 _mode_t st_mode; /* See above constants */ \
123 short st_nlink; /* Number of links. */ \
124 short st_uid; /* User: Maybe significant on NT ? */ \
125 short st_gid; /* Group: Ditto */ \
126 _dev_t st_rdev; /* Seems useless (not even filled in) */ \
127 __st_off_t st_size; /* File size in bytes */ \
128 __st_time_t st_atime; /* Access time (always 00:00 on FAT) */ \
129 __st_time_t st_mtime; /* Modified time */ \
130 __st_time_t st_ctime; /* Creation time */ \
133 /* Here, we expand the preceding macro to yield the actual definition
134 * of struct stat, under its current Microsoft "uglified" name...
136 struct _stat __struct_stat_defined( _off_t, time_t );
139 /* ...while this alternative expansion yields its standard POSIX name,
140 * (and its original Microsoft name); apart from its name, this must be
141 * defined identically to struct _stat above.
143 struct stat __struct_stat_defined( _off_t, time_t );
144 #endif /* !_NO_OLDNAMES */
146 #if defined __MSVCRT__
147 /* This variant of struct stat is required to support the use of the
148 * _stati64() function, which is provided by MSVCRT.DLL, but was not
149 * present in CRTDLL.DLL...
151 struct _stati64 __struct_stat_defined( __off64_t, time_t );
153 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
154 /* ...while this supports the use of the _stat64() function, introduced
155 * by MSVCR61.DLL, and subsequently added to MSVCRT.DLL for releases from
158 struct __stat64 __struct_stat_defined( __off64_t, __time64_t );
160 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
161 /* ...and these are specific to additional function variants, added to
162 * the non-free MSVCR80.DLL, and its later derivatives, but not present
163 * in MSVCRT.DLL (or CRTDLL.DLL).
165 struct __stat32 __struct_stat_defined( __off32_t, __time32_t );
166 struct _stat32i64 __struct_stat_defined( __off64_t, __time32_t );
167 struct _stat64i32 __struct_stat_defined( __off32_t, __time64_t );
169 #endif /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
170 #endif /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
171 #endif /* __MSVCRT__ */
173 /* From here on, it is sufficient to leave __struct_stat_defined as
174 * a macro which expands to nothing.
176 #undef __struct_stat_defined
177 #define __struct_stat_defined
179 #endif /* !__struct_stat_defined */
184 /* This set of function prototypes are to be declared only when
185 * <sys/stat.h> is included directly.
187 _CRTIMP __cdecl __MINGW_NOTHROW int _umask (int);
188 _CRTIMP __cdecl __MINGW_NOTHROW int _chmod (const char *, int);
190 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
191 /* This pair of functions are present in all versions of MSVCRT.DLL, but
192 * they are NOT present in MSVCR80.DLL, nor in any of its later non-free
193 * variants, all of which rely on inline aliases (defined below).
195 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat (int, struct _stat *);
196 _CRTIMP __cdecl __MINGW_NOTHROW int _stat (const char *, struct _stat *);
197 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
200 /* These are the standard POSIX names, (and the original Microsoft names),
201 * for the preceding four functions.
203 _CRTIMP __cdecl __MINGW_NOTHROW int umask (int);
204 _CRTIMP __cdecl __MINGW_NOTHROW int chmod (const char *, int);
206 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
207 /* Since the underlying functions, with "uglified" names, are not supported
208 * by MSVCR80.DLL and its later derivitaves, there is also nothing to which
209 * to map these originally named alternatives; declare prototypes only when
210 * using DLL versions which can support them, while falling back to the use
211 * of inline replacements (defined below) in the unsupported cases.
213 _CRTIMP __cdecl __MINGW_NOTHROW int fstat (int, struct stat *);
214 _CRTIMP __cdecl __MINGW_NOTHROW int stat (const char *, struct stat *);
215 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
216 #endif /* !_NO_OLDNAMES */
218 #if defined __MSVCRT__
219 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
220 /* This pair of functions were withdrawn from MSVCR80.DLL, and its later
221 * derivatives, but remain in all versions of MSVCRT.DLL
223 _CRTIMP __cdecl __MINGW_NOTHROW int _fstati64 (int, struct _stati64 *);
224 _CRTIMP __cdecl __MINGW_NOTHROW int _stati64 (const char *, struct _stati64 *);
225 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
227 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
228 /* This pair of functions were introduced in MSVCR61.DLL, and were subsequently
229 * added to MSVCRT.DLL from the release accompanying Win2K onwards...
231 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat64 (int, struct __stat64 *);
232 _CRTIMP __cdecl __MINGW_NOTHROW int _stat64 (const char *, struct __stat64 *);
234 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
235 /* ...whereas this group were introduced in MSVCR80.DLL, and its later
236 * derivatives, but are not present in MSVCRT.DLL
238 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat32 (int, struct __stat32 *);
239 _CRTIMP __cdecl __MINGW_NOTHROW int _stat32 (const char *, struct __stat32 *);
240 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat32i64 (int, struct _stat32i64 *);
241 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat64i32 (int, struct _stat64i32 *);
242 _CRTIMP __cdecl __MINGW_NOTHROW int _stat32i64 (const char *, struct _stat32i64 *);
243 _CRTIMP __cdecl __MINGW_NOTHROW int _stat64i32 (const char *, struct _stat64i32 *);
245 #ifdef _USE_32BIT_TIME_T
246 /* We must provide inline replacements for the four MSVCRT.DLL functions
247 * which have been withdrawn from MSVCR80.DLL, and its later derivatives;
248 * this first set of replacements are compatible with their MSVCRT.DLL
249 * equivalents, but require the user to define _USE_32BIT_TIME_T...
251 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstat (int __v1, struct _stat *__v2)
252 { return _fstat32 (__v1, (struct __stat32 *)(__v2)); }
254 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stat (const char *__v1, struct _stat *__v2)
255 { return _stat32 (__v1, (struct __stat32 *)(__v2)); }
257 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstati64 (int __v1, struct _stati64 *__v2)
258 { return _fstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
260 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stati64 (const char *__v1, struct _stati64 *__v2)
261 { return _stat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
263 #else /* !_USE_32BIT_TIME_T */
264 /* ...whereas, the following alternatives emulate the brain-damaged
265 * behaviour of Microsoft's own implementations, which take effect when
266 * the user does not define _USE_32BIT_TIME_T; they break compatibility
269 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstat (int __v1, struct _stat *__v2)
270 { return _fstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
272 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stat (const char *__v1, struct _stat *__v2)
273 { return _stat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
275 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstati64 (int __v1, struct _stati64 *__v2)
276 { return _fstat64 (__v1, (struct __stat64 *)(__v2)); }
278 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stati64 (const char *__v1, struct _stati64 *__v2)
279 { return _stat64 (__v1,(struct __stat64*)(__v2)); }
280 #endif /* !_USE_32BIT_TIME_T */
283 /* Irrespective of the state of _USE_32BIT_TIME_T, we may provide inline
284 * replacements for the stat() and fstat() functions, (which are missing
285 * from MSVCR80.DLL and its later derivatives), simply by aliasing them
286 * to their corresponding replacements with "uglified" names.
288 __CRT_ALIAS __cdecl __MINGW_NOTHROW int fstat (int __v1, struct _stat *__v2)
289 { return _fstat (__v1, __v2); }
291 __CRT_ALIAS __cdecl __MINGW_NOTHROW int stat (int __v1, struct _stat *__v2)
292 { return _stat (__v1, __v2); }
294 #endif /* !_NO_OLDNAMES */
295 #endif /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
296 #endif /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
297 #endif /* __MSVCRT__ */
298 #endif /* _SYS_STAT_H */
300 #if defined __MSVCRT__ && !(defined _SYS_STAT_H && defined _WCHAR_H)
301 /* This final group of function prototypes, specific to MSVCRT.DLL and its
302 * non-free derivatives, are to be declared both when <sys/stat.h> is included
303 * directly, and when it is selectively included by <wchar.h>; however, if both
304 * _SYS_STAT_H and _WCHAR_H are defined, by the time we get to here, then this
305 * must be the direct inclusion case, after having already declared these via
306 * selective inclusion by <wchar.h>, and we should not declare these again;
307 * (in particular, we should not repeat inline function implementations).
309 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
310 /* As is the case for their regular counterparts, this pair of functions
311 * remain available in MSVCRT.DLL itself, but they are not exported from its
312 * non-free derivatives from MSVCR80.DLL onwards, whence it is expected that
313 * they will be replaced by inline implementations.
315 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat(const wchar_t *, struct _stat *);
316 _CRTIMP __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *, struct _stati64 *);
317 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
319 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
320 /* Similarly, this variant was introduced in MSVCR80.DLL, and was subsequently
321 * added to MSVCRT.DLL with the release of Win2K...
323 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat64 (const wchar_t *, struct __stat64 *);
325 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
326 /* ...whereas these variants are exclusive to the non-free MSVCR80.DLL, and
327 * its later derivatives; they are not available in MSVCRT.DLL.
329 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat32 (const wchar_t *, struct __stat32 *);
330 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat32i64 (const wchar_t *, struct _stat32i64 *);
331 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat64i32 (const wchar_t *, struct _stat64i32 *);
333 #ifdef _USE_32BIT_TIME_T
334 /* Once again, we must furnish inline replacements for the functions which
335 * were withdrawn from MSVCR80.DLL and its later derivatives; these are the
336 * implementations which remain compatible with MSVCRT.DLL, but require the
337 * user to define _USE_32BIT_TIME_T...
339 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstat (const wchar_t *__v1, struct _stat *__v2)
340 { return _wstat32 (__v1, (struct __stat32 *)(__v2)); }
342 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
343 { return _wstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
345 #else /* !_USE_32BIT_TIME_T */
346 /* ...whereas these emulate the brain-damaged Microsoft behaviour, for the
347 * case when the user does not define _USE_32BIT_TIME_T, breaking MSVCRT.DLL
350 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstat (const wchar_t *__v1, struct _stat *__v2)
351 { return _wstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
353 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
354 { return _wstat64 (__v1, (struct __stat64 *)(__v2)); }
356 #endif /* !_USE_32BIT_TIME_T */
357 #endif /* __MSVCRT_VERSION__ >= 0x0800 */
358 #endif /* __MSVCRT_VERSION__ >= 0x0601 */
359 #endif /* __MSVCRT__ && !(_SYS_STAT_H && _WCHAR_H) */
363 #endif /* ! RC_INVOKED */
364 #endif /* !_SYS_STAT__H: $RCSfile$: end of file */