sys_stat: guarantee struct timespec
[gnulib.git] / lib / sys_stat.in.h
1 /* Provide a more complete sys/stat header file.
2    Copyright (C) 2005-2009 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Eric Blake, Paul Eggert, and Jim Meyering.  */
19
20 /* This file is supposed to be used on platforms where <sys/stat.h> is
21    incomplete.  It is intended to provide definitions and prototypes
22    needed by an application.  Start with what the system provides.  */
23
24 #if __GNUC__ >= 3
25 @PRAGMA_SYSTEM_HEADER@
26 #endif
27
28 #if defined __need_system_sys_stat_h
29 /* Special invocation convention.  */
30
31 #@INCLUDE_NEXT@ @NEXT_SYS_STAT_H@
32
33 #else
34 /* Normal invocation convention.  */
35
36 #ifndef _GL_SYS_STAT_H
37
38 /* Get nlink_t.  */
39 #include <sys/types.h>
40
41 /* Get struct timespec.  */
42 #include <time.h>
43
44 /* The include_next requires a split double-inclusion guard.  */
45 #@INCLUDE_NEXT@ @NEXT_SYS_STAT_H@
46
47 #ifndef _GL_SYS_STAT_H
48 #define _GL_SYS_STAT_H
49
50 /* The definition of GL_LINK_WARNING is copied here.  */
51
52 /* Before doing "#define mkdir rpl_mkdir" below, we need to include all
53    headers that may declare mkdir().  */
54 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
55 # include <io.h>
56 #endif
57
58 #ifndef S_IFMT
59 # define S_IFMT 0170000
60 #endif
61
62 #if STAT_MACROS_BROKEN
63 # undef S_ISBLK
64 # undef S_ISCHR
65 # undef S_ISDIR
66 # undef S_ISFIFO
67 # undef S_ISLNK
68 # undef S_ISNAM
69 # undef S_ISMPB
70 # undef S_ISMPC
71 # undef S_ISNWK
72 # undef S_ISREG
73 # undef S_ISSOCK
74 #endif
75
76 #ifndef S_ISBLK
77 # ifdef S_IFBLK
78 #  define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
79 # else
80 #  define S_ISBLK(m) 0
81 # endif
82 #endif
83
84 #ifndef S_ISCHR
85 # ifdef S_IFCHR
86 #  define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
87 # else
88 #  define S_ISCHR(m) 0
89 # endif
90 #endif
91
92 #ifndef S_ISDIR
93 # ifdef S_IFDIR
94 #  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
95 # else
96 #  define S_ISDIR(m) 0
97 # endif
98 #endif
99
100 #ifndef S_ISDOOR /* Solaris 2.5 and up */
101 # define S_ISDOOR(m) 0
102 #endif
103
104 #ifndef S_ISFIFO
105 # ifdef S_IFIFO
106 #  define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
107 # else
108 #  define S_ISFIFO(m) 0
109 # endif
110 #endif
111
112 #ifndef S_ISLNK
113 # ifdef S_IFLNK
114 #  define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
115 # else
116 #  define S_ISLNK(m) 0
117 # endif
118 #endif
119
120 #ifndef S_ISMPB /* V7 */
121 # ifdef S_IFMPB
122 #  define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
123 #  define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
124 # else
125 #  define S_ISMPB(m) 0
126 #  define S_ISMPC(m) 0
127 # endif
128 #endif
129
130 #ifndef S_ISNAM /* Xenix */
131 # ifdef S_IFNAM
132 #  define S_ISNAM(m) (((m) & S_IFMT) == S_IFNAM)
133 # else
134 #  define S_ISNAM(m) 0
135 # endif
136 #endif
137
138 #ifndef S_ISNWK /* HP/UX */
139 # ifdef S_IFNWK
140 #  define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
141 # else
142 #  define S_ISNWK(m) 0
143 # endif
144 #endif
145
146 #ifndef S_ISPORT /* Solaris 10 and up */
147 # define S_ISPORT(m) 0
148 #endif
149
150 #ifndef S_ISREG
151 # ifdef S_IFREG
152 #  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
153 # else
154 #  define S_ISREG(m) 0
155 # endif
156 #endif
157
158 #ifndef S_ISSOCK
159 # ifdef S_IFSOCK
160 #  define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
161 # else
162 #  define S_ISSOCK(m) 0
163 # endif
164 #endif
165
166
167 #ifndef S_TYPEISMQ
168 # define S_TYPEISMQ(p) 0
169 #endif
170
171 #ifndef S_TYPEISTMO
172 # define S_TYPEISTMO(p) 0
173 #endif
174
175
176 #ifndef S_TYPEISSEM
177 # ifdef S_INSEM
178 #  define S_TYPEISSEM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSEM)
179 # else
180 #  define S_TYPEISSEM(p) 0
181 # endif
182 #endif
183
184 #ifndef S_TYPEISSHM
185 # ifdef S_INSHD
186 #  define S_TYPEISSHM(p) (S_ISNAM ((p)->st_mode) && (p)->st_rdev == S_INSHD)
187 # else
188 #  define S_TYPEISSHM(p) 0
189 # endif
190 #endif
191
192 /* high performance ("contiguous data") */
193 #ifndef S_ISCTG
194 # define S_ISCTG(p) 0
195 #endif
196
197 /* Cray DMF (data migration facility): off line, with data  */
198 #ifndef S_ISOFD
199 # define S_ISOFD(p) 0
200 #endif
201
202 /* Cray DMF (data migration facility): off line, with no data  */
203 #ifndef S_ISOFL
204 # define S_ISOFL(p) 0
205 #endif
206
207 /* 4.4BSD whiteout */
208 #ifndef S_ISWHT
209 # define S_ISWHT(m) 0
210 #endif
211
212 /* If any of the following are undefined,
213    define them to their de facto standard values.  */
214 #if !S_ISUID
215 # define S_ISUID 04000
216 #endif
217 #if !S_ISGID
218 # define S_ISGID 02000
219 #endif
220
221 /* S_ISVTX is a common extension to POSIX.  */
222 #ifndef S_ISVTX
223 # define S_ISVTX 01000
224 #endif
225
226 #if !S_IRUSR && S_IREAD
227 # define S_IRUSR S_IREAD
228 #endif
229 #if !S_IRUSR
230 # define S_IRUSR 00400
231 #endif
232 #if !S_IRGRP
233 # define S_IRGRP (S_IRUSR >> 3)
234 #endif
235 #if !S_IROTH
236 # define S_IROTH (S_IRUSR >> 6)
237 #endif
238
239 #if !S_IWUSR && S_IWRITE
240 # define S_IWUSR S_IWRITE
241 #endif
242 #if !S_IWUSR
243 # define S_IWUSR 00200
244 #endif
245 #if !S_IWGRP
246 # define S_IWGRP (S_IWUSR >> 3)
247 #endif
248 #if !S_IWOTH
249 # define S_IWOTH (S_IWUSR >> 6)
250 #endif
251
252 #if !S_IXUSR && S_IEXEC
253 # define S_IXUSR S_IEXEC
254 #endif
255 #if !S_IXUSR
256 # define S_IXUSR 00100
257 #endif
258 #if !S_IXGRP
259 # define S_IXGRP (S_IXUSR >> 3)
260 #endif
261 #if !S_IXOTH
262 # define S_IXOTH (S_IXUSR >> 6)
263 #endif
264
265 #if !S_IRWXU
266 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
267 #endif
268 #if !S_IRWXG
269 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
270 #endif
271 #if !S_IRWXO
272 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
273 #endif
274
275 /* S_IXUGO is a common extension to POSIX.  */
276 #if !S_IXUGO
277 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
278 #endif
279
280 #ifndef S_IRWXUGO
281 # define S_IRWXUGO (S_IRWXU | S_IRWXG | S_IRWXO)
282 #endif
283
284 /* Macros for futimens and utimensat.  */
285 #ifndef UTIME_NOW
286 # define UTIME_NOW (-1)
287 # define UTIME_OMIT (-2)
288 #endif
289
290
291 #ifdef __cplusplus
292 extern "C" {
293 #endif
294
295
296 #if @GNULIB_LSTAT@
297 # if ! @HAVE_LSTAT@
298 /* mingw does not support symlinks, therefore it does not have lstat.  But
299    without links, stat does just fine.  */
300 #  define lstat stat
301 # elif @REPLACE_LSTAT@
302 #  undef lstat
303 #  define lstat rpl_lstat
304 extern int rpl_lstat (const char *name, struct stat *buf);
305 # endif
306 #elif defined GNULIB_POSIXCHECK
307 # undef lstat
308 # define lstat(p,b)                                                     \
309   (GL_LINK_WARNING ("lstat is unportable - "                            \
310                     "use gnulib module lstat for portability"),         \
311    lstat (p, b))
312 #endif
313
314 #if @GNULIB_STAT@
315 # if @REPLACE_STAT@
316 /* We can't use the object-like #define stat rpl_stat, because of
317    struct stat.  This means that rpl_stat will not be used if the user
318    does (stat)(a,b).  Oh well.  */
319 #  undef stat
320 #  define stat(name, st) rpl_stat (name, st)
321 extern int stat (const char *name, struct stat *buf);
322 # endif
323 #elif defined GNULIB_POSIXCHECK
324 # undef stat
325 # define stat(p,b)                                                      \
326   (GL_LINK_WARNING ("stat is unportable - "                             \
327                     "use gnulib module stat for portability"),          \
328    stat (p, b))
329 #endif
330
331 #if @GNULIB_FCHMODAT@
332 # if !@HAVE_FCHMODAT@
333 extern int fchmodat (int fd, char const *file, mode_t mode, int flag);
334 # endif
335 #elif defined GNULIB_POSIXCHECK
336 # undef fchmodat
337 # define fchmodat(d,n,m,f)                         \
338     (GL_LINK_WARNING ("fchmodat is not portable - " \
339                       "use gnulib module openat for portability"), \
340      fchmodat (d, n, m, f))
341 #endif
342
343
344 #if @GNULIB_FSTATAT@
345 # if @REPLACE_FSTATAT@
346 #  undef fstatat
347 #  define fstatat rpl_fstatat
348 # endif
349 # if !@HAVE_FSTATAT@ || @REPLACE_FSTATAT@
350 extern int fstatat (int fd, char const *name, struct stat *st, int flags);
351 # endif
352 #elif defined GNULIB_POSIXCHECK
353 # undef fstatat
354 # define fstatat(d,n,s,f)                         \
355     (GL_LINK_WARNING ("fstatat is not portable - " \
356                       "use gnulib module openat for portability"), \
357      fstatat (d, n, s, f))
358 #endif
359
360
361 #if @GNULIB_FUTIMENS@
362 # if @REPLACE_FUTIMENS@
363 #  undef futimens
364 #  define futimens rpl_futimens
365 # endif
366 # if !@HAVE_FUTIMENS@ || @REPLACE_FUTIMENS@
367 extern int futimens (int fd, struct timespec const times[2]);
368 # endif
369 #elif defined GNULIB_POSIXCHECK
370 # undef futimens
371 # define futimens(f,t)                         \
372     (GL_LINK_WARNING ("futimens is not portable - " \
373                       "use gnulib module futimens for portability"), \
374      futimens (f, t))
375 #endif
376
377
378 #if @GNULIB_MKDIRAT@
379 # if !@HAVE_MKDIRAT@
380 extern int mkdirat (int fd, char const *file, mode_t mode);
381 # endif
382 #elif defined GNULIB_POSIXCHECK
383 # undef mkdirat
384 # define mkdirat(d,n,m)                         \
385     (GL_LINK_WARNING ("mkdirat is not portable - " \
386                       "use gnulib module openat for portability"), \
387      mkdirat (d, n, m))
388 #endif
389
390 #if @GNULIB_MKFIFOAT@
391 # if !@HAVE_MKFIFOAT@
392 int mkfifoat (int fd, char const *file, mode_t mode);
393 # endif
394 #elif defined GNULIB_POSIXCHECK
395 # undef mkfifoat
396 # define mkfifoat(d,n,m)                                     \
397     (GL_LINK_WARNING ("mkfifoat is not portable - " \
398                       "use gnulib module mkfifoat for portability"), \
399      mkfifoat (d, n, m))
400 #endif
401
402 #if @GNULIB_MKNODAT@
403 # if !@HAVE_MKNODAT@
404 int mknodat (int fd, char const *file, mode_t mode, dev_t dev);
405 # endif
406 #elif defined GNULIB_POSIXCHECK
407 # undef mknodat
408 # define mknodat(f,n,m,d)                            \
409     (GL_LINK_WARNING ("mknodat is not portable - " \
410                       "use gnulib module mkfifoat for portability"), \
411      mknodat (f, n, m, d))
412 #endif
413
414 #if @REPLACE_FSTAT@
415 # define fstat rpl_fstat
416 extern int fstat (int fd, struct stat *buf);
417 #endif
418
419 #if @REPLACE_MKDIR@
420 # undef mkdir
421 # define mkdir rpl_mkdir
422 extern int mkdir (char const *name, mode_t mode);
423 #else
424 /* mingw's _mkdir() function has 1 argument, but we pass 2 arguments.
425    Additionally, it declares _mkdir (and depending on compile flags, an
426    alias mkdir), only in the nonstandard <io.h>, which is included above.  */
427 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
428
429 static inline int
430 rpl_mkdir (char const *name, mode_t mode)
431 {
432   return _mkdir (name);
433 }
434
435 #  define mkdir rpl_mkdir
436 # endif
437 #endif
438
439
440 /* Declare BSD extensions.  */
441
442 #if @GNULIB_LCHMOD@
443 /* Change the mode of FILENAME to MODE, without dereferencing it if FILENAME
444    denotes a symbolic link.  */
445 # if !@HAVE_LCHMOD@
446 /* The lchmod replacement follows symbolic links.  Callers should take
447    this into account; lchmod should be applied only to arguments that
448    are known to not be symbolic links.  On hosts that lack lchmod,
449    this can lead to race conditions between the check and the
450    invocation of lchmod, but we know of no workarounds that are
451    reliable in general.  You might try requesting support for lchmod
452    from your operating system supplier.  */
453 #  define lchmod chmod
454 # endif
455 # if 0 /* assume already declared */
456 extern int lchmod (const char *filename, mode_t mode);
457 # endif
458 #elif defined GNULIB_POSIXCHECK
459 # undef lchmod
460 # define lchmod(f,m) \
461     (GL_LINK_WARNING ("lchmod is unportable - " \
462                       "use gnulib module lchmod for portability"), \
463      lchmod (f, m))
464 #endif
465
466
467 #ifdef __cplusplus
468 }
469 #endif
470
471
472 #endif /* _GL_SYS_STAT_H */
473 #endif /* _GL_SYS_STAT_H */
474 #endif