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