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