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