(jm_FILE_SYSTEM_USAGE): Add `[]' between use of
[coreutils.git] / src / system.h
blob582fd7c514cbd6c645f7b2776d9092e7b98209e2
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2 Copyright (C) 1989, 1991-2000 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 #if !defined(HAVE_MKFIFO)
23 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
24 #endif
26 #if HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
30 /* <unistd.h> should be included before any preprocessor test
31 of _POSIX_VERSION. */
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
36 #ifndef STDIN_FILENO
37 # define STDIN_FILENO 0
38 #endif
40 #ifndef STDOUT_FILENO
41 # define STDOUT_FILENO 1
42 #endif
44 #ifndef STDERR_FILENO
45 # define STDERR_FILENO 2
46 #endif
49 #if HAVE_LIMITS_H
50 /* limits.h must come before pathmax.h because limits.h on some systems
51 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
52 # include <limits.h>
53 #endif
55 #include "pathmax.h"
57 #if TIME_WITH_SYS_TIME
58 # include <sys/time.h>
59 # include <time.h>
60 #else
61 # if HAVE_SYS_TIME_H
62 # include <sys/time.h>
63 # else
64 # include <time.h>
65 # endif
66 #endif
68 /* Since major is a function on SVR4, we can't use `ifndef major'. */
69 #if MAJOR_IN_MKDEV
70 # include <sys/mkdev.h>
71 # define HAVE_MAJOR
72 #endif
73 #if MAJOR_IN_SYSMACROS
74 # include <sys/sysmacros.h>
75 # define HAVE_MAJOR
76 #endif
77 #ifdef major /* Might be defined in sys/types.h. */
78 # define HAVE_MAJOR
79 #endif
81 #ifndef HAVE_MAJOR
82 # define major(dev) (((dev) >> 8) & 0xff)
83 # define minor(dev) ((dev) & 0xff)
84 # define makedev(maj, min) (((maj) << 8) | (min))
85 #endif
86 #undef HAVE_MAJOR
88 #if HAVE_UTIME_H
89 # include <utime.h>
90 #endif
92 /* Some systems (even some that do have <utime.h>) don't declare this
93 structure anywhere. */
94 #ifndef HAVE_STRUCT_UTIMBUF
95 struct utimbuf
97 long actime;
98 long modtime;
100 #endif
102 /* Don't use bcopy! Use memmove if source and destination may overlap,
103 memcpy otherwise. */
105 #if HAVE_STRING_H
106 # if !STDC_HEADERS && HAVE_MEMORY_H
107 # include <memory.h>
108 # endif
109 # include <string.h>
110 #else
111 # include <strings.h>
112 #endif
114 #include <errno.h>
115 #ifndef errno
116 extern int errno;
117 #endif
119 #if HAVE_STDLIB_H
120 # define getopt system_getopt
121 # include <stdlib.h>
122 # undef getopt
123 #endif
125 /* The following test is to work around the gross typo in
126 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
127 is defined to 0, not 1. */
128 #if !EXIT_FAILURE
129 # undef EXIT_FAILURE
130 # define EXIT_FAILURE 1
131 #endif
133 #ifndef EXIT_SUCCESS
134 # define EXIT_SUCCESS 0
135 #endif
137 #if HAVE_FCNTL_H
138 # include <fcntl.h>
139 #else
140 # include <sys/file.h>
141 #endif
143 #if !defined (SEEK_SET)
144 # define SEEK_SET 0
145 # define SEEK_CUR 1
146 # define SEEK_END 2
147 #endif
148 #ifndef F_OK
149 # define F_OK 0
150 # define X_OK 1
151 # define W_OK 2
152 # define R_OK 4
153 #endif
155 /* For systems that distinguish between text and binary I/O.
156 O_BINARY is usually declared in fcntl.h */
157 #if !defined O_BINARY && defined _O_BINARY
158 /* For MSC-compatible compilers. */
159 # define O_BINARY _O_BINARY
160 # define O_TEXT _O_TEXT
161 #endif
163 #ifdef __BEOS__
164 /* BeOS 5 has O_BINARY and O_TEXT, but they have no effect. */
165 # undef O_BINARY
166 # undef O_TEXT
167 #endif
169 #if O_BINARY
170 # ifndef __DJGPP__
171 # define setmode _setmode
172 # define fileno(_fp) _fileno (_fp)
173 # endif /* not DJGPP */
174 # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
175 # define SET_BINARY2(_f1, _f2) \
176 do { \
177 if (!isatty (_f1)) \
179 setmode (_f1, O_BINARY); \
180 if (!isatty (_f2)) \
181 setmode (_f2, O_BINARY); \
183 } while(0)
184 #else
185 # define SET_BINARY(f) (void)0
186 # define SET_BINARY2(f1,f2) (void)0
187 # define O_BINARY 0
188 # define O_TEXT 0
189 #endif /* O_BINARY */
191 #if HAVE_DIRENT_H
192 # include <dirent.h>
193 # define NLENGTH(direct) (strlen((direct)->d_name))
194 #else /* not HAVE_DIRENT_H */
195 # define dirent direct
196 # define NLENGTH(direct) ((direct)->d_namlen)
197 # if HAVE_SYS_NDIR_H
198 # include <sys/ndir.h>
199 # endif /* HAVE_SYS_NDIR_H */
200 # if HAVE_SYS_DIR_H
201 # include <sys/dir.h>
202 # endif /* HAVE_SYS_DIR_H */
203 # if HAVE_NDIR_H
204 # include <ndir.h>
205 # endif /* HAVE_NDIR_H */
206 #endif /* HAVE_DIRENT_H */
208 #if CLOSEDIR_VOID
209 /* Fake a return value. */
210 # define CLOSEDIR(d) (closedir (d), 0)
211 #else
212 # define CLOSEDIR(d) closedir (d)
213 #endif
215 /* Get or fake the disk device blocksize.
216 Usually defined by sys/param.h (if at all). */
217 #if !defined DEV_BSIZE && defined BSIZE
218 # define DEV_BSIZE BSIZE
219 #endif
220 #if !defined DEV_BSIZE && defined BBSIZE /* SGI */
221 # define DEV_BSIZE BBSIZE
222 #endif
223 #ifndef DEV_BSIZE
224 # define DEV_BSIZE 4096
225 #endif
227 /* Extract or fake data from a `struct stat'.
228 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
229 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
230 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
231 #ifndef HAVE_STRUCT_STAT_ST_BLOCKS
232 # define ST_BLKSIZE(statbuf) DEV_BSIZE
233 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
234 # define ST_NBLOCKS(statbuf) \
235 (S_ISREG ((statbuf).st_mode) \
236 || S_ISDIR ((statbuf).st_mode) \
237 ? (statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0) : 0)
238 # else /* !_POSIX_SOURCE && BSIZE */
239 # define ST_NBLOCKS(statbuf) \
240 (S_ISREG ((statbuf).st_mode) \
241 || S_ISDIR ((statbuf).st_mode) \
242 ? st_blocks ((statbuf).st_size) : 0)
243 # endif /* !_POSIX_SOURCE && BSIZE */
244 #else /* HAVE_STRUCT_STAT_ST_BLOCKS */
245 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
246 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
247 ? (statbuf).st_blksize : DEV_BSIZE)
248 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
249 /* HP-UX counts st_blocks in 1024-byte units.
250 This loses when mixing HP-UX and BSD filesystems with NFS. */
251 # define ST_NBLOCKSIZE 1024
252 # else /* !hpux */
253 # if defined(_AIX) && defined(_I386)
254 /* AIX PS/2 counts st_blocks in 4K units. */
255 # define ST_NBLOCKSIZE (4 * 1024)
256 # else /* not AIX PS/2 */
257 # if defined(_CRAY)
258 # define ST_NBLOCKS(statbuf) \
259 (S_ISREG ((statbuf).st_mode) \
260 || S_ISDIR ((statbuf).st_mode) \
261 ? (statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE : 0)
262 # endif /* _CRAY */
263 # endif /* not AIX PS/2 */
264 # endif /* !hpux */
265 #endif /* HAVE_STRUCT_STAT_ST_BLOCKS */
267 #ifndef ST_NBLOCKS
268 # define ST_NBLOCKS(statbuf) \
269 (S_ISREG ((statbuf).st_mode) \
270 || S_ISDIR ((statbuf).st_mode) \
271 ? (statbuf).st_blocks : 0)
272 #endif
274 #ifndef ST_NBLOCKSIZE
275 # define ST_NBLOCKSIZE 512
276 #endif
278 #include "sys2.h"