xref: /freebsd/contrib/diff/src/system.h (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1 /* System dependent declarations.
2 
3    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002,
4    2004 Free Software Foundation, Inc.
5 
6    This file is part of GNU DIFF.
7 
8    GNU DIFF is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12 
13    GNU DIFF is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; see the file COPYING.
20    If not, write to the Free Software Foundation,
21    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22 
23 #include <config.h>
24 
25 /* Don't bother to support K&R C compilers any more; it's not worth
26    the trouble.  These macros prevent some library modules from being
27    compiled in K&R C mode.  */
28 #define PARAMS(Args) Args
29 #define PROTOTYPES 1
30 
31 /* Define `__attribute__' and `volatile' first
32    so that they're used consistently in all system includes.  */
33 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
34 # define __attribute__(x)
35 #endif
36 #if defined const && !defined volatile
37 # define volatile
38 #endif
39 
40 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
41 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
42 
43 
44 /* Determine whether an integer type is signed, and its bounds.
45    This code assumes two's (or one's!) complement with no holes.  */
46 
47 /* The extra casts work around common compiler bugs,
48    e.g. Cray C 5.0.3.0 when t == time_t.  */
49 #ifndef TYPE_SIGNED
50 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
51 #endif
52 #ifndef TYPE_MINIMUM
53 # define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
54 			       ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
55 			       : (t) 0))
56 #endif
57 #ifndef TYPE_MAXIMUM
58 # define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
59 #endif
60 
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 
64 #ifndef S_IRWXU
65 # define S_IRWXU 0700
66 #endif
67 #ifndef S_IRWXG
68 # define S_IRWXG 0070
69 #endif
70 #ifndef S_IRWXO
71 # define S_IRWXO 0007
72 #endif
73 
74 #if HAVE_UNISTD_H
75 # include <unistd.h>
76 #endif
77 
78 #ifndef SEEK_SET
79 # define SEEK_SET 0
80 #endif
81 #ifndef SEEK_CUR
82 # define SEEK_CUR 1
83 #endif
84 
85 #ifndef STDIN_FILENO
86 # define STDIN_FILENO 0
87 #endif
88 #ifndef STDOUT_FILENO
89 # define STDOUT_FILENO 1
90 #endif
91 #ifndef STDERR_FILENO
92 # define STDERR_FILENO 2
93 #endif
94 
95 #include <time.h>
96 
97 #if HAVE_FCNTL_H
98 # include <fcntl.h>
99 #else
100 # if HAVE_SYS_FILE_H
101 #  include <sys/file.h>
102 # endif
103 #endif
104 
105 #if !HAVE_DUP2
106 # define dup2(f, t) (close (t), fcntl (f, F_DUPFD, t))
107 #endif
108 
109 #ifndef O_RDONLY
110 # define O_RDONLY 0
111 #endif
112 #ifndef O_RDWR
113 # define O_RDWR 2
114 #endif
115 #ifndef S_IRUSR
116 # define S_IRUSR 0400
117 #endif
118 #ifndef S_IWUSR
119 # define S_IWUSR 0200
120 #endif
121 
122 #if HAVE_SYS_WAIT_H
123 # include <sys/wait.h>
124 #endif
125 #ifndef WEXITSTATUS
126 # define WEXITSTATUS(stat_val) ((unsigned int) (stat_val) >> 8)
127 #endif
128 #ifndef WIFEXITED
129 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
130 #endif
131 
132 #ifndef STAT_BLOCKSIZE
133 # if HAVE_STRUCT_STAT_ST_BLKSIZE
134 #  define STAT_BLOCKSIZE(s) ((s).st_blksize)
135 # else
136 #  define STAT_BLOCKSIZE(s) (8 * 1024)
137 # endif
138 #endif
139 
140 #if HAVE_DIRENT_H
141 # include <dirent.h>
142 # define NAMLEN(dirent) strlen ((dirent)->d_name)
143 #else
144 # define dirent direct
145 # define NAMLEN(dirent) ((dirent)->d_namlen)
146 # if HAVE_SYS_NDIR_H
147 #  include <sys/ndir.h>
148 # endif
149 # if HAVE_SYS_DIR_H
150 #  include <sys/dir.h>
151 # endif
152 # if HAVE_NDIR_H
153 #  include <ndir.h>
154 # endif
155 #endif
156 
157 #include <stdlib.h>
158 #define EXIT_TROUBLE 2
159 
160 #include <limits.h>
161 
162 #if HAVE_INTTYPES_H
163 # include <inttypes.h>
164 #endif
165 #ifndef PTRDIFF_MAX
166 # define PTRDIFF_MAX TYPE_MAXIMUM (ptrdiff_t)
167 #endif
168 #ifndef SIZE_MAX
169 # define SIZE_MAX TYPE_MAXIMUM (size_t)
170 #endif
171 #ifndef UINTMAX_MAX
172 # define UINTMAX_MAX TYPE_MAXIMUM (uintmax_t)
173 #endif
174 #if ! HAVE_STRTOUMAX  && ! defined strtoumax
175 uintmax_t strtoumax (char const *, char **, int);
176 #endif
177 
178 #include <stddef.h>
179 
180 #include <string.h>
181 #if ! HAVE_STRCASECOLL
182 # if HAVE_STRICOLL || defined stricoll
183 #  define strcasecoll(a, b) stricoll (a, b)
184 # else
185 #  define strcasecoll(a, b) strcasecmp (a, b) /* best we can do */
186 # endif
187 #endif
188 #if ! (HAVE_STRCASECMP || defined strcasecmp)
189 int strcasecmp (char const *, char const *);
190 #endif
191 
192 #if HAVE_LOCALE_H
193 # include <locale.h>
194 #else
195 # define setlocale(category, locale)
196 #endif
197 
198 #include <gettext.h>
199 
200 #define _(msgid) gettext (msgid)
201 #define N_(msgid) msgid
202 
203 #include <ctype.h>
204 
205 /* ISDIGIT differs from isdigit, as follows:
206    - Its arg may be any int or unsigned int; it need not be an unsigned char.
207    - It's guaranteed to evaluate its argument exactly once.
208    - It's typically faster.
209    POSIX 1003.1-2001 says that only '0' through '9' are digits.
210    Prefer ISDIGIT to isdigit unless it's important to use the locale's
211    definition of `digit' even when the host does not conform to POSIX.  */
212 #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
213 
214 #include <errno.h>
215 
216 #include <signal.h>
217 #ifndef SA_RESTART
218 # ifdef SA_INTERRUPT /* e.g. SunOS 4.1.x */
219 #  define SA_RESTART SA_INTERRUPT
220 # else
221 #  define SA_RESTART 0
222 # endif
223 #endif
224 #if !defined SIGCHLD && defined SIGCLD
225 # define SIGCHLD SIGCLD
226 #endif
227 
228 #undef MIN
229 #undef MAX
230 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
231 #define MAX(a, b) ((a) >= (b) ? (a) : (b))
232 
233 #include <stdbool.h>
234 
235 #if HAVE_VFORK_H
236 # include <vfork.h>
237 #endif
238 
239 #if ! HAVE_WORKING_VFORK
240 # define vfork fork
241 #endif
242 
243 /* Type used for fast comparison of several bytes at a time.  */
244 
245 #ifndef word
246 # define word uintmax_t
247 #endif
248 
249 /* The integer type of a line number.  Since files are read into main
250    memory, ptrdiff_t should be wide enough.  */
251 
252 typedef ptrdiff_t lin;
253 #define LIN_MAX PTRDIFF_MAX
254 verify (lin_is_signed, TYPE_SIGNED (lin));
255 verify (lin_is_wide_enough, sizeof (ptrdiff_t) <= sizeof (lin));
256 verify (lin_is_printable_as_long_int, sizeof (lin) <= sizeof (long int));
257 
258 /* This section contains POSIX-compliant defaults for macros
259    that are meant to be overridden by hand in config.h as needed.  */
260 
261 #ifndef file_name_cmp
262 # define file_name_cmp strcmp
263 #endif
264 
265 #ifndef initialize_main
266 # define initialize_main(argcp, argvp)
267 #endif
268 
269 #ifndef NULL_DEVICE
270 # define NULL_DEVICE "/dev/null"
271 #endif
272 
273 /* Do struct stat *S, *T describe the same special file?  */
274 #ifndef same_special_file
275 # if HAVE_ST_RDEV && defined S_ISBLK && defined S_ISCHR
276 #  define same_special_file(s, t) \
277      (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
278        || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
279       && (s)->st_rdev == (t)->st_rdev)
280 # else
281 #  define same_special_file(s, t) 0
282 # endif
283 #endif
284 
285 /* Do struct stat *S, *T describe the same file?  Answer -1 if unknown.  */
286 #ifndef same_file
287 # define same_file(s, t) \
288     ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
289      || same_special_file (s, t))
290 #endif
291 
292 /* Do struct stat *S, *T have the same file attributes?
293 
294    POSIX says that two files are identical if st_ino and st_dev are
295    the same, but many filesystems incorrectly assign the same (device,
296    inode) pair to two distinct files, including:
297 
298    - GNU/Linux NFS servers that export all local filesystems as a
299      single NFS filesystem, if a local device number (st_dev) exceeds
300      255, or if a local inode number (st_ino) exceeds 16777215.
301 
302    - Network Appliance NFS servers in snapshot directories; see
303      Network Appliance bug #195.
304 
305    - ClearCase MVFS; see bug id ATRia04618.
306 
307    Check whether two files that purport to be the same have the same
308    attributes, to work around instances of this common bug.  Do not
309    inspect all attributes, only attributes useful in checking for this
310    bug.
311 
312    It's possible for two distinct files on a buggy filesystem to have
313    the same attributes, but it's not worth slowing down all
314    implementations (or complicating the configuration) to cater to
315    these rare cases in buggy implementations.  */
316 
317 #ifndef same_file_attributes
318 # define same_file_attributes(s, t) \
319    ((s)->st_mode == (t)->st_mode \
320     && (s)->st_nlink == (t)->st_nlink \
321     && (s)->st_uid == (t)->st_uid \
322     && (s)->st_gid == (t)->st_gid \
323     && (s)->st_size == (t)->st_size \
324     && (s)->st_mtime == (t)->st_mtime \
325     && (s)->st_ctime == (t)->st_ctime)
326 #endif
327