OSDN Git Service

30c18a2816f5f5b508dab3de712b64164ffb8402
[mingw/mingw-org-wsl.git] / mingwrt / include / sys / stat.h
1 /*
2  * stat.h
3  *
4  * Symbolic constants for opening and creating files, also stat, fstat and
5  * chmod functions.
6  *
7  * $Id$
8  *
9  * Written by Rob Savoye <rob@cygnus.com>
10  * Copyright (C) 1997-2001, 2003-2005, 2007, 2016, MinGW.org Project.
11  *
12  *
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:
19  *
20  * The above copyright notice, this permission notice, and the following
21  * disclaimer shall be included in all copies or substantial portions of
22  * the Software.
23  *
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.
31  *
32  */
33 #ifndef _SYS_STAT_H
34 #pragma GCC system_header
35
36 /* To support selective (partial) inclusion by <wchar.h>...
37  */
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.
41  */
42 #define _SYS_STAT_H
43
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...
54  */
55 #include <sys/types.h>
56
57 /* Constants for the st_mode member of struct stat, and its Microsoft
58  * specific variants.
59  */
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 */
65
66 #define _S_IFMT         0xF000  /* File type mask */
67
68 #define _S_IEXEC        0x0040
69 #define _S_IWRITE       0x0080
70 #define _S_IREAD        0x0100
71
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
76
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)
82
83 #ifndef _NO_OLDNAMES
84
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
98
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)
104
105 #endif  /* !_NO_OLDNAMES */
106 #endif  /* !__WCHAR_H_SOURCED__ */
107
108 #ifndef RC_INVOKED
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).
114  *
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!
118  */
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 */                          \
131 }
132
133 /* Here, we expand the preceding macro to yield the actual definition
134  * of struct stat, under its current Microsoft "uglified" name...
135  */
136 struct _stat __struct_stat_defined( _off_t, time_t );
137
138 #ifndef _NO_OLDNAMES
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.
142  */
143 struct stat __struct_stat_defined( _off_t, time_t );
144 #endif  /* !_NO_OLDNAMES */
145
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...
150  */
151 struct _stati64 __struct_stat_defined( __off64_t, time_t );
152
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
156  * Win2K onwards...
157  */
158 struct __stat64 __struct_stat_defined( __off64_t, __time64_t );
159
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).
164  */
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 );
168
169 #endif  /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
170 #endif  /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
171 #endif  /* __MSVCRT__ */
172
173 /* From here on, it is sufficient to leave __struct_stat_defined as
174  * a macro which expands to nothing.
175  */
176 #undef  __struct_stat_defined
177 #define __struct_stat_defined
178
179 #endif /* !__struct_stat_defined */
180
181 _BEGIN_C_DECLS
182
183 #ifdef _SYS_STAT_H
184 /* This set of function prototypes are to be declared only when
185  * <sys/stat.h> is included directly.
186  */
187 _CRTIMP __cdecl __MINGW_NOTHROW  int _umask (int);
188 _CRTIMP __cdecl __MINGW_NOTHROW  int _chmod (const char *, int);
189
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).
194  */
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 */
198
199 #ifndef _NO_OLDNAMES
200 /* These are the standard POSIX names, (and the original Microsoft names),
201  * for the preceding four functions.
202  */
203 _CRTIMP __cdecl __MINGW_NOTHROW  int umask (int);
204 _CRTIMP __cdecl __MINGW_NOTHROW  int chmod (const char *, int);
205
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.
212  */
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 */
217
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
222  */
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 */
226
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...
230  */
231 _CRTIMP __cdecl __MINGW_NOTHROW  int _fstat64 (int, struct __stat64 *);
232 _CRTIMP __cdecl __MINGW_NOTHROW  int _stat64 (const char *, struct __stat64 *);
233
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
237  */
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 *);
244
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...
250  */
251 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _fstat (int __v1, struct _stat *__v2)
252   { return _fstat32 (__v1, (struct __stat32 *)(__v2)); }
253
254 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _stat (const char *__v1, struct _stat *__v2)
255   { return _stat32  (__v1, (struct __stat32 *)(__v2)); }
256
257 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _fstati64 (int __v1, struct _stati64 *__v2)
258   { return _fstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
259
260 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _stati64 (const char *__v1, struct _stati64 *__v2)
261   { return _stat32i64  (__v1, (struct _stat32i64 *)(__v2)); }
262
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
267  * with MSVCRT.DLL
268  */
269 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _fstat (int __v1, struct _stat *__v2)
270   { return _fstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
271
272 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _stat (const char *__v1, struct _stat *__v2)
273   { return _stat64i32  (__v1, (struct _stat64i32 *)(__v2)); }
274
275 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _fstati64 (int __v1, struct _stati64 *__v2)
276   { return _fstat64 (__v1, (struct __stat64 *)(__v2)); }
277
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 */
281
282 #ifndef _NO_OLDNAMES
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.
287  */
288 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int fstat (int __v1, struct _stat *__v2)
289   { return _fstat (__v1, __v2); }
290
291 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int stat (int __v1, struct _stat *__v2)
292   { return _stat (__v1, __v2); }
293
294 #endif  /* !_NO_OLDNAMES */
295 #endif  /* __MSVCRT_VERSION__ >= __MSVCR80_DLL */
296 #endif  /* __MSVCRT_VERSION__ >= __MSVCR61_DLL */
297 #endif  /* __MSVCRT__ */
298 #endif  /* _SYS_STAT_H */
299
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).
308  */
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.
314  */
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 */
318
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...
322  */
323 _CRTIMP __cdecl __MINGW_NOTHROW  int _wstat64 (const wchar_t *, struct __stat64 *);
324
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.
328  */
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 *);
332
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...
338  */
339 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _wstat (const wchar_t *__v1, struct _stat *__v2)
340   { return _wstat32 (__v1, (struct __stat32 *)(__v2)); }
341
342 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
343   { return _wstat32i64 (__v1, (struct _stat32i64 *)(__v2)); }
344
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
348  * compatibility.
349  */
350 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _wstat (const wchar_t *__v1, struct _stat *__v2)
351   { return _wstat64i32 (__v1, (struct _stat64i32 *)(__v2)); }
352
353 __CRT_ALIAS __cdecl __MINGW_NOTHROW  int _wstati64 (const wchar_t *__v1, struct _stati64 *__v2)
354   { return _wstat64 (__v1, (struct __stat64 *)(__v2)); }
355
356 #endif  /* !_USE_32BIT_TIME_T */
357 #endif  /* __MSVCRT_VERSION__ >= 0x0800 */
358 #endif  /* __MSVCRT_VERSION__ >= 0x0601 */
359 #endif  /* __MSVCRT__ && !(_SYS_STAT_H && _WCHAR_H) */
360
361 _END_C_DECLS
362
363 #endif  /* ! RC_INVOKED */
364 #endif  /* !_SYS_STAT__H: $RCSfile$: end of file */