4 * Symbolic constants for opening and creating files, also stat, fstat and
9 * Written by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
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_IFDIR 0x4000 /* Directory */
63 #define _S_IFREG 0x8000 /* Regular */
65 #ifdef _MINGW_S_IFBLK_KLUDGE
66 /* For preference, this kludge should NOT be enabled; for rationale,
67 * see: https://sourceforge.net/p/mingw/bugs/1146
69 * MS-Windows doesn't support testing for block special devices via the
70 * st_mode flags; ideally, client code to be ported to Windows should not
71 * blindly assume that S_IFBLK (or _S_IFBLK) is defined, but should rather
72 * check for it, and compile dependent code conditionally. Notwithstanding,
73 * this kludge allows the user to force a definition, which we arbitrarily
74 * choose to ensure that S_ISBLK (or _S_ISBLK) always returns FALSE, (i.e.
75 * choose a value such that _S_IFBLK & _S_IFMT can NEVER equal _S_IFBLK).
77 #define _S_IFBLK 0x3001 /* Block: unsupported on Win32 */
80 #define _S_IFMT 0xF000 /* File type mask */
82 #define _S_IEXEC 0x0040
83 #define _S_IWRITE 0x0080
84 #define _S_IREAD 0x0100
86 #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
87 #define _S_IXUSR _S_IEXEC
88 #define _S_IWUSR _S_IWRITE
89 #define _S_IRUSR _S_IREAD
91 #define _S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
92 #define _S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
93 #define _S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR)
94 #define _S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK)
95 #define _S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
99 #define S_IFIFO _S_IFIFO
100 #define S_IFCHR _S_IFCHR
102 #define S_IFBLK _S_IFBLK
104 #define S_IFDIR _S_IFDIR
105 #define S_IFREG _S_IFREG
106 #define S_IFMT _S_IFMT
107 #define S_IEXEC _S_IEXEC
108 #define S_IWRITE _S_IWRITE
109 #define S_IREAD _S_IREAD
110 #define S_IRWXU _S_IRWXU
111 #define S_IXUSR _S_IXUSR
112 #define S_IWUSR _S_IWUSR
113 #define S_IRUSR _S_IRUSR
115 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
116 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
117 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
118 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
119 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
121 #endif /* !_NO_OLDNAMES */
124 /* When the _S_IFBLK kludge is NOT enabled, (as it ideally should not be),
125 * ensure that any attempt to use its dependent macros is firmly denied.
127 # pragma GCC poison _S_ISBLK
128 # pragma GCC poison S_ISBLK
130 #endif /* !_S_IFBLK */
131 #endif /* !__WCHAR_H_SOURCED__ */
134 #ifndef __struct_stat_defined
135 /* The structure manipulated and returned by stat() and fstat(); note that
136 * expansion of the macro provided below will yield variants of struct stat
137 * to conform with Microsoft's usage, (and POSIX usage up to and including
138 * POSIX.1-2001, but NOT the extended specification of POSIX.1-2008).
140 * NOTE: If called on a directory the values in the time fields are not only
141 * invalid, they will cause localtime et. al. to return NULL. And calling
142 * asctime with a NULL pointer causes an Invalid Page Fault. So watch it!
144 #define __struct_stat_defined(__st_off_t, __st_time_t) \
145 { _dev_t st_dev; /* Equivalent to drive number 0=A 1=B ... */ \
146 _ino_t st_ino; /* Always zero ? */ \
147 _mode_t st_mode; /* See above constants */ \
148 short st_nlink; /* Number of links. */ \
149 short st_uid; /* User: Maybe significant on NT ? */ \
150 short st_gid; /* Group: Ditto */ \
151 _dev_t st_rdev; /* Seems useless (not even filled in) */ \
152 __st_off_t st_size; /* File size in bytes */ \
153 __st_time_t st_atime; /* Access time (always 00:00 on FAT) */ \
154 __st_time_t st_mtime; /* Modified time */ \
155 __st_time_t st_ctime; /* Creation time */ \
158 /* Here, we expand the preceding macro to yield the actual definition
159 * of struct stat, under its current Microsoft "uglified" name...
161 struct _stat __struct_stat_defined( _off_t, time_t );
164 /* ...while this alternative expansion yields its standard POSIX name,
165 * (and its original Microsoft name); apart from its name, this must be
166 * defined identically to struct _stat above.
168 struct stat __struct_stat_defined( _off_t, time_t );
169 #endif /* !_NO_OLDNAMES */
171 #if defined __MSVCRT__
172 /* This variant of struct stat is required to support the use of the
173 * _stati64() function, which is provided by MSVCRT.DLL, but was not
174 * present in CRTDLL.DLL...
176 struct _stati64 __struct_stat_defined( __off64_t, time_t );
178 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
179 /* ...while this supports the use of the _stat64() function, introduced
180 * by MSVCR61.DLL, and subsequently added to MSVCRT.DLL for releases from
183 struct __stat64 __struct_stat_defined( __off64_t, __time64_t );
185 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
186 /* ...and these are specific to additional function variants, added to
187 * the non-free MSVCR80.DLL, and its later derivatives, but not present
188 * in MSVCRT.DLL (or CRTDLL.DLL).
190 struct __stat32 __struct_stat_defined( __off32_t, __time32_t );
191 struct _stat32i64 __struct_stat_defined( __off64_t, __time32_t );
192 struct _stat64i32 __struct_stat_defined( __off32_t, __time64_t );
194 #endif /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
195 #endif /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
196 #endif /* __MSVCRT__ */
198 /* From here on, it is sufficient to leave __struct_stat_defined as
199 * a macro which expands to nothing.
201 #undef __struct_stat_defined
202 #define __struct_stat_defined
204 #endif /* !__struct_stat_defined */
209 /* This set of function prototypes are to be declared only when
210 * <sys/stat.h> is included directly.
212 _CRTIMP __cdecl __MINGW_NOTHROW int _umask (int);
213 _CRTIMP __cdecl __MINGW_NOTHROW int _chmod (const char *, int);
215 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
216 /* This pair of functions are present in all versions of MSVCRT.DLL, but
217 * they are NOT present in MSVCR80.DLL, nor in any of its later non-free
218 * variants, all of which rely on inline aliases (defined below).
220 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat (int, struct _stat *);
221 _CRTIMP __cdecl __MINGW_NOTHROW int _stat (const char *, struct _stat *);
222 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
225 /* These are the standard POSIX names, (and the original Microsoft names),
226 * for the preceding four functions.
228 _CRTIMP __cdecl __MINGW_NOTHROW int umask (int);
229 _CRTIMP __cdecl __MINGW_NOTHROW int chmod (const char *, int);
231 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
232 /* Since the underlying functions, with "uglified" names, are not supported
233 * by MSVCR80.DLL and its later derivitaves, there is also nothing to which
234 * to map these originally named alternatives; declare prototypes only when
235 * using DLL versions which can support them, while falling back to the use
236 * of inline replacements (defined below) in the unsupported cases.
238 _CRTIMP __cdecl __MINGW_NOTHROW int fstat (int, struct stat *);
239 _CRTIMP __cdecl __MINGW_NOTHROW int stat (const char *, struct stat *);
240 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
241 #endif /* !_NO_OLDNAMES */
243 #if defined __MSVCRT__
244 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
245 /* This pair of functions were withdrawn from MSVCR80.DLL, and its later
246 * derivatives, but remain in all versions of MSVCRT.DLL
248 _CRTIMP __cdecl __MINGW_NOTHROW int _fstati64 (int, struct _stati64 *);
249 _CRTIMP __cdecl __MINGW_NOTHROW int _stati64 (const char *, struct _stati64 *);
250 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
252 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
253 /* This pair of functions were introduced in MSVCR61.DLL, and were subsequently
254 * added to MSVCRT.DLL from the release accompanying Win2K onwards...
256 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat64 (int, struct __stat64 *);
257 _CRTIMP __cdecl __MINGW_NOTHROW int _stat64 (const char *, struct __stat64 *);
259 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
260 /* ...whereas this group were introduced in MSVCR80.DLL, and its later
261 * derivatives, but are not present in MSVCRT.DLL
263 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat32 (int, struct __stat32 *);
264 _CRTIMP __cdecl __MINGW_NOTHROW int _stat32 (const char *, struct __stat32 *);
265 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat32i64 (int, struct _stat32i64 *);
266 _CRTIMP __cdecl __MINGW_NOTHROW int _fstat64i32 (int, struct _stat64i32 *);
267 _CRTIMP __cdecl __MINGW_NOTHROW int _stat32i64 (const char *, struct _stat32i64 *);
268 _CRTIMP __cdecl __MINGW_NOTHROW int _stat64i32 (const char *, struct _stat64i32 *);
270 #ifdef _USE_32BIT_TIME_T
271 /* We must provide inline replacements for the four MSVCRT.DLL functions
272 * which have been withdrawn from MSVCR80.DLL, and its later derivatives;
273 * this first set of replacements are compatible with their MSVCRT.DLL
274 * equivalents, but require the user to define _USE_32BIT_TIME_T...
276 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstat (int __v1, struct _stat *__v2)
277 { return _fstat32 (__v1, (struct __stat32 *)(__v2)); }
279 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stat (const char *__v1, struct _stat *__v2)
280 { return _stat32 (__v1, (struct __stat32 *)(__v2)); }
282 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstati64 (int __v1, struct _stati64 *__v2)
283 { return _fstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
285 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stati64 (const char *__v1, struct _stati64 *__v2)
286 { return _stat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
288 #else /* !_USE_32BIT_TIME_T */
289 /* ...whereas, the following alternatives emulate the brain-damaged
290 * behaviour of Microsoft's own implementations, which take effect when
291 * the user does not define _USE_32BIT_TIME_T; they break compatibility
294 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstat (int __v1, struct _stat *__v2)
295 { return _fstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
297 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stat (const char *__v1, struct _stat *__v2)
298 { return _stat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
300 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _fstati64 (int __v1, struct _stati64 *__v2)
301 { return _fstat64 (__v1, (struct __stat64 *)(__v2)); }
303 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _stati64 (const char *__v1, struct _stati64 *__v2)
304 { return _stat64 (__v1,(struct __stat64*)(__v2)); }
305 #endif /* !_USE_32BIT_TIME_T */
308 /* Irrespective of the state of _USE_32BIT_TIME_T, we may provide inline
309 * replacements for the stat() and fstat() functions, (which are missing
310 * from MSVCR80.DLL and its later derivatives), simply by aliasing them
311 * to their corresponding replacements with "uglified" names.
313 __CRT_ALIAS __cdecl __MINGW_NOTHROW int fstat (int __v1, struct _stat *__v2)
314 { return _fstat (__v1, __v2); }
316 __CRT_ALIAS __cdecl __MINGW_NOTHROW int stat (const char *__v1, struct _stat *__v2)
317 { return _stat (__v1, __v2); }
319 #endif /* !_NO_OLDNAMES */
320 #endif /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
321 #endif /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
322 #endif /* __MSVCRT__ */
323 #endif /* _SYS_STAT_H */
325 #if defined __MSVCRT__ && !(defined _SYS_STAT_H && defined _WCHAR_H)
326 /* This final group of function prototypes, specific to MSVCRT.DLL and its
327 * non-free derivatives, are to be declared both when <sys/stat.h> is included
328 * directly, and when it is selectively included by <wchar.h>; however, if both
329 * _SYS_STAT_H and _WCHAR_H are defined, by the time we get to here, then this
330 * must be the direct inclusion case, after having already declared these via
331 * selective inclusion by <wchar.h>, and we should not declare these again;
332 * (in particular, we should not repeat inline function implementations).
334 #if __MSVCRT_VERSION__ < __MSVCR80_DLL
335 /* As is the case for their regular counterparts, this pair of functions
336 * remain available in MSVCRT.DLL itself, but they are not exported from its
337 * non-free derivatives from MSVCR80.DLL onwards, whence it is expected that
338 * they will be replaced by inline implementations.
340 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat(const wchar_t *, struct _stat *);
341 _CRTIMP __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *, struct _stati64 *);
342 #endif /* __MSVCRT_VERSION__ < __MSVCR80_DLL */
344 #if __MSVCRT_VERSION__ >= __MSVCR61_DLL || _WIN32_WINNT >= _WIN32_WINNT_WIN2K
345 /* Similarly, this variant was introduced in MSVCR80.DLL, and was subsequently
346 * added to MSVCRT.DLL with the release of Win2K...
348 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat64 (const wchar_t *, struct __stat64 *);
350 #if __MSVCRT_VERSION__ >= __MSVCR80_DLL
351 /* ...whereas these variants are exclusive to the non-free MSVCR80.DLL, and
352 * its later derivatives; they are not available in MSVCRT.DLL.
354 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat32 (const wchar_t *, struct __stat32 *);
355 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat32i64 (const wchar_t *, struct _stat32i64 *);
356 _CRTIMP __cdecl __MINGW_NOTHROW int _wstat64i32 (const wchar_t *, struct _stat64i32 *);
358 #ifdef _USE_32BIT_TIME_T
359 /* Once again, we must furnish inline replacements for the functions which
360 * were withdrawn from MSVCR80.DLL and its later derivatives; these are the
361 * implementations which remain compatible with MSVCRT.DLL, but require the
362 * user to define _USE_32BIT_TIME_T...
364 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstat (const wchar_t *__v1, struct _stat *__v2)
365 { return _wstat32 (__v1, (struct __stat32 *)(__v2)); }
367 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
368 { return _wstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
370 #else /* !_USE_32BIT_TIME_T */
371 /* ...whereas these emulate the brain-damaged Microsoft behaviour, for the
372 * case when the user does not define _USE_32BIT_TIME_T, breaking MSVCRT.DLL
375 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstat (const wchar_t *__v1, struct _stat *__v2)
376 { return _wstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
378 __CRT_ALIAS __cdecl __MINGW_NOTHROW int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
379 { return _wstat64 (__v1, (struct __stat64 *)(__v2)); }
381 #endif /* !_USE_32BIT_TIME_T */
382 #endif /* __MSVCRT_VERSION__ >= 0x0800 */
383 #endif /* __MSVCRT_VERSION__ >= 0x0601 */
384 #endif /* __MSVCRT__ && !(_SYS_STAT_H && _WCHAR_H) */
388 #endif /* ! RC_INVOKED */
389 #endif /* !_SYS_STAT__H: $RCSfile$: end of file */