.
[coreutils.git] / src / system.h
blobcdd8f3e394fea29e70545d8b2bda8fe308cbe99a
1 /* system-dependent definitions for fileutils programs.
2 Copyright (C) 89, 91, 92, 93, 1994 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 #ifdef S_IEXEC
72 #ifndef S_IXUSR
73 #define S_IXUSR S_IEXEC
74 #endif
75 #ifndef S_IXGRP
76 #define S_IXGRP (S_IEXEC >> 3)
77 #endif
78 #ifndef S_IXOTH
79 #define S_IXOTH (S_IEXEC >> 6)
80 #endif
81 #endif /* S_IEXEC */
83 #if !defined(HAVE_MKFIFO)
84 #define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
85 #endif
87 #ifdef HAVE_SYS_PARAM_H
88 #include <sys/param.h>
89 #endif
91 /* <unistd.h> should be included before any preprocessor test
92 of _POSIX_VERSION. */
93 #ifdef HAVE_UNISTD_H
94 #include <unistd.h>
95 #endif
97 #ifndef STDIN_FILENO
98 #define STDIN_FILENO 0
99 #endif
101 #ifndef STDOUT_FILENO
102 #define STDOUT_FILENO 1
103 #endif
105 #ifndef STDERR_FILENO
106 #define STDERR_FILENO 2
107 #endif
109 #include "pathmax.h"
111 /* FIXME: Don't use _POSIX_VERSION. */
112 #ifndef _POSIX_VERSION
113 off_t lseek ();
114 #endif
116 #ifdef TM_IN_SYS_TIME
117 #include <sys/time.h>
118 #else
119 #include <time.h>
120 #endif
122 /* Since major is a function on SVR4, we can't use `ifndef major'. */
123 #ifdef MAJOR_IN_MKDEV
124 #include <sys/mkdev.h>
125 #define HAVE_MAJOR
126 #endif
127 #ifdef MAJOR_IN_SYSMACROS
128 #include <sys/sysmacros.h>
129 #define HAVE_MAJOR
130 #endif
131 #ifdef major /* Might be defined in sys/types.h. */
132 #define HAVE_MAJOR
133 #endif
135 #ifndef HAVE_MAJOR
136 #define major(dev) (((dev) >> 8) & 0xff)
137 #define minor(dev) ((dev) & 0xff)
138 #define makedev(maj, min) (((maj) << 8) | (min))
139 #endif
140 #undef HAVE_MAJOR
142 #ifdef HAVE_UTIME_H
143 #include <utime.h>
144 #else
145 struct utimbuf
147 long actime;
148 long modtime;
150 #endif
152 /* Don't use bcopy! Use memmove if source and destination may overlap,
153 memcpy otherwise. */
155 #ifdef HAVE_STRING_H
156 # if !STDC_HEADERS && HAVE_MEMORY_H
157 # include <memory.h>
158 # endif
159 # include <string.h>
160 #else
161 # include <strings.h>
162 char *memchr ();
163 #endif
165 #include <errno.h>
166 #ifndef errno
167 extern int errno;
168 #endif
170 #ifdef STDC_HEADERS
171 #define getopt system_getopt
172 #include <stdlib.h>
173 #undef getopt
174 #else /* not STDC_HEADERS */
175 char *getenv ();
176 #endif /* STDC_HEADERS */
178 #ifdef HAVE_FCNTL_H
179 #include <fcntl.h>
180 #else
181 #include <sys/file.h>
182 #endif
184 #ifndef SEEK_SET
185 #define SEEK_SET 0
186 #define SEEK_CUR 1
187 #define SEEK_END 2
188 #endif
189 #ifndef F_OK
190 #define F_OK 0
191 #define X_OK 1
192 #define W_OK 2
193 #define R_OK 4
194 #endif
196 #ifdef HAVE_DIRENT_H
197 # include <dirent.h>
198 # define NLENGTH(direct) (strlen((direct)->d_name))
199 #else /* not HAVE_DIRENT_H */
200 # define dirent direct
201 # define NLENGTH(direct) ((direct)->d_namlen)
202 # ifdef HAVE_SYS_NDIR_H
203 # include <sys/ndir.h>
204 # endif /* HAVE_SYS_NDIR_H */
205 # ifdef HAVE_SYS_DIR_H
206 # include <sys/dir.h>
207 # endif /* HAVE_SYS_DIR_H */
208 # ifdef HAVE_NDIR_H
209 # include <ndir.h>
210 # endif /* HAVE_NDIR_H */
211 #endif /* HAVE_DIRENT_H */
213 #ifdef CLOSEDIR_VOID
214 /* Fake a return value. */
215 #define CLOSEDIR(d) (closedir (d), 0)
216 #else
217 #define CLOSEDIR(d) closedir (d)
218 #endif
220 /* Get or fake the disk device blocksize.
221 Usually defined by sys/param.h (if at all). */
222 #ifndef DEV_BSIZE
223 #ifdef BSIZE
224 #define DEV_BSIZE BSIZE
225 #else /* !BSIZE */
226 #define DEV_BSIZE 4096
227 #endif /* !BSIZE */
228 #endif /* !DEV_BSIZE */
230 /* Extract or fake data from a `struct stat'.
231 ST_BLKSIZE: Optimal I/O blocksize for the file, in bytes.
232 ST_NBLOCKS: Number of 512-byte blocks in the file
233 (including indirect blocks). */
234 #ifndef HAVE_ST_BLOCKS
235 # define ST_BLKSIZE(statbuf) DEV_BSIZE
236 # if defined(_POSIX_SOURCE) || !defined(BSIZE) /* fileblocks.c uses BSIZE. */
237 # define ST_NBLOCKS(statbuf) (((statbuf).st_size + 512 - 1) / 512)
238 # else /* !_POSIX_SOURCE && BSIZE */
239 # define ST_NBLOCKS(statbuf) (st_blocks ((statbuf).st_size))
240 # endif /* !_POSIX_SOURCE && BSIZE */
241 #else /* HAVE_ST_BLOCKS */
242 /* Some systems, like Sequents, return st_blksize of 0 on pipes. */
243 # define ST_BLKSIZE(statbuf) ((statbuf).st_blksize > 0 \
244 ? (statbuf).st_blksize : DEV_BSIZE)
245 # if defined(hpux) || defined(__hpux__) || defined(__hpux)
246 /* HP-UX counts st_blocks in 1024-byte units.
247 This loses when mixing HP-UX and BSD filesystems with NFS. */
248 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * 2)
249 # else /* !hpux */
250 # if defined(_AIX) && defined(_I386)
251 /* AIX PS/2 counts st_blocks in 4K units. */
252 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks * 8)
253 # else /* not AIX PS/2 */
254 # define ST_NBLOCKS(statbuf) ((statbuf).st_blocks)
255 # endif /* not AIX PS/2 */
256 # endif /* !hpux */
257 #endif /* HAVE_ST_BLOCKS */
259 /* Convert B 512-byte blocks to kilobytes if K is nonzero,
260 otherwise return it unchanged. */
261 #define convert_blocks(b, k) ((k) ? ((b) + 1) / 2 : (b))
263 #ifndef RETSIGTYPE
264 #define RETSIGTYPE void
265 #endif
267 #ifdef __GNUC__
268 # undef alloca
269 # define alloca __builtin_alloca
270 #else
271 # ifdef HAVE_ALLOCA_H
272 # include <alloca.h>
273 # else
274 # ifndef _AIX
275 /* AIX alloca decl has to be the first thing in the file, bletch! */
276 char *alloca ();
277 # endif
278 # endif
279 #endif
281 #include <ctype.h>
283 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
284 #define ISASCII(c) 1
285 #else
286 #define ISASCII(c) isascii(c)
287 #endif
289 #ifdef isblank
290 #define ISBLANK(c) (ISASCII (c) && isblank (c))
291 #else
292 #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
293 #endif
294 #ifdef isgraph
295 #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
296 #else
297 #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
298 #endif
300 #define ISPRINT(c) (ISASCII (c) && isprint (c))
301 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
302 #define ISALNUM(c) (ISASCII (c) && isalnum (c))
303 #define ISALPHA(c) (ISASCII (c) && isalpha (c))
304 #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
305 #define ISLOWER(c) (ISASCII (c) && islower (c))
306 #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
307 #define ISSPACE(c) (ISASCII (c) && isspace (c))
308 #define ISUPPER(c) (ISASCII (c) && isupper (c))
309 #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))