*** empty log message ***
[coreutils.git] / src / system.h
blobe9ad4c994f5209f1c95bacd9d829e0ef7ef148f0
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2 Copyright (C) 1989, 1991-1999 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Include sys/types.h before this file. */
20 #include <sys/stat.h>
22 #ifdef STAT_MACROS_BROKEN
23 # undef S_ISBLK
24 # undef S_ISCHR
25 # undef S_ISDIR
26 # undef S_ISDOOR
27 # undef S_ISFIFO
28 # undef S_ISLNK
29 # undef S_ISMPB
30 # undef S_ISMPC
31 # undef S_ISNWK
32 # undef S_ISREG
33 # undef S_ISSOCK
34 #endif /* STAT_MACROS_BROKEN. */
36 #ifndef S_IFMT
37 # define S_IFMT 0170000
38 #endif
39 #if !defined(S_ISBLK) && defined(S_IFBLK)
40 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
41 #endif
42 #if !defined(S_ISCHR) && defined(S_IFCHR)
43 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
44 #endif
45 #if !defined(S_ISDIR) && defined(S_IFDIR)
46 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
47 #endif
48 #if !defined(S_ISREG) && defined(S_IFREG)
49 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
50 #endif
51 #if !defined(S_ISFIFO) && defined(S_IFIFO)
52 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
53 #endif
54 #if !defined(S_ISLNK) && defined(S_IFLNK)
55 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
56 #endif
57 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
58 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
59 #endif
60 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
61 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
62 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
63 #endif
64 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
65 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
66 #endif
67 #if !defined(S_ISDOOR) && defined(S_IFDOOR) /* Solaris 2.5 and up */
68 # define S_ISDOOR(m) (((m) & S_IFMT) == S_IFDOOR)
69 #endif
71 #if !S_ISUID
72 # define S_ISUID 04000
73 #endif
74 #if !S_ISGID
75 # define S_ISGID 02000
76 #endif
78 /* S_ISVTX is a common extension to POSIX.1. */
79 #ifndef S_ISVTX
80 # define S_ISVTX 01000
81 #endif
83 #if !S_IWUSR
84 # if S_IWRITE
85 # define S_IWUSR S_IWRITE
86 # else
87 # define S_IWUSR 00200
88 # endif
89 #endif
91 #ifndef S_IWUSR
92 # ifdef S_IWRITE
93 # define S_IWUSR S_IWRITE
94 # else
95 # define S_IWUSR 00200
96 # endif
97 #endif
99 #ifndef S_IEXEC
100 # define S_IEXEC S_IXUSR
101 #endif
103 #ifndef S_IXUSR
104 # define S_IXUSR S_IEXEC
105 #endif
106 #ifndef S_IXGRP
107 # define S_IXGRP (S_IEXEC >> 3)
108 #endif
109 #ifndef S_IXOTH
110 # define S_IXOTH (S_IEXEC >> 6)
111 #endif
113 #ifndef S_IRWXU
114 # define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
115 #endif
116 #ifndef S_IRWXG
117 # define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
118 #endif
119 #ifndef S_IRWXO
120 # define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
121 #endif
123 /* S_IXUGO is a common extension to POSIX.1. */
124 #ifndef S_IXUGO
125 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
126 #endif
128 /* All the mode bits that can be affected by chmod. */
129 #define CHMOD_MODE_BITS \
130 (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
132 #ifdef ST_MTIM_NSEC
133 # define ST_TIME_CMP_NS(a, b, ns) ((a).ns < (b).ns ? -1 : (a).ns > (b).ns)
134 #else
135 # define ST_TIME_CMP_NS(a, b, ns) 0
136 #endif
137 #define ST_TIME_CMP(a, b, s, ns) \
138 ((a).s < (b).s ? -1 : (a).s > (b).s ? 1 : ST_TIME_CMP_NS(a, b, ns))
139 #define ATIME_CMP(a, b) ST_TIME_CMP (a, b, st_atime, st_atim.ST_MTIM_NSEC)
140 #define CTIME_CMP(a, b) ST_TIME_CMP (a, b, st_ctime, st_ctim.ST_MTIM_NSEC)
141 #define MTIME_CMP(a, b) ST_TIME_CMP (a, b, st_mtime, st_mtim.ST_MTIM_NSEC)
143 #if !defined(HAVE_MKFIFO)
144 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
145 #endif
147 #ifdef HAVE_SYS_PARAM_H
148 # include <sys/param.h>
149 #endif
151 /* <unistd.h> should be included before any preprocessor test
152 of _POSIX_VERSION. */
153 #ifdef HAVE_UNISTD_H
154 # include <unistd.h>
155 #endif
157 #ifndef STDIN_FILENO
158 # define STDIN_FILENO 0
159 #endif
161 #ifndef STDOUT_FILENO
162 # define STDOUT_FILENO 1
163 #endif
165 #ifndef STDERR_FILENO
166 # define STDERR_FILENO 2
167 #endif
170 #if HAVE_LIMITS_H
171 /* limits.h must come before pathmax.h because limits.h on some systems
172 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
173 # include <limits.h>
174 #endif
176 #include "pathmax.h"
178 #if TIME_WITH_SYS_TIME
179 # include <sys/time.h>
180 # include <time.h>
181 #else
182 # if HAVE_SYS_TIME_H
183 # include <sys/time.h>
184 # else
185 # include <time.h>
186 # endif
187 #endif
189 /* Since major is a function on SVR4, we can't use `ifndef major'. */
190 #ifdef MAJOR_IN_MKDEV
191 # include <sys/mkdev.h>
192 # define HAVE_MAJOR
193 #endif
194 #ifdef MAJOR_IN_SYSMACROS
195 # include <sys/sysmacros.h>
196 # define HAVE_MAJOR
197 #endif
198 #ifdef major /* Might be defined in sys/types.h. */
199 # define HAVE_MAJOR
200 #endif
202 #ifndef HAVE_MAJOR
203 # define major(dev) (((dev) >> 8) & 0xff)
204 # define minor(dev) ((dev) & 0xff)
205 # define makedev(maj, min) (((maj) << 8) | (min))
206 #endif
207 #undef HAVE_MAJOR
209 #ifdef HAVE_UTIME_H
210 # include <utime.h>
211 #endif
213 /* Some systems (even some that do have <utime.h>) don't declare this
214 structure anywhere. */
215 #ifndef HAVE_STRUCT_UTIMBUF
216 struct utimbuf
218 long actime;
219 long modtime;
221 #endif
223 /* Don't use bcopy! Use memmove if source and destination may overlap,
224 memcpy otherwise. */
226 #ifdef HAVE_STRING_H
227 # if !STDC_HEADERS && HAVE_MEMORY_H
228 # include <memory.h>
229 # endif
230 # include <string.h>
231 #else
232 # include <strings.h>
233 #endif
235 #include <errno.h>
236 #ifndef errno
237 extern int errno;
238 #endif
240 #if HAVE_STDLIB_H
241 # define getopt system_getopt
242 # include <stdlib.h>
243 # undef getopt
244 #endif
246 /* The following test is to work around the gross typo in
247 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
248 is defined to 0, not 1. */
249 #if !EXIT_FAILURE
250 # undef EXIT_FAILURE
251 # define EXIT_FAILURE 1
252 #endif
254 #ifndef EXIT_SUCCESS
255 # define EXIT_SUCCESS 0
256 #endif
258 #ifdef HAVE_FCNTL_H
259 # include <fcntl.h>
260 #else
261 # include <sys/file.h>
262 #endif
264 #if !defined (SEEK_SET)
265 # define SEEK_SET 0
266 # define SEEK_CUR 1
267 # define SEEK_END 2
268 #endif
269 #ifndef F_OK
270 # define F_OK 0
271 # define X_OK 1
272 # define W_OK 2
273 # define R_OK 4
274 #endif
276 #ifdef HAVE_DIRENT_H
277 # include <dirent.h>
278 # define NLENGTH(direct) (strlen((direct)->d_name))
279 #else /* not HAVE_DIRENT_H */
280 # define dirent direct
281 # define NLENGTH(direct) ((direct)->d_namlen)
282 # ifdef HAVE_SYS_NDIR_H
283 # include <sys/ndir.h>
284 # endif /* HAVE_SYS_NDIR_H */
285 # ifdef HAVE_SYS_DIR_H
286 # include <sys/dir.h>
287 # endif /* HAVE_SYS_DIR_H */
288 # ifdef HAVE_NDIR_H
289 # include <ndir.h>
290 # endif /* HAVE_NDIR_H */
291 #endif /* HAVE_DIRENT_H */
293 #ifdef CLOSEDIR_VOID
294 /* Fake a return value. */
295 # define CLOSEDIR(d) (closedir (d), 0)
296 #else
297 # define CLOSEDIR(d) closedir (d)
298 #endif
300 /* Get or fake the disk device blocksize.
301 Usually defined by sys/param.h (if at all). */
302 #ifndef DEV_BSIZE
303 # ifdef BSIZE
304 # define DEV_BSIZE BSIZE
305 # else /* !BSIZE */
306 # define DEV_BSIZE 4096
307 # endif /* !BSIZE */
308 #endif /* !DEV_BSIZE */
310 /* Extract or fake data from a `struct stat'.
311 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
312 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
313 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
314 #ifndef HAVE_ST_BLOCKS
315 # define ST_BLKSIZE(statbuf) DEV_BSIZE
316 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
317 # define ST_NBLOCKS(statbuf) ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
318 # else /* !_POSIX_SOURCE && BSIZE */
319 # define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
320 # endif /* !_POSIX_SOURCE && BSIZE */
321 #else /* HAVE_ST_BLOCKS */
322 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
323 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
324 ? (statbuf).st_blksize : DEV_BSIZE)
325 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
326 /* HP-UX counts st_blocks in 1024-byte units.
327 This loses when mixing HP-UX and BSD filesystems with NFS. */
328 # define ST_NBLOCKSIZE 1024
329 # else /* !hpux */
330 # if defined(_AIX) && defined(_I386)
331 /* AIX PS/2 counts st_blocks in 4K units. */
332 # define ST_NBLOCKSIZE (4 * 1024)
333 # else /* not AIX PS/2 */
334 # if defined(_CRAY)
335 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE)
336 # endif /* _CRAY */
337 # endif /* not AIX PS/2 */
338 # endif /* !hpux */
339 #endif /* HAVE_ST_BLOCKS */
341 #ifndef ST_NBLOCKS
342 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
343 #endif
345 #ifndef ST_NBLOCKSIZE
346 # define ST_NBLOCKSIZE 512
347 #endif
349 #include "sys2.h"