1 /**
2  * @file stat.h
3  * Copyright 2012, 2013 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _STAT_H_
25 #define _STAT_H_
26 #ifndef __CRT_TESTING__
27 #pragma GCC system_header
28 #endif
29 #include <_mingw.h>
30 
31 #define __need_size_t
32 #define __need_wchar_t
33 #ifndef RC_INVOKED
34 #include <stddef.h>
35 #endif /* Not RC_INVOKED */
36 
37 #include <sys/types.h>
38 #include <string.h> /* Need memset declaration */
39 
40 /*
41  * Constants for the stat st_mode member.
42  */
43 #define	_S_IFIFO	0x1000	/* FIFO */
44 #define	_S_IFCHR	0x2000	/* Character */
45 #define	_S_IFBLK	0x3000	/* Block: Is this ever set under w32? */
46 #define	_S_IFDIR	0x4000	/* Directory */
47 #define	_S_IFREG	0x8000	/* Regular */
48 
49 #define	_S_IFMT		0xF000	/* File type mask */
50 
51 #define	_S_IEXEC	0x0040
52 #define	_S_IWRITE	0x0080
53 #define	_S_IREAD	0x0100
54 
55 #define	_S_IRWXU	(_S_IREAD | _S_IWRITE | _S_IEXEC)
56 #define	_S_IXUSR	_S_IEXEC
57 #define	_S_IWUSR	_S_IWRITE
58 #define	_S_IRUSR	_S_IREAD
59 
60 #define	_S_ISDIR(m)	(((m) & _S_IFMT) == _S_IFDIR)
61 #define	_S_ISFIFO(m)	(((m) & _S_IFMT) == _S_IFIFO)
62 #define	_S_ISCHR(m)	(((m) & _S_IFMT) == _S_IFCHR)
63 #define	_S_ISBLK(m)	(((m) & _S_IFMT) == _S_IFBLK)
64 #define	_S_ISREG(m)	(((m) & _S_IFMT) == _S_IFREG)
65 
66 #ifndef _NO_OLDNAMES
67 
68 #define	S_IFIFO		_S_IFIFO
69 #define	S_IFCHR		_S_IFCHR
70 #define	S_IFBLK		_S_IFBLK
71 #define	S_IFDIR		_S_IFDIR
72 #define	S_IFREG		_S_IFREG
73 #define	S_IFMT		_S_IFMT
74 #define	S_IEXEC		_S_IEXEC
75 #define	S_IWRITE	_S_IWRITE
76 #define	S_IREAD		_S_IREAD
77 #define	S_IRWXU		_S_IRWXU
78 #define	S_IXUSR		_S_IXUSR
79 #define	S_IWUSR		_S_IWUSR
80 #define	S_IRUSR		_S_IRUSR
81 
82 #define	S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
83 #define	S_ISFIFO(m)	(((m) & S_IFMT) == S_IFIFO)
84 #define	S_ISCHR(m)	(((m) & S_IFMT) == S_IFCHR)
85 #define	S_ISBLK(m)	(((m) & S_IFMT) == S_IFBLK)
86 #define	S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
87 
88 #endif	/* Not _NO_OLDNAMES */
89 
90 #ifndef RC_INVOKED
91 
92 #ifdef	__cplusplus
93 extern "C" {
94 #endif
95 
96 #ifndef _STAT_DEFINED
97 /*
98  * The structure manipulated and returned by stat and fstat.
99  *
100  * NOTE: If called on a directory the values in the time fields are not only
101  * invalid, they will cause localtime et. al. to return NULL. And calling
102  * asctime with a NULL pointer causes an Invalid Page Fault. So watch it!
103  */
104 struct _stat32
105 {
106 	_dev_t	   st_dev;	/* Equivalent to drive number 0=A 1=B ... */
107 	_ino_t	   st_ino;	/* Always zero ? */
108 	_mode_t	   st_mode;	/* See above constants */
109 	short	   st_nlink;	/* Number of links. */
110 	short	   st_uid;	/* User: Maybe significant on NT ? */
111 	short	   st_gid;	/* Group: Ditto */
112 	_dev_t	   st_rdev;	/* Seems useless (not even filled in) */
113 	_off_t	   st_size;	/* File size in bytes */
114 	__time32_t st_atime;	/* Accessed date (always 00:00 hrs local
115 				 * on FAT) */
116 	__time32_t st_mtime;	/* Modified time */
117 	__time32_t st_ctime;	/* Creation time */
118 };
119 
120 #ifndef __STRICT_ANSI__
121 struct stat {
122 	_dev_t	   st_dev;	/* Equivalent to drive number 0=A 1=B ... */
123 	_ino_t	   st_ino;	/* Always zero ? */
124 	_mode_t    st_mode;	/* See above constants */
125 	short	   st_nlink;	/* Number of links. */
126 	short	   st_uid;	/* User: Maybe significant on NT ? */
127 	short	   st_gid;	/* Group: Ditto */
128 	_dev_t	   st_rdev;	/* Seems useless (not even filled in) */
129 	_off_t	   st_size;	/* File size in bytes */
130 	time_t	   st_atime;	/* Accessed date (always 00:00 hrs local
131 				 * on FAT) */
132 	time_t	   st_mtime;	/* Modified time */
133 	time_t	   st_ctime;	/* Creation time */
134 };
135 #endif /* __STRICT_ANSI__ */
136 
137 struct _stat64 {
138 	dev_t	   st_dev;	/* Equivalent to drive number 0=A 1=B ... */
139 	ino_t	   st_ino;	/* Always zero ? */
140 	mode_t	   st_mode;	/* See above constants */
141 	short	   st_nlink;	/* Number of links. */
142 	short	   st_uid;	/* User: Maybe significant on NT ? */
143 	short	   st_gid;	/* Group: Ditto */
144 	dev_t	   st_rdev;	/* Seems useless (not even filled in) */
145 	_off64_t   st_size;	/* File size in bytes */
146 	__time64_t st_atime;	/* Accessed date (always 00:00 hrs local
147 				 * on FAT) */
148 	__time64_t st_mtime;	/* Modified time */
149 	__time64_t st_ctime;	/* Creation time */
150 };
151 
152 struct _stat32i64 {
153 	_dev_t		st_dev;
154 	_ino_t		st_ino;
155 	_mode_t		st_mode;
156 	short		st_nlink;
157 	short		st_uid;
158 	short		st_gid;
159 	_dev_t		st_rdev;
160 	_off64_t	st_size;
161 	__time32_t	st_atime;
162 	__time32_t	st_mtime;
163 	__time32_t	st_ctime;
164 };
165 
166 struct _stat64i32 {
167 	_dev_t		st_dev;
168 	_ino_t		st_ino;
169 	_mode_t		st_mode;
170 	short		st_nlink;
171 	short		st_uid;
172 	short		st_gid;
173 	_dev_t		st_rdev;
174 	_off_t		st_size;
175 	__time64_t	st_atime;
176 	__time64_t	st_mtime;
177 	__time64_t	st_ctime;
178 };
179 
180 /* _stat32 does not exist in MSVCRT.DLL */
181 _CRTIMP int __cdecl __MINGW_NOTHROW _stat (const char*, struct _stat32*);
_stat32(const char * _v1,struct _stat32 * _v2)182 _CRTALIAS int __cdecl __MINGW_NOTHROW _stat32 (const char* _v1, struct _stat32* _v2) {
183   return _stat(_v1, _v2);
184 }
185 
186 _CRTIMP int __cdecl __MINGW_NOTHROW _stat64 (const char*, struct _stat64*);
187 int __cdecl __MINGW_NOTHROW _stat32i64 (const char*, struct _stat32i64*);
188 int __cdecl __MINGW_NOTHROW _stat64i32 (const char*, struct _stat64i32*);
189 
190 /* _fstat32 does not exist in MSVCRT.DLL */
191 _CRTIMP int __cdecl __MINGW_NOTHROW _fstat (int, struct _stat32*);
_fstat32(int _v1,struct _stat32 * _v2)192 _CRTALIAS int __cdecl __MINGW_NOTHROW _fstat32 (int _v1, struct _stat32* _v2) {
193     return _fstat(_v1, _v2);
194 }
195 
196 _CRTIMP int __cdecl __MINGW_NOTHROW _fstat64 (int, struct _stat64*);
197 int __cdecl __MINGW_NOTHROW _fstat32i64 (int, struct _stat32i64*);
198 int __cdecl __MINGW_NOTHROW _fstat64i32 (int, struct _stat64i32*);
199 
_fstat64i32(int desc,struct _stat64i32 * _stat)200 __CRT_MAYBE_INLINE int __cdecl _fstat64i32(int desc, struct _stat64i32 *_stat) {
201   struct _stat64 st;
202   int ret = _fstat64(desc, &st);
203   if (ret == -1) {
204     memset(_stat, 0, sizeof(struct _stat64i32));
205     return -1;
206   }
207   _stat->st_dev = st.st_dev;
208   _stat->st_ino = st.st_ino;
209   _stat->st_mode = st.st_mode;
210   _stat->st_nlink = st.st_nlink;
211   _stat->st_uid = st.st_uid;
212   _stat->st_gid = st.st_gid;
213   _stat->st_rdev = st.st_rdev;
214   _stat->st_size = (_off_t) st.st_size;
215   _stat->st_atime = st.st_atime;
216   _stat->st_mtime = st.st_mtime;
217   _stat->st_ctime = st.st_ctime;
218   return ret;
219 }
_fstat32i64(int desc,struct _stat32i64 * _stat)220 __CRT_MAYBE_INLINE int __cdecl _fstat32i64(int desc, struct _stat32i64 *_stat) {
221   struct _stat32 st;
222   int ret = _fstat32(desc, &st);
223   if (ret == -1) {
224     memset(_stat, 0, sizeof(struct _stat32i64));
225     return -1;
226   }
227   _stat->st_dev = st.st_dev;
228   _stat->st_ino = st.st_ino;
229   _stat->st_mode = st.st_mode;
230   _stat->st_nlink = st.st_nlink;
231   _stat->st_uid = st.st_uid;
232   _stat->st_gid = st.st_gid;
233   _stat->st_rdev = st.st_rdev;
234   _stat->st_size = (_off_t) st.st_size;
235   _stat->st_atime = st.st_atime;
236   _stat->st_mtime = st.st_mtime;
237   _stat->st_ctime = st.st_ctime;
238   return ret;
239 }
_stat64i32(const char * fname,struct _stat64i32 * _stat)240 __CRT_MAYBE_INLINE int __cdecl _stat64i32(const char *fname, struct _stat64i32 *_stat) {
241   struct _stat64 st;
242   int ret = _stat64(fname, &st);
243   if (ret == -1) {
244     memset(_stat, 0, sizeof(struct _stat64i32));
245     return -1;
246   }
247   _stat->st_dev = st.st_dev;
248   _stat->st_ino = st.st_ino;
249   _stat->st_mode = st.st_mode;
250   _stat->st_nlink = st.st_nlink;
251   _stat->st_uid = st.st_uid;
252   _stat->st_gid = st.st_gid;
253   _stat->st_rdev = st.st_rdev;
254   _stat->st_size = (_off_t) st.st_size;
255   _stat->st_atime = st.st_atime;
256   _stat->st_mtime = st.st_mtime;
257   _stat->st_ctime = st.st_ctime;
258   return ret;
259 }
_stat32i64(const char * fname,struct _stat32i64 * _stat)260 __CRT_MAYBE_INLINE int __cdecl _stat32i64(const char *fname, struct _stat32i64 *_stat) {
261   struct _stat32 st;
262   int ret = _stat32(fname, &st);
263   if (ret == -1) {
264     memset(_stat, 0, sizeof(struct _stat32i64));
265     return -1;
266   }
267   _stat->st_dev = st.st_dev;
268   _stat->st_ino = st.st_ino;
269   _stat->st_mode = st.st_mode;
270   _stat->st_nlink = st.st_nlink;
271   _stat->st_uid = st.st_uid;
272   _stat->st_gid = st.st_gid;
273   _stat->st_rdev = st.st_rdev;
274   _stat->st_size = (_off_t) st.st_size;
275   _stat->st_atime = st.st_atime;
276   _stat->st_mtime = st.st_mtime;
277   _stat->st_ctime = st.st_ctime;
278   return ret;
279 }
280 
281 #define __stat64 _stat64
282 #if defined(_USE_32BIT_TIME_T)
283 #define _fstat      _fstat32
284 #define _fstati64   _fstat32i64
285 #define _stat       _stat32
286 #define _stati64    _stat32i64
287 
288 #else  /* !_USE_32BIT_TIME_T */
289 #define _fstat      _fstat64i32
290 #define _fstati64   _fstat64
291 #define _stat       _stat64i32
292 #define _stati64    _stat64
293 
294 #endif /* _USE_32BIT_TIME_T */
295 #define _STAT_DEFINED
296 
297 #endif /* _STAT_DEFINED */
298 
299 #if !defined(_NO_OLDNAMES) && !defined(__STRICT_ANSI__)
300 #define stat _stat
301 #define fstat _fstat
302 #endif /* !defined(_NO_OLDNAMES) && !defined(__STRICT_ANSI__) */
303 
304 #if !defined ( _WSTAT_DEFINED) /* also declared in wchar.h */
305 /* _wstat32 does not exist in MSVCRT.DLL */
306 _CRTIMP int __cdecl __MINGW_NOTHROW _wstat (const wchar_t*, struct _stat32*);
_wstat32(const wchar_t * _v1,struct _stat32 * _v2)307 _CRTALIAS int __cdecl __MINGW_NOTHROW _wstat32 (const wchar_t* _v1, struct _stat32* _v2) {
308     return _wstat(_v1, _v2);
309 }
310 
311 _CRTIMP int __cdecl __MINGW_NOTHROW _wstat64 (const wchar_t*, struct _stat64*);
312 _CRTIMP int __cdecl __MINGW_NOTHROW _wstat32i64 (const wchar_t*, struct _stat32i64*);
313 int __cdecl __MINGW_NOTHROW _wstat64i32 (const wchar_t*, struct _stat64i32*);
_wstat64i32(const wchar_t * fname,struct _stat64i32 * _stat)314 __CRT_MAYBE_INLINE int __cdecl _wstat64i32(const wchar_t *fname, struct _stat64i32 *_stat) {
315   struct _stat64 st;
316   int ret = _wstat64(fname, &st);
317   if (ret == -1) {
318     memset(_stat, 0, sizeof(struct _stat64i32));
319     return -1;
320   }
321   _stat->st_dev = st.st_dev;
322   _stat->st_ino = st.st_ino;
323   _stat->st_mode = st.st_mode;
324   _stat->st_nlink = st.st_nlink;
325   _stat->st_uid = st.st_uid;
326   _stat->st_gid = st.st_gid;
327   _stat->st_rdev = st.st_rdev;
328   _stat->st_size = (_off_t) st.st_size;
329   _stat->st_atime = st.st_atime;
330   _stat->st_mtime = st.st_mtime;
331   _stat->st_ctime = st.st_ctime;
332   return ret;
333 }
_wstat32i64(const wchar_t * fname,struct _stat32i64 * _stat)334 __CRT_MAYBE_INLINE int __cdecl _wstat32i64(const wchar_t *fname, struct _stat32i64 *_stat) {
335   struct _stat32 st;
336   int ret = _wstat32(fname, &st);
337   if (ret == -1) {
338     memset(_stat, 0, sizeof(struct _stat32i64));
339     return -1;
340   }
341   _stat->st_dev = st.st_dev;
342   _stat->st_ino = st.st_ino;
343   _stat->st_mode = st.st_mode;
344   _stat->st_nlink = st.st_nlink;
345   _stat->st_uid = st.st_uid;
346   _stat->st_gid = st.st_gid;
347   _stat->st_rdev = st.st_rdev;
348   _stat->st_size = (_off_t) st.st_size;
349   _stat->st_atime = st.st_atime;
350   _stat->st_mtime = st.st_mtime;
351   _stat->st_ctime = st.st_ctime;
352   return ret;
353 }
354 
355 #if defined(_USE_32BIT_TIME_T)
356 #define _wstat      _wstat32
357 #define _wstati64   _wstat32i64
358 
359 #else /* ! _USE_32BIT_TIME_T */
360 #define _wstat      _wstat64i32
361 #define _wstati64   _wstat64
362 
363 #endif /* _USE_32BIT_TIME_T */
364 
365 #define _WSTAT_DEFINED
366 #endif /* ! _WSTAT_DEFIND */
367 
368 
369 #ifdef	__cplusplus
370 }
371 #endif
372 
373 #endif	/* Not RC_INVOKED */
374 
375 #endif	/* Not _STAT_H_ */
376