The base/count bounds checking was insufficient, leading to a kernel memory
[dragonfly.git] / contrib / patch / common.h
1 /* common definitions for `patch' */
2
3 /* $Id: common.h,v 1.18 1997/06/13 06:28:37 eggert Exp $ */
4
5 /*
6 Copyright 1986, 1988 Larry Wall
7 Copyright 1990, 1991, 1992, 1993, 1997 Free Software Foundation, Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING.
21 If not, write to the Free Software Foundation,
22 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #ifndef DEBUGGING
26 #define DEBUGGING 1
27 #endif
28
29 /* We must define `volatile' and `const' first (the latter inside config.h),
30    so that they're used consistently in all system includes.  */
31 #ifndef __STDC__
32 # ifndef volatile
33 # define volatile
34 # endif
35 #endif
36
37 /* Enable support for fseeko and ftello on hosts
38    where it is available but is turned off by default.
39    This must be defined before any system file is included.  */
40 #define _LARGEFILE_SOURCE 1
41
42 #include <config.h>
43
44 #include <assert.h>
45 #include <stdio.h>
46 #include <sys/types.h>
47 #include <time.h>
48
49 #include <sys/stat.h>
50 #if ! defined S_ISDIR && defined S_IFDIR
51 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
52 #endif
53 #if ! defined S_ISREG && defined S_IFREG
54 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
55 #endif
56 #ifndef S_IXOTH
57 #define S_IXOTH 1
58 #endif
59 #ifndef S_IWOTH
60 #define S_IWOTH 2
61 #endif
62 #ifndef S_IROTH
63 #define S_IROTH 4
64 #endif
65 #ifndef S_IXGRP
66 #define S_IXGRP (S_IXOTH << 3)
67 #endif
68 #ifndef S_IWGRP
69 #define S_IWGRP (S_IWOTH << 3)
70 #endif
71 #ifndef S_IRGRP
72 #define S_IRGRP (S_IROTH << 3)
73 #endif
74 #ifndef S_IXUSR
75 #define S_IXUSR (S_IXOTH << 6)
76 #endif
77 #ifndef S_IWUSR
78 #define S_IWUSR (S_IWOTH << 6)
79 #endif
80 #ifndef S_IRUSR
81 #define S_IRUSR (S_IROTH << 6)
82 #endif
83
84 #if HAVE_LIMITS_H
85 # include <limits.h>
86 #endif
87 #ifndef INT_MAX
88 #define INT_MAX 2147483647
89 #endif
90 #ifndef LONG_MIN
91 #define LONG_MIN (-1-2147483647L)
92 #endif
93
94 #include <ctype.h>
95 /* CTYPE_DOMAIN (C) is nonzero if the unsigned char C can safely be given
96    as an argument to <ctype.h> macros like `isspace'.  */
97 #if STDC_HEADERS
98 #define CTYPE_DOMAIN(c) 1
99 #else
100 #define CTYPE_DOMAIN(c) ((unsigned) (c) <= 0177)
101 #endif
102 #ifndef ISSPACE
103 #define ISSPACE(c) (CTYPE_DOMAIN (c) && isspace (c))
104 #endif
105
106 #ifndef ISDIGIT
107 #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
108 #endif
109
110
111 #ifndef FILESYSTEM_PREFIX_LEN
112 #define FILESYSTEM_PREFIX_LEN(f) 0
113 #endif
114
115 #ifndef ISSLASH
116 #define ISSLASH(c) ((c) == '/')
117 #endif
118
119
120 /* constants */
121
122 /* AIX predefines these.  */
123 #ifdef TRUE
124 #undef TRUE
125 #endif
126 #ifdef FALSE
127 #undef FALSE
128 #endif
129 #define TRUE 1
130 #define FALSE 0
131
132 /* handy definitions */
133
134 #define strEQ(s1,s2) (!strcmp(s1, s2))
135 #define strnEQ(s1,s2,l) (!strncmp(s1, s2, l))
136
137 /* typedefs */
138
139 typedef int bool;                       /* must promote to itself */
140 typedef long LINENUM;                   /* must be signed */
141
142 /* globals */
143
144 extern char const program_name[];
145
146 XTERN char *buf;                        /* general purpose buffer */
147 XTERN size_t bufsize;                   /* allocated size of buf */
148
149 XTERN bool using_plan_a;                /* try to keep everything in memory */
150
151 XTERN char *inname;
152 XTERN char *outfile;
153 XTERN int inerrno;
154 XTERN int invc;
155 XTERN struct stat instat;
156 XTERN bool dry_run;
157 XTERN bool posixly_correct;
158
159 XTERN char const *origprae;
160 XTERN char const *origbase;
161
162 XTERN char const * volatile TMPOUTNAME;
163 XTERN char const * volatile TMPINNAME;
164 XTERN char const * volatile TMPPATNAME;
165
166 #ifdef DEBUGGING
167 XTERN int debug;
168 #else
169 # define debug 0
170 #endif
171 XTERN bool force;
172 XTERN bool batch;
173 XTERN bool noreverse;
174 XTERN int reverse;
175 XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity;
176 XTERN bool skip_rest_of_patch;
177 XTERN int strippath;
178 XTERN bool canonicalize;
179 XTERN int patch_get;
180 XTERN int set_time;
181 XTERN int set_utc;
182
183 enum diff
184   {
185     NO_DIFF,
186     CONTEXT_DIFF,
187     NORMAL_DIFF,
188     ED_DIFF,
189     NEW_CONTEXT_DIFF,
190     UNI_DIFF
191   };
192
193 XTERN enum diff diff_type;
194
195 XTERN char *revision;                   /* prerequisite revision, if any */
196
197 #ifdef __STDC__
198 # define GENERIC_OBJECT void
199 #else
200 # define GENERIC_OBJECT char
201 #endif
202
203 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6) || __STRICT_ANSI__
204 # define __attribute__(x)
205 #endif
206
207 #ifndef PARAMS
208 # ifdef __STDC__
209 #  define PARAMS(args) args
210 # else
211 #  define PARAMS(args) ()
212 # endif
213 #endif
214
215 GENERIC_OBJECT *xmalloc PARAMS ((size_t));
216 void fatal_exit PARAMS ((int)) __attribute__ ((noreturn));
217
218 #include <errno.h>
219 #if !STDC_HEADERS && !defined errno
220 extern int errno;
221 #endif
222
223 #if STDC_HEADERS || HAVE_STRING_H
224 # include <string.h>
225 #else
226 # if !HAVE_MEMCHR
227 #  define memcmp(s1, s2, n) bcmp (s1, s2, n)
228 #  define memcpy(d, s, n) bcopy (s, d, n)
229 GENERIC_OBJECT *memchr ();
230 # endif
231 #endif
232
233 #if STDC_HEADERS
234 # include <stdlib.h>
235 #else
236 long atol ();
237 char *getenv ();
238 GENERIC_OBJECT *malloc ();
239 GENERIC_OBJECT *realloc ();
240 #endif
241
242 #if HAVE_UNISTD_H
243 # include <unistd.h>
244 #endif
245 #ifndef lseek
246 off_t lseek ();
247 #endif
248 #ifndef SEEK_SET
249 #define SEEK_SET 0
250 #endif
251 #ifndef STDIN_FILENO
252 #define STDIN_FILENO 0
253 #endif
254 #ifndef STDOUT_FILENO
255 #define STDOUT_FILENO 1
256 #endif
257 #ifndef STDERR_FILENO
258 #define STDERR_FILENO 2
259 #endif
260 #if _LFS_LARGEFILE
261   typedef off_t file_offset;
262 # define file_seek fseeko
263 # define file_tell ftello
264 #else
265   typedef long file_offset;
266 # define file_seek fseek
267 # define file_tell ftell
268 #endif
269
270 #if HAVE_FCNTL_H
271 # include <fcntl.h>
272 #endif
273 #ifndef O_RDONLY
274 #define O_RDONLY 0
275 #endif
276 #ifndef O_WRONLY
277 #define O_WRONLY 1
278 #endif
279 #ifndef O_RDWR
280 #define O_RDWR 2
281 #endif
282 #ifndef _O_BINARY
283 #define _O_BINARY 0
284 #endif
285 #ifndef O_BINARY
286 #define O_BINARY _O_BINARY
287 #endif
288 #ifndef O_CREAT
289 #define O_CREAT 0
290 #endif
291 #ifndef O_TRUNC
292 #define O_TRUNC 0
293 #endif
294
295 #if HAVE_SETMODE
296   XTERN int binary_transput;    /* O_BINARY if binary i/o is desired */
297 #else
298 # define binary_transput 0
299 #endif
300
301 #ifndef NULL_DEVICE
302 #define NULL_DEVICE "/dev/null"
303 #endif
304
305 #ifndef TTY_DEVICE
306 #define TTY_DEVICE "/dev/tty"
307 #endif