1 /* @(#)stat.h	1.20 15/08/23 Copyright 1998-2015 J. Schilling */
2 /*
3  *	Definitions for stat() file mode
4  *
5  *	Copyright (c) 1998-2015 J. Schilling
6  */
7 /*
8  * The contents of this file are subject to the terms of the
9  * Common Development and Distribution License, Version 1.0 only
10  * (the "License").  You may not use this file except in compliance
11  * with the License.
12  *
13  * See the file CDDL.Schily.txt in this distribution for details.
14  * A copy of the CDDL is also available via the Internet at
15  * http://www.opensource.org/licenses/cddl1.txt
16  *
17  * When distributing Covered Code, include this CDDL HEADER in each
18  * file and include the License file CDDL.Schily.txt from this distribution.
19  */
20 
21 #ifndef	_SCHILY_STAT_H
22 #define	_SCHILY_STAT_H
23 
24 #ifndef _SCHILY_MCONFIG_H
25 #include <schily/mconfig.h>
26 #endif
27 
28 #ifndef	_SCHILY_TYPES_H
29 #include <schily/types.h>	/* Needed for sys/stat.h		*/
30 #endif
31 #ifndef	_INCL_SYS_STAT_H
32 #include <sys/stat.h>		/* For S_IS* and S_IF*			*/
33 #define	_INCL_SYS_STAT_H
34 #endif
35 
36 #if	!defined(HAVE_LSTAT) || !defined(S_IFLNK)
37 #define	lstat	stat
38 #endif
39 
40 /*
41  * Apollo Domain/OS has a broken sys/stat.h that defines
42  * S_IFIFO == S_IFSOCK and creates trouble if the constants
43  * are used as case labels.
44  */
45 #if S_IFIFO == S_IFSOCK
46 #	undef	S_IFSOCK
47 #endif
48 
49 #ifdef	STAT_MACROS_BROKEN
50 #undef	S_ISFIFO			/* Named pipe		*/
51 #undef	S_ISCHR				/* Character special	*/
52 #undef	S_ISMPC				/* UNUSED multiplexed c	*/
53 #undef	S_ISDIR				/* Directory		*/
54 #undef	S_ISNAM				/* Named file (XENIX)	*/
55 #undef	S_ISBLK				/* Block special	*/
56 #undef	S_ISMPB				/* UNUSED multiplexed b	*/
57 #undef	S_ISREG				/* Regular file		*/
58 #undef	S_ISCTG				/* Contiguous file	*/
59 #undef	S_ISLNK				/* Symbolic link	*/
60 #undef	S_ISSHAD			/* Solaris shadow inode	*/
61 #undef	S_ISSOCK			/* UNIX domain socket	*/
62 #undef	S_ISDOOR			/* Solaris DOOR		*/
63 #undef	S_ISPORT			/* Solaris event port	*/
64 #undef	S_ISWHT				/* BSD whiteout		*/
65 #undef	S_ISEVC				/* UNOS eventcount	*/
66 #endif
67 
68 #ifndef	S_ISFIFO			/* 1 Named pipe		*/
69 #	ifdef	S_IFIFO
70 #		define	S_ISFIFO(m)	(((m) & S_IFMT) == S_IFIFO)
71 #	else
72 #		define	S_ISFIFO(m)	(0)
73 #	endif
74 #endif
75 #ifndef	S_ISCHR				/* 2 Character special	*/
76 #	ifdef	S_IFCHR
77 #		define	S_ISCHR(m)	(((m) & S_IFMT) == S_IFCHR)
78 #	else
79 #		define	S_ISCHR(m)	(0)
80 #	endif
81 #endif
82 #ifndef	S_ISMPC				/* 3 UNUSED multiplexed char	*/
83 #	ifdef	S_IFMPC
84 #		define	S_ISMPC(m)	(((m) & S_IFMT) == S_IFMPC)
85 #	else
86 #		define	S_ISMPC(m)	(0)
87 #	endif
88 #endif
89 #ifndef	S_ISDIR				/* 4 Directory			*/
90 #	ifdef	S_IFDIR
91 #		define	S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
92 #	else
93 #		define	S_ISDIR(m)	(0)
94 #	endif
95 #endif
96 #ifndef	S_ISNAM				/* 5 Named file (XENIX)		*/
97 #	ifdef	S_IFNAM
98 #		define	S_ISNAM(m)	(((m) & S_IFMT) == S_IFNAM)
99 #	else
100 #		define	S_ISNAM(m)	(0)
101 #	endif
102 #endif
103 #ifndef	S_ISBLK				/* 6 Block special		*/
104 #	ifdef	S_IFBLK
105 #		define	S_ISBLK(m)	(((m) & S_IFMT) == S_IFBLK)
106 #	else
107 #		define	S_ISBLK(m)	(0)
108 #	endif
109 #endif
110 #ifndef	S_ISMPB				/* 7 UNUSED multiplexed block	*/
111 #	ifdef	S_IFMPB
112 #		define	S_ISMPB(m)	(((m) & S_IFMT) == S_IFMPB)
113 #	else
114 #		define	S_ISMPB(m)	(0)
115 #	endif
116 #endif
117 #ifndef	S_ISREG				/* 8 Regular file		*/
118 #	ifdef	S_IFREG
119 #		define	S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
120 #	else
121 #		define	S_ISREG(m)	(0)
122 #	endif
123 #endif
124 #ifndef	S_ISCTG				/* 9 Contiguous file		*/
125 #	ifdef	S_IFCTG
126 #		define	S_ISCTG(m)	(((m) & S_IFMT) == S_IFCTG)
127 #	else
128 #		define	S_ISCTG(m)	(0)
129 #	endif
130 #endif
131 #ifndef	S_ISLNK				/* 10 Symbolic link		*/
132 #	ifdef	S_IFLNK
133 #		define	S_ISLNK(m)	(((m) & S_IFMT) == S_IFLNK)
134 #	else
135 #		define	S_ISLNK(m)	(0)
136 #	endif
137 #endif
138 #ifndef	S_ISSHAD			/* 11 Solaris shadow inode	*/
139 #	ifdef	S_IFSHAD
140 #		define	S_ISSHAD(m)	(((m) & S_IFMT) == S_IFSHAD)
141 #	else
142 #		define	S_ISSHAD(m)	(0)
143 #	endif
144 #endif
145 #ifndef	S_ISSOCK			/* 12 UNIX domain socket	*/
146 #	ifdef	S_IFSOCK
147 #		define	S_ISSOCK(m)	(((m) & S_IFMT) == S_IFSOCK)
148 #	else
149 #		define	S_ISSOCK(m)	(0)
150 #	endif
151 #endif
152 #ifndef	S_ISDOOR			/* 13 Solaris DOOR		*/
153 #	ifdef	S_IFDOOR
154 #		define	S_ISDOOR(m)	(((m) & S_IFMT) == S_IFDOOR)
155 #	else
156 #		define	S_ISDOOR(m)	(0)
157 #	endif
158 #endif
159 #ifndef	S_ISPORT			/* 14 Solaris event port	*/
160 #	ifdef	S_IFPORT
161 #		define	S_ISPORT(m)	(((m) & S_IFMT) == S_IFPORT)
162 #	else
163 #		define	S_ISPORT(m)	(0)
164 #	endif
165 #endif
166 #ifndef	S_ISWHT				/* 14 BSD whiteout		*/
167 #	ifdef	S_IFWHT
168 #		define	S_ISWHT(m)	(((m) & S_IFMT) == S_ISWHT)
169 #	else
170 #		define	S_ISWHT(m)	(0)
171 #	endif
172 #endif
173 #ifndef	S_IFEVC				/* 15 UNOS eventcount		*/
174 #	ifdef	S_IFEVC
175 #		define	S_ISEVC(m)	(((m) & S_IFMT) == S_IFEVC)
176 #	else
177 #		define	S_ISEVC(m)	(0)
178 #	endif
179 #endif
180 
181 #ifndef	S_TYPEISMQ
182 /*
183  * XXX ??? where is a definition of a message queue ???
184  */
185 #		define	S_TYPEISMQ(_stbuf)	(0)
186 #endif
187 #ifndef	S_TYPEISSEM
188 #	ifdef	S_INSEM
189 #		define	S_TYPEISSEM(_stbuf)	(S_ISNAM((_stbuf)->st_mode) && \
190 						(_stbuf)->st_rdev == S_INSEM)
191 #	else
192 #		define	S_TYPEISSEM(_stbuf)	(0)
193 #	endif
194 #endif
195 #ifndef	S_TYPEISSHM
196 #	ifdef	S_INSHD
197 #		define	S_TYPEISSHM(_stbuf)	(S_ISNAM((_stbuf)->st_mode) && \
198 						(_stbuf)->st_rdev == S_INSHD)
199 #	else
200 #		define	S_TYPEISSHM(_stbuf)	(0)
201 #	endif
202 #endif
203 
204 /*
205  * Mode permission bits.
206  * UNIX V.7 has only	S_ISUID/S_ISGID/S_ISVTX and S_IREAD/S_IWRITE/S_IEXEC
207  *
208  * S_ISUID/S_ISGID/S_ISVTX is available on UNIX V.7 and POSIX
209  */
210 #ifndef	S_ISUID			/* Set-user-ID on execution */
211 #define	S_ISUID	0		/* If it is not defined, it is not supported */
212 #endif
213 #ifndef	S_ISGID			/* Set-group-ID on execution */
214 #define	S_ISGID	0		/* If it is not defined, it is not supported */
215 #endif
216 #ifndef	S_ISVTX			/* On directories, restricted deletion flag */
217 #define	S_ISVTX	0		/* If it is not defined, it is not supported */
218 #endif
219 
220 /*
221  * S_IREAD/S_IWRITE/S_IEXEC is only available on UNIX V.7 but not on POSIX
222  * Emulate these definitions to support compilation of programs like
223  * SCCS and the Bourne Shell and to make the other definitions simpler.
224  */
225 #ifndef	S_IREAD
226 #ifdef	S_IRUSR
227 #define	S_IREAD		S_IRUSR	/* Use POSIX name */
228 #else
229 #define	S_IREAD		0400	/* Very old UNIX, use own definition */
230 #endif
231 #endif
232 #ifndef	S_IWRITE
233 #ifdef	S_IWUSR
234 #define	S_IWRITE	S_IWUSR	/* Use POSIX name */
235 #else
236 #define	S_IWRITE	0200	/* Very old UNIX, use own definition */
237 #endif
238 #endif
239 #ifndef	S_IEXEC
240 #ifdef	S_IXUSR
241 #define	S_IEXEC		S_IXUSR	/* Use POSIX name */
242 #else
243 #define	S_IEXEC		0100	/* Very old UNIX, use own definition */
244 #endif
245 #endif
246 
247 #ifndef	S_IRUSR			/* Read permission, owner */
248 #define	S_IRUSR	S_IREAD		/* Needed on old UNIX systems */
249 #endif
250 #ifndef	S_IWUSR			/* Write permission, owner */
251 #define	S_IWUSR	S_IWRITE	/* Needed on old UNIX systems */
252 #endif
253 #ifndef	S_IXUSR			/* Execute/search permission, owner */
254 #define	S_IXUSR	S_IEXEC		/* Needed on old UNIX systems */
255 #endif
256 #ifndef	S_IRGRP			/* Read permission, group */
257 #define	S_IRGRP	(S_IREAD >> 3)	/* Needed on old UNIX systems */
258 #endif
259 #ifndef	S_IWGRP			/* Write permission, group */
260 #define	S_IWGRP	(S_IWRITE >> 3)	/* Needed on old UNIX systems */
261 #endif
262 #ifndef	S_IXGRP			/* Execute/search permission, group */
263 #define	S_IXGRP	(S_IEXEC >> 3)	/* Needed on old UNIX systems */
264 #endif
265 #ifndef	S_IROTH			/* Read permission, others */
266 #define	S_IROTH	(S_IREAD >> 6)	/* Needed on old UNIX systems */
267 #endif
268 #ifndef	S_IWOTH			/* Write permission, others */
269 #define	S_IWOTH	(S_IWRITE >> 6)	/* Needed on old UNIX systems */
270 #endif
271 #ifndef	S_IXOTH			/* Execute/search permission, others */
272 #define	S_IXOTH	(S_IEXEC >> 6)	/* Needed on old UNIX systems */
273 #endif
274 
275 #ifndef	S_IRWXU			/* Read, write, execute/search by owner */
276 #define	S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
277 #endif
278 #ifndef	S_IRWXG			/* Read, write, execute/search by group */
279 #define	S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
280 #endif
281 #ifndef	S_IRWXO			/* Read, write, execute/search by others */
282 #define	S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
283 #endif
284 
285 /*
286  * SCO UnixWare has st_atim.st__tim.tv_nsec but the st_atim.tv_nsec tests also
287  * succeeds. If we use st_atim.tv_nsec on UnixWare, we get a warning about
288  * illegal structure usage. For this reason, our code needs to have
289  * #ifdef HAVE_ST__TIM before #ifdef HAVE_ST_NSEC.
290  */
291 #if	defined(HAVE_ST_ATIMENSEC)
292 
293 /*
294  * Found e.g. on NetBSD and OpenBSD.
295  */
296 #define	stat_ansecs(s)		((s)->st_atimensec)
297 #define	stat_mnsecs(s)		((s)->st_mtimensec)
298 #define	stat_cnsecs(s)		((s)->st_ctimensec)
299 
300 #define	stat_set_ansecs(s, n)	((s)->st_atimensec = n)
301 #define	stat_set_mnsecs(s, n)	((s)->st_mtimensec = n)
302 #define	stat_set_cnsecs(s, n)	((s)->st_ctimensec = n)
303 
304 #define	_FOUND_STAT_NSECS_
305 #endif
306 
307 #if	defined(HAVE_ST_ATIME_N)
308 
309 /*
310  * Found e.g. on AIX.
311  */
312 #define	stat_ansecs(s)		((s)->st_atime_n)
313 #define	stat_mnsecs(s)		((s)->st_mtime_n)
314 #define	stat_cnsecs(s)		((s)->st_ctime_n)
315 
316 #define	stat_set_ansecs(s, n)	((s)->st_atime_n = n)
317 #define	stat_set_mnsecs(s, n)	((s)->st_mtime_n = n)
318 #define	stat_set_cnsecs(s, n)	((s)->st_ctime_n = n)
319 
320 #define	_FOUND_STAT_NSECS_
321 #endif
322 
323 #if	defined(HAVE_ST__TIM) && !defined(_FOUND_STAT_NSECS_)
324 
325 /*
326  * Found e.g. on UnixWare.
327  */
328 #define	stat_ansecs(s)		((s)->st_atim.st__tim.tv_nsec)
329 #define	stat_mnsecs(s)		((s)->st_mtim.st__tim.tv_nsec)
330 #define	stat_cnsecs(s)		((s)->st_ctim.st__tim.tv_nsec)
331 
332 #define	stat_set_ansecs(s, n)	((s)->st_atim.st__tim.tv_nsec = n)
333 #define	stat_set_mnsecs(s, n)	((s)->st_mtim.st__tim.tv_nsec = n)
334 #define	stat_set_cnsecs(s, n)	((s)->st_ctim.st__tim.tv_nsec = n)
335 
336 #define	_FOUND_STAT_NSECS_
337 #endif
338 
339 #if	defined(HAVE_ST_NSEC) && !defined(_FOUND_STAT_NSECS_)
340 
341 /*
342  * Found e.g. on SunOS-5.x and IRIX.
343  */
344 #define	stat_ansecs(s)		((s)->st_atim.tv_nsec)
345 #define	stat_mnsecs(s)		((s)->st_mtim.tv_nsec)
346 #define	stat_cnsecs(s)		((s)->st_ctim.tv_nsec)
347 
348 #define	stat_set_ansecs(s, n)	((s)->st_atim.tv_nsec = n)
349 #define	stat_set_mnsecs(s, n)	((s)->st_mtim.tv_nsec = n)
350 #define	stat_set_cnsecs(s, n)	((s)->st_ctim.tv_nsec = n)
351 
352 #define	_FOUND_STAT_NSECS_
353 #endif
354 
355 #if	defined(HAVE_ST_ATIMESPEC) && !defined(_FOUND_STAT_NSECS_)
356 
357 /*
358  * Found e.g. on FreeBSD and Mac OS X.
359  */
360 #define	stat_ansecs(s)		((s)->st_atimespec.tv_nsec)
361 #define	stat_mnsecs(s)		((s)->st_mtimespec.tv_nsec)
362 #define	stat_cnsecs(s)		((s)->st_ctimespec.tv_nsec)
363 
364 #define	stat_set_ansecs(s, n)	((s)->st_atimespec.tv_nsec = n)
365 #define	stat_set_mnsecs(s, n)	((s)->st_mtimespec.tv_nsec = n)
366 #define	stat_set_cnsecs(s, n)	((s)->st_ctimespec.tv_nsec = n)
367 
368 #define	_FOUND_STAT_NSECS_
369 #endif
370 
371 /*
372  * st_spare1 is last as some implementations keep backwards
373  * compatibility although there is one of the nsec implementations.
374  */
375 #if	defined(HAVE_ST_SPARE1) && !defined(_FOUND_STAT_NSECS_)
376 
377 /*
378  * Found e.g. on SunOS-4.x and HP-UX.
379  */
380 #define	stat_ansecs(s)		((s)->st_spare1 * 1000)
381 #define	stat_mnsecs(s)		((s)->st_spare2 * 1000)
382 #define	stat_cnsecs(s)		((s)->st_spare3 * 1000)
383 
384 #define	stat_set_ansecs(s, n)	((s)->st_spare1 = n / 1000)
385 #define	stat_set_mnsecs(s, n)	((s)->st_spare2 = n / 1000)
386 #define	stat_set_cnsecs(s, n)	((s)->st_spare3 = n / 1000)
387 
388 #define	_FOUND_STAT_USECS_
389 #define	_FOUND_STAT_NSECS_
390 #endif
391 
392 #if	!defined(_FOUND_STAT_NSECS_)
393 #define	stat_ansecs(s)		(0)
394 #define	stat_mnsecs(s)		(0)
395 #define	stat_cnsecs(s)		(0)
396 
397 #define	stat_set_ansecs(s, n)	(0)
398 #define	stat_set_mnsecs(s, n)	(0)
399 #define	stat_set_cnsecs(s, n)	(0)
400 #endif
401 
402 #endif	/* _SCHILY_STAT_H */
403