xref: /dragonfly/gnu/usr.bin/diff/diff/system.h (revision 5b8cd87c)
1d1248869SJohn Marino /* System dependent declarations.
2d1248869SJohn Marino 
3*5b8cd87cSDaniel Fojt    Copyright (C) 1988-1989, 1992-1995, 1998, 2001-2002, 2004, 2006, 2009-2013,
4*5b8cd87cSDaniel Fojt    2015-2018 Free Software Foundation, Inc.
5d1248869SJohn Marino 
6d1248869SJohn Marino    This file is part of GNU DIFF.
7d1248869SJohn Marino 
8d1248869SJohn Marino    This program is free software: you can redistribute it and/or modify
9d1248869SJohn Marino    it under the terms of the GNU General Public License as published by
10d1248869SJohn Marino    the Free Software Foundation, either version 3 of the License, or
11d1248869SJohn Marino    (at your option) any later version.
12d1248869SJohn Marino 
13d1248869SJohn Marino    This program is distributed in the hope that it will be useful,
14d1248869SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
15d1248869SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16d1248869SJohn Marino    GNU General Public License for more details.
17d1248869SJohn Marino 
18d1248869SJohn Marino    You should have received a copy of the GNU General Public License
19d1248869SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20d1248869SJohn Marino 
21d1248869SJohn Marino #include <config.h>
22d1248869SJohn Marino 
23*5b8cd87cSDaniel Fojt /* Use this to suppress gcc's "...may be used before initialized" warnings. */
24d1248869SJohn Marino #ifdef lint
25d1248869SJohn Marino # define IF_LINT(Code) Code
26d1248869SJohn Marino #else
27d1248869SJohn Marino # define IF_LINT(Code) /* empty */
28d1248869SJohn Marino #endif
29d1248869SJohn Marino 
30*5b8cd87cSDaniel Fojt /* Define '__attribute__' and 'volatile' first
31d1248869SJohn Marino    so that they're used consistently in all system includes.  */
32d1248869SJohn Marino #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
33d1248869SJohn Marino # define __attribute__(x)
34d1248869SJohn Marino #endif
35d1248869SJohn Marino 
36d1248869SJohn Marino #include <verify.h>
37d1248869SJohn Marino 
38d1248869SJohn Marino #include <sys/types.h>
39d1248869SJohn Marino 
40d1248869SJohn Marino #include <sys/stat.h>
41d1248869SJohn Marino #include "stat-macros.h"
42d1248869SJohn Marino 
43d1248869SJohn Marino #ifndef STAT_BLOCKSIZE
44d1248869SJohn Marino # if HAVE_STRUCT_STAT_ST_BLKSIZE
45d1248869SJohn Marino #  define STAT_BLOCKSIZE(s) ((s).st_blksize)
46d1248869SJohn Marino # else
47d1248869SJohn Marino #  define STAT_BLOCKSIZE(s) (8 * 1024)
48d1248869SJohn Marino # endif
49d1248869SJohn Marino #endif
50d1248869SJohn Marino 
51d1248869SJohn Marino #include <unistd.h>
52d1248869SJohn Marino 
53d1248869SJohn Marino #include <fcntl.h>
54d1248869SJohn Marino #include <time.h>
55d1248869SJohn Marino 
56d1248869SJohn Marino #include <sys/wait.h>
57d1248869SJohn Marino 
58d1248869SJohn Marino #include <dirent.h>
59d1248869SJohn Marino #ifndef _D_EXACT_NAMLEN
60d1248869SJohn Marino # define _D_EXACT_NAMLEN(dp) strlen ((dp)->d_name)
61d1248869SJohn Marino #endif
62d1248869SJohn Marino 
63d1248869SJohn Marino #include <stdlib.h>
64d1248869SJohn Marino #define EXIT_TROUBLE 2
65d1248869SJohn Marino 
66d1248869SJohn Marino #include <limits.h>
67d1248869SJohn Marino #include <locale.h>
68d1248869SJohn Marino #include <stddef.h>
69d1248869SJohn Marino #include <inttypes.h>
70d1248869SJohn Marino 
71d1248869SJohn Marino #include <string.h>
72d1248869SJohn Marino #if ! HAVE_STRCASECOLL
73d1248869SJohn Marino # if HAVE_STRICOLL || defined stricoll
74d1248869SJohn Marino #  define strcasecoll(a, b) stricoll (a, b)
75d1248869SJohn Marino # else
76d1248869SJohn Marino #  define strcasecoll(a, b) strcasecmp (a, b) /* best we can do */
77d1248869SJohn Marino # endif
78d1248869SJohn Marino #endif
79d1248869SJohn Marino #if ! (HAVE_STRCASECMP || defined strcasecmp)
80d1248869SJohn Marino int strcasecmp (char const *, char const *);
81d1248869SJohn Marino #endif
82d1248869SJohn Marino 
83d1248869SJohn Marino #include <gettext.h>
84d1248869SJohn Marino #if ! ENABLE_NLS
85d1248869SJohn Marino # undef textdomain
86d1248869SJohn Marino # define textdomain(Domainname) /* empty */
87d1248869SJohn Marino # undef bindtextdomain
88d1248869SJohn Marino # define bindtextdomain(Domainname, Dirname) /* empty */
89d1248869SJohn Marino #endif
90d1248869SJohn Marino 
91d1248869SJohn Marino #define _(msgid) gettext (msgid)
92d1248869SJohn Marino #define N_(msgid) msgid
93d1248869SJohn Marino 
94d1248869SJohn Marino #include <ctype.h>
95d1248869SJohn Marino 
96d1248869SJohn Marino /* ISDIGIT differs from isdigit, as follows:
97d1248869SJohn Marino    - Its arg may be any int or unsigned int; it need not be an unsigned char.
98d1248869SJohn Marino    - It's guaranteed to evaluate its argument exactly once.
99d1248869SJohn Marino    - It's typically faster.
100d1248869SJohn Marino    POSIX 1003.1-2001 says that only '0' through '9' are digits.
101d1248869SJohn Marino    Prefer ISDIGIT to isdigit unless it's important to use the locale's
102*5b8cd87cSDaniel Fojt    definition of 'digit' even when the host does not conform to POSIX.  */
103d1248869SJohn Marino #define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
104d1248869SJohn Marino 
105d1248869SJohn Marino #include <errno.h>
106d1248869SJohn Marino 
107d1248869SJohn Marino #include <signal.h>
108d1248869SJohn Marino #if !defined SIGCHLD && defined SIGCLD
109d1248869SJohn Marino # define SIGCHLD SIGCLD
110d1248869SJohn Marino #endif
111d1248869SJohn Marino 
112d1248869SJohn Marino #undef MIN
113d1248869SJohn Marino #undef MAX
114d1248869SJohn Marino #define MIN(a, b) ((a) <= (b) ? (a) : (b))
115d1248869SJohn Marino #define MAX(a, b) ((a) >= (b) ? (a) : (b))
116d1248869SJohn Marino 
117d1248869SJohn Marino #include <stdbool.h>
118d1248869SJohn Marino #include <intprops.h>
119d1248869SJohn Marino #include "propername.h"
120d1248869SJohn Marino #include "version.h"
121d1248869SJohn Marino 
122*5b8cd87cSDaniel Fojt /* Type used for fast comparison of several bytes at a time.
123*5b8cd87cSDaniel Fojt    This used to be uintmax_t, but changing it to size_t
124*5b8cd87cSDaniel Fojt    made plain 'cmp' 90% faster (GCC 4.8.1, x86).  */
125d1248869SJohn Marino 
126d1248869SJohn Marino #ifndef word
127*5b8cd87cSDaniel Fojt # define word size_t
128d1248869SJohn Marino #endif
129d1248869SJohn Marino 
130*5b8cd87cSDaniel Fojt /* The signed integer type of a line number.  Since files are read
131*5b8cd87cSDaniel Fojt    into main memory, ptrdiff_t should be wide enough.  */
132d1248869SJohn Marino 
133d1248869SJohn Marino typedef ptrdiff_t lin;
134d1248869SJohn Marino #define LIN_MAX PTRDIFF_MAX
135*5b8cd87cSDaniel Fojt 
136*5b8cd87cSDaniel Fojt /* The signed integer type for printing line numbers, and its printf
137*5b8cd87cSDaniel Fojt    length modifier.  This is not simply ptrdiff_t, to cater to older
138*5b8cd87cSDaniel Fojt    and/or nonstandard C libraries where "l" works but "ll" and "t" do
139*5b8cd87cSDaniel Fojt    not, or where 'long' is too narrow and "ll" works but "t" does not.  */
140*5b8cd87cSDaniel Fojt 
141*5b8cd87cSDaniel Fojt #if LIN_MAX <= LONG_MAX
142*5b8cd87cSDaniel Fojt typedef long int printint;
143*5b8cd87cSDaniel Fojt # define pI "l"
144*5b8cd87cSDaniel Fojt #elif LIN_MAX <= LLONG_MAX
145*5b8cd87cSDaniel Fojt typedef long long int printint;
146*5b8cd87cSDaniel Fojt # define pI "ll"
147*5b8cd87cSDaniel Fojt #else
148*5b8cd87cSDaniel Fojt typedef ptrdiff_t printint;
149*5b8cd87cSDaniel Fojt # define pI "t"
150*5b8cd87cSDaniel Fojt #endif
151*5b8cd87cSDaniel Fojt 
152d1248869SJohn Marino verify (TYPE_SIGNED (lin));
153*5b8cd87cSDaniel Fojt verify (TYPE_SIGNED (printint));
154*5b8cd87cSDaniel Fojt verify (LIN_MAX == TYPE_MAXIMUM (lin));
155*5b8cd87cSDaniel Fojt verify (LIN_MAX <= TYPE_MAXIMUM (printint));
156*5b8cd87cSDaniel Fojt 
157*5b8cd87cSDaniel Fojt /* Limit so that 2 * CONTEXT + 1 does not overflow.  */
158*5b8cd87cSDaniel Fojt 
159*5b8cd87cSDaniel Fojt #define CONTEXT_MAX ((LIN_MAX - 1) / 2)
160*5b8cd87cSDaniel Fojt 
161d1248869SJohn Marino 
162d1248869SJohn Marino /* This section contains POSIX-compliant defaults for macros
163d1248869SJohn Marino    that are meant to be overridden by hand in config.h as needed.  */
164d1248869SJohn Marino 
165d1248869SJohn Marino #ifndef file_name_cmp
166d1248869SJohn Marino # define file_name_cmp strcmp
167d1248869SJohn Marino #endif
168d1248869SJohn Marino 
169d1248869SJohn Marino #ifndef initialize_main
170d1248869SJohn Marino # define initialize_main(argcp, argvp)
171d1248869SJohn Marino #endif
172d1248869SJohn Marino 
173d1248869SJohn Marino #ifndef NULL_DEVICE
174d1248869SJohn Marino # define NULL_DEVICE "/dev/null"
175d1248869SJohn Marino #endif
176d1248869SJohn Marino 
177d1248869SJohn Marino /* Do struct stat *S, *T describe the same special file?  */
178d1248869SJohn Marino #ifndef same_special_file
179*5b8cd87cSDaniel Fojt # if HAVE_STRUCT_STAT_ST_RDEV && defined S_ISBLK && defined S_ISCHR
180d1248869SJohn Marino #  define same_special_file(s, t) \
181d1248869SJohn Marino      (((S_ISBLK ((s)->st_mode) && S_ISBLK ((t)->st_mode)) \
182d1248869SJohn Marino        || (S_ISCHR ((s)->st_mode) && S_ISCHR ((t)->st_mode))) \
183d1248869SJohn Marino       && (s)->st_rdev == (t)->st_rdev)
184d1248869SJohn Marino # else
185d1248869SJohn Marino #  define same_special_file(s, t) 0
186d1248869SJohn Marino # endif
187d1248869SJohn Marino #endif
188d1248869SJohn Marino 
189d1248869SJohn Marino /* Do struct stat *S, *T describe the same file?  Answer -1 if unknown.  */
190d1248869SJohn Marino #ifndef same_file
191d1248869SJohn Marino # define same_file(s, t) \
192d1248869SJohn Marino     ((((s)->st_ino == (t)->st_ino) && ((s)->st_dev == (t)->st_dev)) \
193d1248869SJohn Marino      || same_special_file (s, t))
194d1248869SJohn Marino #endif
195d1248869SJohn Marino 
196d1248869SJohn Marino /* Do struct stat *S, *T have the same file attributes?
197d1248869SJohn Marino 
198d1248869SJohn Marino    POSIX says that two files are identical if st_ino and st_dev are
199d1248869SJohn Marino    the same, but many file systems incorrectly assign the same (device,
200d1248869SJohn Marino    inode) pair to two distinct files, including:
201d1248869SJohn Marino 
202d1248869SJohn Marino    - GNU/Linux NFS servers that export all local file systems as a
203d1248869SJohn Marino      single NFS file system, if a local device number (st_dev) exceeds
204d1248869SJohn Marino      255, or if a local inode number (st_ino) exceeds 16777215.
205d1248869SJohn Marino 
206d1248869SJohn Marino    - Network Appliance NFS servers in snapshot directories; see
207d1248869SJohn Marino      Network Appliance bug #195.
208d1248869SJohn Marino 
209d1248869SJohn Marino    - ClearCase MVFS; see bug id ATRia04618.
210d1248869SJohn Marino 
211d1248869SJohn Marino    Check whether two files that purport to be the same have the same
212d1248869SJohn Marino    attributes, to work around instances of this common bug.  Do not
213d1248869SJohn Marino    inspect all attributes, only attributes useful in checking for this
214d1248869SJohn Marino    bug.
215d1248869SJohn Marino 
216d1248869SJohn Marino    It's possible for two distinct files on a buggy file system to have
217d1248869SJohn Marino    the same attributes, but it's not worth slowing down all
218d1248869SJohn Marino    implementations (or complicating the configuration) to cater to
219d1248869SJohn Marino    these rare cases in buggy implementations.  */
220d1248869SJohn Marino 
221d1248869SJohn Marino #ifndef same_file_attributes
222d1248869SJohn Marino # define same_file_attributes(s, t) \
223d1248869SJohn Marino    ((s)->st_mode == (t)->st_mode \
224d1248869SJohn Marino     && (s)->st_nlink == (t)->st_nlink \
225d1248869SJohn Marino     && (s)->st_uid == (t)->st_uid \
226d1248869SJohn Marino     && (s)->st_gid == (t)->st_gid \
227d1248869SJohn Marino     && (s)->st_size == (t)->st_size \
228d1248869SJohn Marino     && (s)->st_mtime == (t)->st_mtime \
229d1248869SJohn Marino     && (s)->st_ctime == (t)->st_ctime)
230d1248869SJohn Marino #endif
231d1248869SJohn Marino 
232d1248869SJohn Marino #define STREQ(a, b) (strcmp (a, b) == 0)
233*5b8cd87cSDaniel Fojt 
234*5b8cd87cSDaniel Fojt #ifndef FALLTHROUGH
235*5b8cd87cSDaniel Fojt # if __GNUC__ < 7
236*5b8cd87cSDaniel Fojt #  define FALLTHROUGH ((void) 0)
237*5b8cd87cSDaniel Fojt # else
238*5b8cd87cSDaniel Fojt #  define FALLTHROUGH __attribute__ ((__fallthrough__))
239*5b8cd87cSDaniel Fojt # endif
240*5b8cd87cSDaniel Fojt #endif
241