Add tests with filenames containing newline and backslash characters.
[coreutils.git] / src / system.h
blob50159222869869e306b4ade7635ece58ef1fe6b4
1 /* system-dependent definitions for fileutils, textutils, and sh-utils packages.
2 Copyright (C) 89, 91, 92, 93, 94, 96, 97, 1998 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_ISFIFO
27 # undef S_ISLNK
28 # undef S_ISMPB
29 # undef S_ISMPC
30 # undef S_ISNWK
31 # undef S_ISREG
32 # undef S_ISSOCK
33 #endif /* STAT_MACROS_BROKEN. */
35 #ifndef S_IFMT
36 # define S_IFMT 0170000
37 #endif
38 #if !defined(S_ISBLK) && defined(S_IFBLK)
39 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
40 #endif
41 #if !defined(S_ISCHR) && defined(S_IFCHR)
42 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
43 #endif
44 #if !defined(S_ISDIR) && defined(S_IFDIR)
45 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 #endif
47 #if !defined(S_ISREG) && defined(S_IFREG)
48 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
49 #endif
50 #if !defined(S_ISFIFO) && defined(S_IFIFO)
51 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
52 #endif
53 #if !defined(S_ISLNK) && defined(S_IFLNK)
54 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
55 #endif
56 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
57 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
58 #endif
59 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
60 # define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
61 # define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
62 #endif
63 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
64 # define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
65 #endif
67 #ifndef S_IEXEC
68 # define S_IEXEC S_IXUSR
69 #endif
71 #ifndef S_IXUSR
72 # define S_IXUSR S_IEXEC
73 #endif
74 #ifndef S_IXGRP
75 # define S_IXGRP (S_IEXEC >> 3)
76 #endif
77 #ifndef S_IXOTH
78 # define S_IXOTH (S_IEXEC >> 6)
79 #endif
80 #ifndef S_IXUGO
81 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
82 #endif
84 #ifdef ST_MTIM_NSEC
85 # define ST_TIME_CMP_NS(a, b, ns) ((a).ns < (b).ns ? -1 : (a).ns > (b).ns)
86 #else
87 # define ST_TIME_CMP_NS(a, b, ns) 0
88 #endif
89 #define ST_TIME_CMP(a, b, s, ns) \
90 ((a).s < (b).s ? -1 : (a).s > (b).s ? 1 : ST_TIME_CMP_NS(a, b, ns))
91 #define ATIME_CMP(a, b) ST_TIME_CMP (a, b, st_atime, st_atim.ST_MTIM_NSEC)
92 #define CTIME_CMP(a, b) ST_TIME_CMP (a, b, st_ctime, st_ctim.ST_MTIM_NSEC)
93 #define MTIME_CMP(a, b) ST_TIME_CMP (a, b, st_mtime, st_mtim.ST_MTIM_NSEC)
95 #if !defined(HAVE_MKFIFO)
96 # define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
97 #endif
99 #ifdef HAVE_SYS_PARAM_H
100 # include <sys/param.h>
101 #endif
103 /* <unistd.h> should be included before any preprocessor test
104 of _POSIX_VERSION. */
105 #ifdef HAVE_UNISTD_H
106 # include <unistd.h>
107 #endif
109 #ifndef STDIN_FILENO
110 # define STDIN_FILENO 0
111 #endif
113 #ifndef STDOUT_FILENO
114 # define STDOUT_FILENO 1
115 #endif
117 #ifndef STDERR_FILENO
118 # define STDERR_FILENO 2
119 #endif
122 #if HAVE_LIMITS_H
123 /* limits.h must come before pathmax.h because limits.h on some systems
124 undefs PATH_MAX, whereas pathmax.h sets PATH_MAX. */
125 # include <limits.h>
126 #endif
128 #ifndef CHAR_BIT
129 # define CHAR_BIT 8
130 #endif
132 /* The extra casts work around common compiler bugs,
133 e.g. Cray C 5.0.3.0 when t == time_t. */
134 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
135 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
136 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
137 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
139 #ifndef CHAR_MIN
140 # define CHAR_MIN TYPE_MINIMUM (char)
141 #endif
143 #ifndef CHAR_MAX
144 # define CHAR_MAX TYPE_MAXIMUM (char)
145 #endif
147 #ifndef UCHAR_MAX
148 # define UCHAR_MAX TYPE_MAXIMUM (unsigned char)
149 #endif
151 #ifndef SHRT_MIN
152 # define SHRT_MIN TYPE_MINIMUM (short int)
153 #endif
155 #ifndef SHRT_MAX
156 # define SHRT_MAX TYPE_MAXIMUM (short int)
157 #endif
159 #ifndef INT_MAX
160 # define INT_MAX TYPE_MAXIMUM (int)
161 #endif
163 #ifndef UINT_MAX
164 # define UINT_MAX TYPE_MAXIMUM (unsigned int)
165 #endif
167 #ifndef LONG_MAX
168 # define LONG_MAX TYPE_MAXIMUM (long)
169 #endif
171 #ifndef ULONG_MAX
172 # define ULONG_MAX TYPE_MAXIMUM (unsigned long)
173 #endif
175 #include "pathmax.h"
177 #ifdef TM_IN_SYS_TIME
178 # include <sys/time.h>
179 #else
180 # include <time.h>
181 #endif
183 /* Since major is a function on SVR4, we can't use `ifndef major'. */
184 #ifdef MAJOR_IN_MKDEV
185 # include <sys/mkdev.h>
186 # define HAVE_MAJOR
187 #endif
188 #ifdef MAJOR_IN_SYSMACROS
189 # include <sys/sysmacros.h>
190 # define HAVE_MAJOR
191 #endif
192 #ifdef major /* Might be defined in sys/types.h. */
193 # define HAVE_MAJOR
194 #endif
196 #ifndef HAVE_MAJOR
197 # define major(dev) (((dev) >> 8) & 0xff)
198 # define minor(dev) ((dev) & 0xff)
199 # define makedev(maj, min) (((maj) << 8) | (min))
200 #endif
201 #undef HAVE_MAJOR
203 #ifdef HAVE_UTIME_H
204 # include <utime.h>
205 #endif
207 /* Some systems (even some that do have <utime.h>) don't declare this
208 structure anywhere. */
209 #ifndef HAVE_STRUCT_UTIMBUF
210 struct utimbuf
212 long actime;
213 long modtime;
215 #endif
217 /* Don't use bcopy! Use memmove if source and destination may overlap,
218 memcpy otherwise. */
220 #ifdef HAVE_STRING_H
221 # if !STDC_HEADERS && HAVE_MEMORY_H
222 # include <memory.h>
223 # endif
224 # include <string.h>
225 #else
226 # include <strings.h>
227 #endif
229 #include <errno.h>
230 #ifndef errno
231 extern int errno;
232 #endif
234 #if HAVE_STDLIB_H
235 # define getopt system_getopt
236 # include <stdlib.h>
237 # undef getopt
238 #endif
240 /* The following test is to work around the gross typo in
241 systems like Sony NEWS-OS Release 4.0C, whereby EXIT_FAILURE
242 is defined to 0, not 1. */
243 #if !EXIT_FAILURE
244 # undef EXIT_FAILURE
245 # define EXIT_FAILURE 1
246 #endif
248 #ifndef EXIT_SUCCESS
249 # define EXIT_SUCCESS 0
250 #endif
252 #ifdef HAVE_FCNTL_H
253 # include <fcntl.h>
254 #else
255 # include <sys/file.h>
256 #endif
258 #if !defined (SEEK_SET)
259 # define SEEK_SET 0
260 # define SEEK_CUR 1
261 # define SEEK_END 2
262 #endif
263 #ifndef F_OK
264 # define F_OK 0
265 # define X_OK 1
266 # define W_OK 2
267 # define R_OK 4
268 #endif
270 #ifdef HAVE_DIRENT_H
271 # include <dirent.h>
272 # define NLENGTH(direct) (strlen((direct)->d_name))
273 #else /* not HAVE_DIRENT_H */
274 # define dirent direct
275 # define NLENGTH(direct) ((direct)->d_namlen)
276 # ifdef HAVE_SYS_NDIR_H
277 # include <sys/ndir.h>
278 # endif /* HAVE_SYS_NDIR_H */
279 # ifdef HAVE_SYS_DIR_H
280 # include <sys/dir.h>
281 # endif /* HAVE_SYS_DIR_H */
282 # ifdef HAVE_NDIR_H
283 # include <ndir.h>
284 # endif /* HAVE_NDIR_H */
285 #endif /* HAVE_DIRENT_H */
287 #ifdef CLOSEDIR_VOID
288 /* Fake a return value. */
289 # define CLOSEDIR(d) (closedir (d), 0)
290 #else
291 # define CLOSEDIR(d) closedir (d)
292 #endif
294 /* Get or fake the disk device blocksize.
295 Usually defined by sys/param.h (if at all). */
296 #ifndef DEV_BSIZE
297 # ifdef BSIZE
298 # define DEV_BSIZE BSIZE
299 # else /* !BSIZE */
300 # define DEV_BSIZE 4096
301 # endif /* !BSIZE */
302 #endif /* !DEV_BSIZE */
304 /* Extract or fake data from a `struct stat'.
305 ST_BLKSIZE: Preferred I/O blocksize for the file, in bytes.
306 ST_NBLOCKS: Number of blocks in the file, including indirect blocks.
307 ST_NBLOCKSIZE: Size of blocks used when calculating ST_NBLOCKS. */
308 #ifndef HAVE_ST_BLOCKS
309 # define ST_BLKSIZE(statbuf) DEV_BSIZE
310 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
311 # define ST_NBLOCKS(statbuf) ((statbuf).st_size / ST_NBLOCKSIZE + ((statbuf).st_size % ST_NBLOCKSIZE != 0))
312 # else /* !_POSIX_SOURCE && BSIZE */
313 # define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
314 # endif /* !_POSIX_SOURCE && BSIZE */
315 #else /* HAVE_ST_BLOCKS */
316 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
317 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
318 ? (statbuf).st_blksize : DEV_BSIZE)
319 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
320 /* HP-UX counts st_blocks in 1024-byte units.
321 This loses when mixing HP-UX and BSD filesystems with NFS. */
322 # define ST_NBLOCKSIZE 1024
323 # else /* !hpux */
324 # if defined(_AIX) && defined(_I386)
325 /* AIX PS/2 counts st_blocks in 4K units. */
326 # define ST_NBLOCKSIZE (4 * 1024)
327 # else /* not AIX PS/2 */
328 # if defined(_CRAY)
329 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * ST_BLKSIZE(statbuf)/ST_NBLOCKSIZE)
330 # endif /* _CRAY */
331 # endif /* not AIX PS/2 */
332 # endif /* !hpux */
333 #endif /* HAVE_ST_BLOCKS */
335 #ifndef ST_NBLOCKS
336 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
337 #endif
339 #ifndef ST_NBLOCKSIZE
340 # define ST_NBLOCKSIZE 512
341 #endif
343 #include "sys2.h"