.
[coreutils.git] / src / system.h
blob9654672c58ec3c6f8c8b24fdfd754093622da8d3
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2 Copyright (C) 1989, 1991-2002 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 #if 2 <= __GLIBC__ && 2 <= __GLIBC_MINOR__
21 # if ! defined _SYS_TYPES_H
22 you must include <sys/types.h> before including this file
23 # endif
24 #endif
26 #include <sys/stat.h>
28 #if !defined HAVE_MKFIFO
29 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
30 #endif
32 #if HAVE_SYS_PARAM_H
33 # include <sys/param.h>
34 #endif
36 /* <unistd.h> should be included before any preprocessor test
37 of _POSIX_VERSION. */
38 #if HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
42 #ifndef STDIN_FILENO
43 # define STDIN_FILENO 0
44 #endif
46 #ifndef STDOUT_FILENO
47 # define STDOUT_FILENO 1
48 #endif
50 #ifndef STDERR_FILENO
51 # define STDERR_FILENO 2
52 #endif
55 #if HAVE_LIMITS_H
56 /* limits.h must come before pathmax.h because limits.h on some systems
57 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
58 # include <limits.h>
59 #endif
61 #include "pathmax.h"
63 #if TIME_WITH_SYS_TIME
64 # include <sys/time.h>
65 # include <time.h>
66 #else
67 # if HAVE_SYS_TIME_H
68 # include <sys/time.h>
69 # else
70 # include <time.h>
71 # endif
72 #endif
74 /* Since major is a function on SVR4, we can't use `ifndef major'. */
75 #if MAJOR_IN_MKDEV
76 # include <sys/mkdev.h>
77 # define HAVE_MAJOR
78 #endif
79 #if MAJOR_IN_SYSMACROS
80 # include <sys/sysmacros.h>
81 # define HAVE_MAJOR
82 #endif
83 #ifdef major /* Might be defined in sys/types.h. */
84 # define HAVE_MAJOR
85 #endif
87 #ifndef HAVE_MAJOR
88 # define major(dev) (((dev) >> 8) & 0xff)
89 # define minor(dev) ((dev) & 0xff)
90 # define makedev(maj, min) (((maj) << 8) | (min))
91 #endif
92 #undef HAVE_MAJOR
94 #if HAVE_UTIME_H
95 # include <utime.h>
96 #endif
98 /* Some systems (even some that do have <utime.h>) don't declare this
99 structure anywhere. */
100 #ifndef HAVE_STRUCT_UTIMBUF
101 struct utimbuf
103 long actime;
104 long modtime;
106 #endif
108 /* Don't use bcopy! Use memmove if source and destination may overlap,
109 memcpy otherwise. */
111 #if HAVE_STRING_H
112 # if !STDC_HEADERS && HAVE_MEMORY_H
113 # include <memory.h>
114 # endif
115 # include <string.h>
116 #else
117 # include <strings.h>
118 #endif
119 #if ! HAVE_DECL_MEMRCHR
120 void *memrchr (const void *, int, size_t);
121 #endif
123 #include <errno.h>
124 #ifndef errno
125 extern int errno;
126 #endif
128 #if HAVE_STDBOOL_H
129 # include <stdbool.h>
130 #else
131 typedef enum {false = 0, true = 1} bool;
132 #endif
134 #if HAVE_STDLIB_H
135 # define getopt system_getopt
136 # include <stdlib.h>
137 # undef getopt
138 #endif
140 /* The following test is to work around the gross typo in
141 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
142 is defined to 0, not 1. */
143 #if !EXIT_FAILURE
144 # undef EXIT_FAILURE
145 # define EXIT_FAILURE 1
146 #endif
148 #ifndef EXIT_SUCCESS
149 # define EXIT_SUCCESS 0
150 #endif
152 #if HAVE_FCNTL_H
153 # include <fcntl.h>
154 #else
155 # include <sys/file.h>
156 #endif
158 #if !defined SEEK_SET
159 # define SEEK_SET 0
160 # define SEEK_CUR 1
161 # define SEEK_END 2
162 #endif
163 #ifndef F_OK
164 # define F_OK 0
165 # define X_OK 1
166 # define W_OK 2
167 # define R_OK 4
168 #endif
170 /* For systems that distinguish between text and binary I/O.
171 O_BINARY is usually declared in fcntl.h */
172 #if !defined O_BINARY && defined _O_BINARY
173 /* For MSC-compatible compilers. */
174 # define O_BINARY _O_BINARY
175 # define O_TEXT _O_TEXT
176 #endif
178 #ifdef __BEOS__
179 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
180 # undef O_BINARY
181 # undef O_TEXT
182 #endif
184 #if O_BINARY
185 # ifndef __DJGPP__
186 # define setmode _setmode
187 # define fileno(_fp) _fileno (_fp)
188 # endif /* not DJGPP */
189 # define SET_MODE(_f, _m) setmode (_f, _m)
190 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
191 # define SET_BINARY2(_f1, _f2) \
192 do { \
193 if (!isatty (_f1)) \
195 setmode (_f1, O_BINARY); \
196 if (!isatty (_f2)) \
197 setmode (_f2, O_BINARY); \
199 } while(0)
200 #else
201 # define SET_MODE(_f, _m) (void)0
202 # define SET_BINARY(f) (void)0
203 # define SET_BINARY2(f1,f2) (void)0
204 # define O_BINARY 0
205 # define O_TEXT 0
206 #endif /* O_BINARY */
208 #if HAVE_DIRENT_H
209 # include <dirent.h>
210 # define NLENGTH(direct) (strlen((direct)->d_name))
211 #else /* not HAVE_DIRENT_H */
212 # define dirent direct
213 # define NLENGTH(direct) ((direct)->d_namlen)
214 # if HAVE_SYS_NDIR_H
215 # include <sys/ndir.h>
216 # endif /* HAVE_SYS_NDIR_H */
217 # if HAVE_SYS_DIR_H
218 # include <sys/dir.h>
219 # endif /* HAVE_SYS_DIR_H */
220 # if HAVE_NDIR_H
221 # include <ndir.h>
222 # endif /* HAVE_NDIR_H */
223 #endif /* HAVE_DIRENT_H */
225 #if CLOSEDIR_VOID
226 /* Fake a return value. */
227 # define CLOSEDIR(d) (closedir (d), 0)
228 #else
229 # define CLOSEDIR(d) closedir (d)
230 #endif
232 /* Get or fake the disk device blocksize.
233 Usually defined by sys/param.h (if at all). */
234 #if !defined DEV_BSIZE && defined BSIZE
235 # define DEV_BSIZE BSIZE
236 #endif
237 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
238 # define DEV_BSIZE BBSIZE
239 #endif
240 #ifndef DEV_BSIZE
241 # define DEV_BSIZE 4096
242 #endif
244 /* Extract or fake data from a `struct stat'.
245 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
246 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
247 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
248 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
249 # define ST_BLKSIZE(statbuf) DEV_BSIZE
250 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
251 # define ST_NBLOCKS(statbuf) \
252 ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
253 # else /* !_POSIX_SOURCE && BSIZE */
254 # define ST_NBLOCKS(statbuf) \
255 (S_ISREG ((statbuf).st_mode) \
256 || S_ISDIR ((statbuf).st_mode) \
257 ? st_blocks ((statbuf).st_size) : 0)
258 # endif /* !_POSIX_SOURCE && BSIZE */
259 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
260 /* Some systems, like Sequents, return st_blksize of 0 on pipes.
261 Also, when running `rsh hpux11-system cat any-file', cat would
262 determine that the output stream had an st_blksize of 2147421096.
263 So here we arbitrarily limit the `optimal' block size to 4MB.
264 If anyone knows of a system for which the legitimate value for
265 st_blksize can exceed 4MB, please report it as a bug in this code. */
266 # define ST_BLKSIZE(statbuf) ((0 < (statbuf).st_blksize \
267 && (statbuf).st_blksize <= (1 << 22)) /* 4MB */ \
268 ? (statbuf).st_blksize : DEV_BSIZE)
269 # if defined hpux || defined __hpux__ || defined __hpux
270 /* HP-UX counts st_blocks in 1024-byte units.
271 This loses when mixing HP-UX and BSD filesystems with NFS. */
272 # define ST_NBLOCKSIZE 1024
273 # else /* !hpux */
274 # if defined _AIX && defined _I386
275 /* AIX PS/2 counts st_blocks in 4K units. */
276 # define ST_NBLOCKSIZE (4 * 1024)
277 # else /* not AIX PS/2 */
278 # if defined _CRAY
279 # define ST_NBLOCKS(statbuf) \
280 (S_ISREG ((statbuf).st_mode) \
281 || S_ISDIR ((statbuf).st_mode) \
282 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
283 # endif /* _CRAY */
284 # endif /* not AIX PS/2 */
285 # endif /* !hpux */
286 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
288 #ifndef ST_NBLOCKS
289 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
290 #endif
292 #ifndef ST_NBLOCKSIZE
293 # define ST_NBLOCKSIZE 512
294 #endif
296 #include "sys2.h"