*** empty log message ***
[coreutils.git] / src / system.h
bloba2c08d2f6b8266dc4ab7777e2b4d39241041cb34
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
120 #include <errno.h>
121 #ifndef errno
122 extern int errno;
123 #endif
125 #if HAVE_STDBOOL_H
126 # include <stdbool.h>
127 #else
128 typedef enum {false = 0, true = 1} bool;
129 #endif
131 #if HAVE_STDLIB_H
132 # define getopt system_getopt
133 # include <stdlib.h>
134 # undef getopt
135 #endif
137 /* The following test is to work around the gross typo in
138 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
139 is defined to 0, not 1. */
140 #if !EXIT_FAILURE
141 # undef EXIT_FAILURE
142 # define EXIT_FAILURE 1
143 #endif
145 #ifndef EXIT_SUCCESS
146 # define EXIT_SUCCESS 0
147 #endif
149 #if HAVE_FCNTL_H
150 # include <fcntl.h>
151 #else
152 # include <sys/file.h>
153 #endif
155 #if !defined SEEK_SET
156 # define SEEK_SET 0
157 # define SEEK_CUR 1
158 # define SEEK_END 2
159 #endif
160 #ifndef F_OK
161 # define F_OK 0
162 # define X_OK 1
163 # define W_OK 2
164 # define R_OK 4
165 #endif
167 /* For systems that distinguish between text and binary I/O.
168 O_BINARY is usually declared in fcntl.h */
169 #if !defined O_BINARY && defined _O_BINARY
170 /* For MSC-compatible compilers. */
171 # define O_BINARY _O_BINARY
172 # define O_TEXT _O_TEXT
173 #endif
175 #ifdef __BEOS__
176 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
177 # undef O_BINARY
178 # undef O_TEXT
179 #endif
181 #if O_BINARY
182 # ifndef __DJGPP__
183 # define setmode _setmode
184 # define fileno(_fp) _fileno (_fp)
185 # endif /* not DJGPP */
186 # define SET_MODE(_f, _m) setmode (_f, _m)
187 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
188 # define SET_BINARY2(_f1, _f2) \
189 do { \
190 if (!isatty (_f1)) \
192 setmode (_f1, O_BINARY); \
193 if (!isatty (_f2)) \
194 setmode (_f2, O_BINARY); \
196 } while(0)
197 #else
198 # define SET_MODE(_f, _m) (void)0
199 # define SET_BINARY(f) (void)0
200 # define SET_BINARY2(f1,f2) (void)0
201 # define O_BINARY 0
202 # define O_TEXT 0
203 #endif /* O_BINARY */
205 #if HAVE_DIRENT_H
206 # include <dirent.h>
207 # define NLENGTH(direct) (strlen((direct)->d_name))
208 #else /* not HAVE_DIRENT_H */
209 # define dirent direct
210 # define NLENGTH(direct) ((direct)->d_namlen)
211 # if HAVE_SYS_NDIR_H
212 # include <sys/ndir.h>
213 # endif /* HAVE_SYS_NDIR_H */
214 # if HAVE_SYS_DIR_H
215 # include <sys/dir.h>
216 # endif /* HAVE_SYS_DIR_H */
217 # if HAVE_NDIR_H
218 # include <ndir.h>
219 # endif /* HAVE_NDIR_H */
220 #endif /* HAVE_DIRENT_H */
222 #if CLOSEDIR_VOID
223 /* Fake a return value. */
224 # define CLOSEDIR(d) (closedir (d), 0)
225 #else
226 # define CLOSEDIR(d) closedir (d)
227 #endif
229 /* Get or fake the disk device blocksize.
230 Usually defined by sys/param.h (if at all). */
231 #if !defined DEV_BSIZE && defined BSIZE
232 # define DEV_BSIZE BSIZE
233 #endif
234 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
235 # define DEV_BSIZE BBSIZE
236 #endif
237 #ifndef DEV_BSIZE
238 # define DEV_BSIZE 4096
239 #endif
241 /* Extract or fake data from a `struct stat'.
242 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
243 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
244 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
245 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
246 # define ST_BLKSIZE(statbuf) DEV_BSIZE
247 # if defined _POSIX_SOURCE || !defined BSIZE /* fileblocks.c uses BSIZE. */
248 # define ST_NBLOCKS(statbuf) \
249 (S_ISREG ((statbuf).st_mode) \
250 || S_ISDIR ((statbuf).st_mode) \
251 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
252 # else /* !_POSIX_SOURCE && BSIZE */
253 # define ST_NBLOCKS(statbuf) \
254 (S_ISREG ((statbuf).st_mode) \
255 || S_ISDIR ((statbuf).st_mode) \
256 ? st_blocks ((statbuf).st_size) : 0)
257 # endif /* !_POSIX_SOURCE && BSIZE */
258 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
259 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
260 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
261 ? (statbuf).st_blksize : DEV_BSIZE)
262 # if defined hpux || defined __hpux__ || defined __hpux
263 /* HP-UX counts st_blocks in 1024-byte units.
264 This loses when mixing HP-UX and BSD filesystems with NFS. */
265 # define ST_NBLOCKSIZE 1024
266 # else /* !hpux */
267 # if defined _AIX && defined _I386
268 /* AIX PS/2 counts st_blocks in 4K units. */
269 # define ST_NBLOCKSIZE (4 * 1024)
270 # else /* not AIX PS/2 */
271 # if defined _CRAY
272 # define ST_NBLOCKS(statbuf) \
273 (S_ISREG ((statbuf).st_mode) \
274 || S_ISDIR ((statbuf).st_mode) \
275 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
276 # endif /* _CRAY */
277 # endif /* not AIX PS/2 */
278 # endif /* !hpux */
279 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
281 #ifndef ST_NBLOCKS
282 # define ST_NBLOCKS(statbuf) \
283 (S_ISREG ((statbuf).st_mode) \
284 || S_ISDIR ((statbuf).st_mode) \
285 ? (statbuf).st_blocks : 0)
286 #endif
288 #ifndef ST_NBLOCKSIZE
289 # define ST_NBLOCKSIZE 512
290 #endif
292 #include "sys2.h"