| 1 | /* patch - a program to apply diffs to original files */ | 
|---|
| 2 |  | 
|---|
| 3 | /* $Id: patch.c,v 1.44 2003/05/20 13:55:03 eggert Exp $ */ | 
|---|
| 4 |  | 
|---|
| 5 | /* Copyright (C) 1984, 1985, 1986, 1987, 1988 Larry Wall | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 1997, 1998, 1999, 2002, | 
|---|
| 8 | 2003 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.  See the | 
|---|
| 18 | 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 | #define XTERN | 
|---|
| 26 | #include <common.h> | 
|---|
| 27 | #undef XTERN | 
|---|
| 28 | #define XTERN extern | 
|---|
| 29 | #include <argmatch.h> | 
|---|
| 30 | #include <backupfile.h> | 
|---|
| 31 | #include <getopt.h> | 
|---|
| 32 | #include <inp.h> | 
|---|
| 33 | #include <pch.h> | 
|---|
| 34 | #include <quotearg.h> | 
|---|
| 35 | #include <util.h> | 
|---|
| 36 | #include <version.h> | 
|---|
| 37 | #include <xalloc.h> | 
|---|
| 38 |  | 
|---|
| 39 | #if HAVE_UTIME_H | 
|---|
| 40 | # include <utime.h> | 
|---|
| 41 | #endif | 
|---|
| 42 | /* Some nonstandard hosts don't declare this structure even in <utime.h>.  */ | 
|---|
| 43 | #if ! HAVE_STRUCT_UTIMBUF | 
|---|
| 44 | struct utimbuf | 
|---|
| 45 | { | 
|---|
| 46 | time_t actime; | 
|---|
| 47 | time_t modtime; | 
|---|
| 48 | }; | 
|---|
| 49 | #endif | 
|---|
| 50 |  | 
|---|
| 51 | /* Output stream state.  */ | 
|---|
| 52 | struct outstate | 
|---|
| 53 | { | 
|---|
| 54 | FILE *ofp; | 
|---|
| 55 | bool after_newline; | 
|---|
| 56 | bool zero_output; | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | /* procedures */ | 
|---|
| 60 |  | 
|---|
| 61 | static FILE *create_output_file (char const *, int); | 
|---|
| 62 | static LINENUM locate_hunk (LINENUM); | 
|---|
| 63 | static bool apply_hunk (struct outstate *, LINENUM); | 
|---|
| 64 | static bool copy_till (struct outstate *, LINENUM); | 
|---|
| 65 | static bool patch_match (LINENUM, LINENUM, LINENUM, LINENUM); | 
|---|
| 66 | static bool similar (char const *, size_t, char const *, size_t); | 
|---|
| 67 | static bool spew_output (struct outstate *); | 
|---|
| 68 | static char const *make_temp (char); | 
|---|
| 69 | static int numeric_string (char const *, bool, char const *); | 
|---|
| 70 | static void abort_hunk (void); | 
|---|
| 71 | static void cleanup (void); | 
|---|
| 72 | static void get_some_switches (void); | 
|---|
| 73 | static void init_output (char const *, int, struct outstate *); | 
|---|
| 74 | static void init_reject (void); | 
|---|
| 75 | static void reinitialize_almost_everything (void); | 
|---|
| 76 | static void remove_if_needed (char const *, int volatile *); | 
|---|
| 77 | static void usage (FILE *, int) __attribute__((noreturn)); | 
|---|
| 78 |  | 
|---|
| 79 | static bool make_backups; | 
|---|
| 80 | static bool backup_if_mismatch; | 
|---|
| 81 | static char const *version_control; | 
|---|
| 82 | static char const *version_control_context; | 
|---|
| 83 | static bool remove_empty_files; | 
|---|
| 84 |  | 
|---|
| 85 | /* true if -R was specified on command line.  */ | 
|---|
| 86 | static bool reverse_flag_specified; | 
|---|
| 87 |  | 
|---|
| 88 | /* how many input lines have been irretractably output */ | 
|---|
| 89 | static LINENUM last_frozen_line; | 
|---|
| 90 |  | 
|---|
| 91 | static char const *do_defines; /* symbol to patch using ifdef, ifndef, etc. */ | 
|---|
| 92 | static char const if_defined[] = "\n#ifdef %s\n"; | 
|---|
| 93 | static char const not_defined[] = "\n#ifndef %s\n"; | 
|---|
| 94 | static char const else_defined[] = "\n#else\n"; | 
|---|
| 95 | static char const end_defined[] = "\n#endif\n"; | 
|---|
| 96 |  | 
|---|
| 97 | static int Argc; | 
|---|
| 98 | static char * const *Argv; | 
|---|
| 99 |  | 
|---|
| 100 | static FILE *rejfp;  /* reject file pointer */ | 
|---|
| 101 |  | 
|---|
| 102 | static char const *patchname; | 
|---|
| 103 | static char *rejname; | 
|---|
| 104 | static char const * volatile TMPREJNAME; | 
|---|
| 105 | static int volatile TMPREJNAME_needs_removal; | 
|---|
| 106 |  | 
|---|
| 107 | static LINENUM last_offset; | 
|---|
| 108 | static LINENUM maxfuzz = 2; | 
|---|
| 109 |  | 
|---|
| 110 | static char serrbuf[BUFSIZ]; | 
|---|
| 111 |  | 
|---|
| 112 | /* Apply a set of diffs as appropriate. */ | 
|---|
| 113 |  | 
|---|
| 114 | int | 
|---|
| 115 | main (int argc, char **argv) | 
|---|
| 116 | { | 
|---|
| 117 | char const *val; | 
|---|
| 118 | bool somefailed = false; | 
|---|
| 119 | struct outstate outstate; | 
|---|
| 120 | char numbuf[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 121 |  | 
|---|
| 122 | xalloc_exit_failure = 2; | 
|---|
| 123 | program_name = argv[0]; | 
|---|
| 124 | init_time (); | 
|---|
| 125 |  | 
|---|
| 126 | setbuf(stderr, serrbuf); | 
|---|
| 127 |  | 
|---|
| 128 | xalloc_fail_func = memory_fatal; | 
|---|
| 129 | bufsize = 8 * 1024; | 
|---|
| 130 | buf = xmalloc (bufsize); | 
|---|
| 131 |  | 
|---|
| 132 | strippath = -1; | 
|---|
| 133 |  | 
|---|
| 134 | val = getenv ("QUOTING_STYLE"); | 
|---|
| 135 | { | 
|---|
| 136 | int i = val ? argmatch (val, quoting_style_args, 0, 0) : -1; | 
|---|
| 137 | set_quoting_style ((struct quoting_options *) 0, | 
|---|
| 138 | i < 0 ? shell_quoting_style : (enum quoting_style) i); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | posixly_correct = getenv ("POSIXLY_CORRECT") != 0; | 
|---|
| 142 | backup_if_mismatch = ! posixly_correct; | 
|---|
| 143 | patch_get = ((val = getenv ("PATCH_GET")) | 
|---|
| 144 | ? numeric_string (val, true, "PATCH_GET value") | 
|---|
| 145 | : posixly_correct - 1); | 
|---|
| 146 |  | 
|---|
| 147 | val = getenv ("SIMPLE_BACKUP_SUFFIX"); | 
|---|
| 148 | simple_backup_suffix = val && *val ? val : ".orig"; | 
|---|
| 149 |  | 
|---|
| 150 | if ((version_control = getenv ("PATCH_VERSION_CONTROL"))) | 
|---|
| 151 | version_control_context = "$PATCH_VERSION_CONTROL"; | 
|---|
| 152 | else if ((version_control = getenv ("VERSION_CONTROL"))) | 
|---|
| 153 | version_control_context = "$VERSION_CONTROL"; | 
|---|
| 154 |  | 
|---|
| 155 | /* Cons up the names of the global temporary files. | 
|---|
| 156 | Do this before `cleanup' can possibly be called (e.g. by `pfatal').  */ | 
|---|
| 157 | TMPOUTNAME = make_temp ('o'); | 
|---|
| 158 | TMPINNAME = make_temp ('i'); | 
|---|
| 159 | TMPREJNAME = make_temp ('r'); | 
|---|
| 160 | TMPPATNAME = make_temp ('p'); | 
|---|
| 161 |  | 
|---|
| 162 | /* parse switches */ | 
|---|
| 163 | Argc = argc; | 
|---|
| 164 | Argv = argv; | 
|---|
| 165 | get_some_switches(); | 
|---|
| 166 |  | 
|---|
| 167 | if (make_backups | backup_if_mismatch) | 
|---|
| 168 | backup_type = get_version (version_control_context, version_control); | 
|---|
| 169 |  | 
|---|
| 170 | init_output (outfile, 0, &outstate); | 
|---|
| 171 |  | 
|---|
| 172 | /* Make sure we clean up in case of disaster.  */ | 
|---|
| 173 | set_signals (false); | 
|---|
| 174 |  | 
|---|
| 175 | for ( | 
|---|
| 176 | open_patch_file (patchname); | 
|---|
| 177 | there_is_another_patch(); | 
|---|
| 178 | reinitialize_almost_everything() | 
|---|
| 179 | ) {                                 /* for each patch in patch file */ | 
|---|
| 180 | int hunk = 0; | 
|---|
| 181 | int failed = 0; | 
|---|
| 182 | bool mismatch = false; | 
|---|
| 183 | char *outname = outfile ? outfile : inname; | 
|---|
| 184 |  | 
|---|
| 185 | if (!skip_rest_of_patch) | 
|---|
| 186 | get_input_file (inname, outname); | 
|---|
| 187 |  | 
|---|
| 188 | if (diff_type == ED_DIFF) { | 
|---|
| 189 | outstate.zero_output = false; | 
|---|
| 190 | somefailed |= skip_rest_of_patch; | 
|---|
| 191 | do_ed_script (outstate.ofp); | 
|---|
| 192 | if (! dry_run && ! outfile && ! skip_rest_of_patch) | 
|---|
| 193 | { | 
|---|
| 194 | struct stat statbuf; | 
|---|
| 195 | if (stat (TMPOUTNAME, &statbuf) != 0) | 
|---|
| 196 | pfatal ("%s", TMPOUTNAME); | 
|---|
| 197 | outstate.zero_output = statbuf.st_size == 0; | 
|---|
| 198 | } | 
|---|
| 199 | } else { | 
|---|
| 200 | int got_hunk; | 
|---|
| 201 | bool apply_anyway = false; | 
|---|
| 202 |  | 
|---|
| 203 | /* initialize the patched file */ | 
|---|
| 204 | if (! skip_rest_of_patch && ! outfile) | 
|---|
| 205 | { | 
|---|
| 206 | int exclusive = TMPOUTNAME_needs_removal ? 0 : O_EXCL; | 
|---|
| 207 | TMPOUTNAME_needs_removal = 1; | 
|---|
| 208 | init_output (TMPOUTNAME, exclusive, &outstate); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /* initialize reject file */ | 
|---|
| 212 | init_reject (); | 
|---|
| 213 |  | 
|---|
| 214 | /* find out where all the lines are */ | 
|---|
| 215 | if (!skip_rest_of_patch) | 
|---|
| 216 | scan_input (inname); | 
|---|
| 217 |  | 
|---|
| 218 | /* from here on, open no standard i/o files, because malloc */ | 
|---|
| 219 | /* might misfire and we can't catch it easily */ | 
|---|
| 220 |  | 
|---|
| 221 | /* apply each hunk of patch */ | 
|---|
| 222 | while (0 < (got_hunk = another_hunk (diff_type, reverse))) { | 
|---|
| 223 | LINENUM where = 0; /* Pacify `gcc -Wall'.  */ | 
|---|
| 224 | LINENUM newwhere; | 
|---|
| 225 | LINENUM fuzz = 0; | 
|---|
| 226 | LINENUM prefix_context = pch_prefix_context (); | 
|---|
| 227 | LINENUM suffix_context = pch_suffix_context (); | 
|---|
| 228 | LINENUM context = (prefix_context < suffix_context | 
|---|
| 229 | ? suffix_context : prefix_context); | 
|---|
| 230 | LINENUM mymaxfuzz = (maxfuzz < context ? maxfuzz : context); | 
|---|
| 231 | hunk++; | 
|---|
| 232 | if (!skip_rest_of_patch) { | 
|---|
| 233 | do { | 
|---|
| 234 | where = locate_hunk(fuzz); | 
|---|
| 235 | if (! where || fuzz || last_offset) | 
|---|
| 236 | mismatch = true; | 
|---|
| 237 | if (hunk == 1 && ! where && ! (force | apply_anyway) | 
|---|
| 238 | && reverse == reverse_flag_specified) { | 
|---|
| 239 | /* dwim for reversed patch? */ | 
|---|
| 240 | if (!pch_swap()) { | 
|---|
| 241 | say ( | 
|---|
| 242 | "Not enough memory to try swapped hunk!  Assuming unswapped.\n"); | 
|---|
| 243 | continue; | 
|---|
| 244 | } | 
|---|
| 245 | /* Try again.  */ | 
|---|
| 246 | where = locate_hunk (fuzz); | 
|---|
| 247 | if (where | 
|---|
| 248 | && (ok_to_reverse | 
|---|
| 249 | ("%s patch detected!", | 
|---|
| 250 | (reverse | 
|---|
| 251 | ? "Unreversed" | 
|---|
| 252 | : "Reversed (or previously applied)")))) | 
|---|
| 253 | reverse = ! reverse; | 
|---|
| 254 | else | 
|---|
| 255 | { | 
|---|
| 256 | /* Put it back to normal.  */ | 
|---|
| 257 | if (! pch_swap ()) | 
|---|
| 258 | fatal ("lost hunk on alloc error!"); | 
|---|
| 259 | if (where) | 
|---|
| 260 | { | 
|---|
| 261 | apply_anyway = true; | 
|---|
| 262 | fuzz--; /* Undo `++fuzz' below.  */ | 
|---|
| 263 | where = 0; | 
|---|
| 264 | } | 
|---|
| 265 | } | 
|---|
| 266 | } | 
|---|
| 267 | } while (!skip_rest_of_patch && !where | 
|---|
| 268 | && ++fuzz <= mymaxfuzz); | 
|---|
| 269 |  | 
|---|
| 270 | if (skip_rest_of_patch) {               /* just got decided */ | 
|---|
| 271 | if (outstate.ofp && ! outfile) | 
|---|
| 272 | { | 
|---|
| 273 | fclose (outstate.ofp); | 
|---|
| 274 | outstate.ofp = 0; | 
|---|
| 275 | } | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | newwhere = pch_newfirst() + last_offset; | 
|---|
| 280 | if (skip_rest_of_patch) { | 
|---|
| 281 | abort_hunk(); | 
|---|
| 282 | failed++; | 
|---|
| 283 | if (verbosity == VERBOSE) | 
|---|
| 284 | say ("Hunk #%d ignored at %s.\n", hunk, | 
|---|
| 285 | format_linenum (numbuf, newwhere)); | 
|---|
| 286 | } | 
|---|
| 287 | else if (!where | 
|---|
| 288 | || (where == 1 && pch_says_nonexistent (reverse) == 2 | 
|---|
| 289 | && instat.st_size)) { | 
|---|
| 290 |  | 
|---|
| 291 | if (where) | 
|---|
| 292 | say ("Patch attempted to create file %s, which already exists.\n", | 
|---|
| 293 | quotearg (inname)); | 
|---|
| 294 |  | 
|---|
| 295 | abort_hunk(); | 
|---|
| 296 | failed++; | 
|---|
| 297 | if (verbosity != SILENT) | 
|---|
| 298 | say ("Hunk #%d FAILED at %s.\n", hunk, | 
|---|
| 299 | format_linenum (numbuf, newwhere)); | 
|---|
| 300 | } | 
|---|
| 301 | else if (! apply_hunk (&outstate, where)) { | 
|---|
| 302 | abort_hunk (); | 
|---|
| 303 | failed++; | 
|---|
| 304 | if (verbosity != SILENT) | 
|---|
| 305 | say ("Hunk #%d FAILED at %s.\n", hunk, | 
|---|
| 306 | format_linenum (numbuf, newwhere)); | 
|---|
| 307 | } else { | 
|---|
| 308 | if (verbosity == VERBOSE | 
|---|
| 309 | || (verbosity != SILENT && (fuzz || last_offset))) { | 
|---|
| 310 | say ("Hunk #%d succeeded at %s", hunk, | 
|---|
| 311 | format_linenum (numbuf, newwhere)); | 
|---|
| 312 | if (fuzz) | 
|---|
| 313 | say (" with fuzz %s", format_linenum (numbuf, fuzz)); | 
|---|
| 314 | if (last_offset) | 
|---|
| 315 | say (" (offset %s line%s)", | 
|---|
| 316 | format_linenum (numbuf, last_offset), | 
|---|
| 317 | "s" + (last_offset == 1)); | 
|---|
| 318 | say (".\n"); | 
|---|
| 319 | } | 
|---|
| 320 | } | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | if (!skip_rest_of_patch) | 
|---|
| 324 | { | 
|---|
| 325 | if (got_hunk < 0  &&  using_plan_a) | 
|---|
| 326 | { | 
|---|
| 327 | if (outfile) | 
|---|
| 328 | fatal ("out of memory using Plan A"); | 
|---|
| 329 | say ("\n\nRan out of memory using Plan A -- trying again...\n\n"); | 
|---|
| 330 | if (outstate.ofp) | 
|---|
| 331 | { | 
|---|
| 332 | fclose (outstate.ofp); | 
|---|
| 333 | outstate.ofp = 0; | 
|---|
| 334 | } | 
|---|
| 335 | fclose (rejfp); | 
|---|
| 336 | continue; | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | /* Finish spewing out the new file.  */ | 
|---|
| 340 | assert (hunk); | 
|---|
| 341 | if (! spew_output (&outstate)) | 
|---|
| 342 | { | 
|---|
| 343 | say ("Skipping patch.\n"); | 
|---|
| 344 | skip_rest_of_patch = true; | 
|---|
| 345 | } | 
|---|
| 346 | } | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | /* and put the output where desired */ | 
|---|
| 350 | ignore_signals (); | 
|---|
| 351 | if (! skip_rest_of_patch && ! outfile) { | 
|---|
| 352 | if (outstate.zero_output | 
|---|
| 353 | && (remove_empty_files | 
|---|
| 354 | || (pch_says_nonexistent (! reverse) == 2 | 
|---|
| 355 | && ! posixly_correct))) | 
|---|
| 356 | { | 
|---|
| 357 | if (verbosity == VERBOSE) | 
|---|
| 358 | say ("Removing file %s%s\n", quotearg (outname), | 
|---|
| 359 | dry_run ? " and any empty ancestor directories" : ""); | 
|---|
| 360 | if (! dry_run) | 
|---|
| 361 | { | 
|---|
| 362 | move_file ((char *) 0, (int *) 0, outname, (mode_t) 0, | 
|---|
| 363 | (make_backups | 
|---|
| 364 | || (backup_if_mismatch && (mismatch | failed)))); | 
|---|
| 365 | removedirs (outname); | 
|---|
| 366 | } | 
|---|
| 367 | } | 
|---|
| 368 | else | 
|---|
| 369 | { | 
|---|
| 370 | if (! outstate.zero_output | 
|---|
| 371 | && pch_says_nonexistent (! reverse)) | 
|---|
| 372 | { | 
|---|
| 373 | mismatch = true; | 
|---|
| 374 | if (verbosity != SILENT) | 
|---|
| 375 | say ("File %s is not empty after patch, as expected\n", | 
|---|
| 376 | quotearg (outname)); | 
|---|
| 377 | } | 
|---|
| 378 |  | 
|---|
| 379 | if (! dry_run) | 
|---|
| 380 | { | 
|---|
| 381 | time_t t; | 
|---|
| 382 |  | 
|---|
| 383 | move_file (TMPOUTNAME, &TMPOUTNAME_needs_removal, | 
|---|
| 384 | outname, instat.st_mode, | 
|---|
| 385 | (make_backups | 
|---|
| 386 | || (backup_if_mismatch && (mismatch | failed)))); | 
|---|
| 387 |  | 
|---|
| 388 | if ((set_time | set_utc) | 
|---|
| 389 | && (t = pch_timestamp (! reverse)) != (time_t) -1) | 
|---|
| 390 | { | 
|---|
| 391 | struct utimbuf utimbuf; | 
|---|
| 392 | utimbuf.actime = utimbuf.modtime = t; | 
|---|
| 393 |  | 
|---|
| 394 | if (! force && ! inerrno | 
|---|
| 395 | && pch_says_nonexistent (reverse) != 2 | 
|---|
| 396 | && (t = pch_timestamp (reverse)) != (time_t) -1 | 
|---|
| 397 | && t != instat.st_mtime) | 
|---|
| 398 | say ("Not setting time of file %s (time mismatch)\n", | 
|---|
| 399 | quotearg (outname)); | 
|---|
| 400 | else if (! force && (mismatch | failed)) | 
|---|
| 401 | say ("Not setting time of file %s (contents mismatch)\n", | 
|---|
| 402 | quotearg (outname)); | 
|---|
| 403 | else if (utime (outname, &utimbuf) != 0) | 
|---|
| 404 | pfatal ("Can't set timestamp on file %s", | 
|---|
| 405 | quotearg (outname)); | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 | if (! inerrno && chmod (outname, instat.st_mode) != 0) | 
|---|
| 409 | pfatal ("Can't set permissions on file %s", | 
|---|
| 410 | quotearg (outname)); | 
|---|
| 411 | } | 
|---|
| 412 | } | 
|---|
| 413 | } | 
|---|
| 414 | if (diff_type != ED_DIFF) { | 
|---|
| 415 | if (fclose (rejfp) != 0) | 
|---|
| 416 | write_fatal (); | 
|---|
| 417 | if (failed) { | 
|---|
| 418 | somefailed = true; | 
|---|
| 419 | say ("%d out of %d hunk%s %s", failed, hunk, "s" + (hunk == 1), | 
|---|
| 420 | skip_rest_of_patch ? "ignored" : "FAILED"); | 
|---|
| 421 | if (outname) { | 
|---|
| 422 | char *rej = rejname; | 
|---|
| 423 | if (!rejname) { | 
|---|
| 424 | rej = xmalloc (strlen (outname) + 5); | 
|---|
| 425 | strcpy (rej, outname); | 
|---|
| 426 | addext (rej, ".rej", '#'); | 
|---|
| 427 | } | 
|---|
| 428 | say (" -- saving rejects to file %s", quotearg (rej)); | 
|---|
| 429 | if (! dry_run) | 
|---|
| 430 | { | 
|---|
| 431 | move_file (TMPREJNAME, &TMPREJNAME_needs_removal, | 
|---|
| 432 | rej, instat.st_mode, false); | 
|---|
| 433 | if (! inerrno | 
|---|
| 434 | && (chmod (rej, (instat.st_mode | 
|---|
| 435 | & ~(S_IXUSR|S_IXGRP|S_IXOTH))) | 
|---|
| 436 | != 0)) | 
|---|
| 437 | pfatal ("can't set permissions on file %s", | 
|---|
| 438 | quotearg (rej)); | 
|---|
| 439 | } | 
|---|
| 440 | if (!rejname) | 
|---|
| 441 | free (rej); | 
|---|
| 442 | } | 
|---|
| 443 | say ("\n"); | 
|---|
| 444 | } | 
|---|
| 445 | } | 
|---|
| 446 | set_signals (true); | 
|---|
| 447 | } | 
|---|
| 448 | if (outstate.ofp && (ferror (outstate.ofp) || fclose (outstate.ofp) != 0)) | 
|---|
| 449 | write_fatal (); | 
|---|
| 450 | cleanup (); | 
|---|
| 451 | if (somefailed) | 
|---|
| 452 | exit (1); | 
|---|
| 453 | return 0; | 
|---|
| 454 | } | 
|---|
| 455 |  | 
|---|
| 456 | /* Prepare to find the next patch to do in the patch file. */ | 
|---|
| 457 |  | 
|---|
| 458 | static void | 
|---|
| 459 | reinitialize_almost_everything (void) | 
|---|
| 460 | { | 
|---|
| 461 | re_patch(); | 
|---|
| 462 | re_input(); | 
|---|
| 463 |  | 
|---|
| 464 | input_lines = 0; | 
|---|
| 465 | last_frozen_line = 0; | 
|---|
| 466 |  | 
|---|
| 467 | if (inname) { | 
|---|
| 468 | free (inname); | 
|---|
| 469 | inname = 0; | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | last_offset = 0; | 
|---|
| 473 |  | 
|---|
| 474 | diff_type = NO_DIFF; | 
|---|
| 475 |  | 
|---|
| 476 | if (revision) { | 
|---|
| 477 | free(revision); | 
|---|
| 478 | revision = 0; | 
|---|
| 479 | } | 
|---|
| 480 |  | 
|---|
| 481 | reverse = reverse_flag_specified; | 
|---|
| 482 | skip_rest_of_patch = false; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | static char const shortopts[] = "bB:cd:D:eEfF:g:i:lnNo:p:r:RstTuvV:x:Y:z:Z"; | 
|---|
| 486 | static struct option const longopts[] = | 
|---|
| 487 | { | 
|---|
| 488 | {"backup", no_argument, NULL, 'b'}, | 
|---|
| 489 | {"prefix", required_argument, NULL, 'B'}, | 
|---|
| 490 | {"context", no_argument, NULL, 'c'}, | 
|---|
| 491 | {"directory", required_argument, NULL, 'd'}, | 
|---|
| 492 | {"ifdef", required_argument, NULL, 'D'}, | 
|---|
| 493 | {"ed", no_argument, NULL, 'e'}, | 
|---|
| 494 | {"remove-empty-files", no_argument, NULL, 'E'}, | 
|---|
| 495 | {"force", no_argument, NULL, 'f'}, | 
|---|
| 496 | {"fuzz", required_argument, NULL, 'F'}, | 
|---|
| 497 | {"get", no_argument, NULL, 'g'}, | 
|---|
| 498 | {"input", required_argument, NULL, 'i'}, | 
|---|
| 499 | {"ignore-whitespace", no_argument, NULL, 'l'}, | 
|---|
| 500 | {"normal", no_argument, NULL, 'n'}, | 
|---|
| 501 | {"forward", no_argument, NULL, 'N'}, | 
|---|
| 502 | {"output", required_argument, NULL, 'o'}, | 
|---|
| 503 | {"strip", required_argument, NULL, 'p'}, | 
|---|
| 504 | {"reject-file", required_argument, NULL, 'r'}, | 
|---|
| 505 | {"reverse", no_argument, NULL, 'R'}, | 
|---|
| 506 | {"quiet", no_argument, NULL, 's'}, | 
|---|
| 507 | {"silent", no_argument, NULL, 's'}, | 
|---|
| 508 | {"batch", no_argument, NULL, 't'}, | 
|---|
| 509 | {"set-time", no_argument, NULL, 'T'}, | 
|---|
| 510 | {"unified", no_argument, NULL, 'u'}, | 
|---|
| 511 | {"version", no_argument, NULL, 'v'}, | 
|---|
| 512 | {"version-control", required_argument, NULL, 'V'}, | 
|---|
| 513 | {"debug", required_argument, NULL, 'x'}, | 
|---|
| 514 | {"basename-prefix", required_argument, NULL, 'Y'}, | 
|---|
| 515 | {"suffix", required_argument, NULL, 'z'}, | 
|---|
| 516 | {"set-utc", no_argument, NULL, 'Z'}, | 
|---|
| 517 | {"dry-run", no_argument, NULL, CHAR_MAX + 1}, | 
|---|
| 518 | {"verbose", no_argument, NULL, CHAR_MAX + 2}, | 
|---|
| 519 | {"binary", no_argument, NULL, CHAR_MAX + 3}, | 
|---|
| 520 | {"help", no_argument, NULL, CHAR_MAX + 4}, | 
|---|
| 521 | {"backup-if-mismatch", no_argument, NULL, CHAR_MAX + 5}, | 
|---|
| 522 | {"no-backup-if-mismatch", no_argument, NULL, CHAR_MAX + 6}, | 
|---|
| 523 | {"posix", no_argument, NULL, CHAR_MAX + 7}, | 
|---|
| 524 | {"quoting-style", required_argument, NULL, CHAR_MAX + 8}, | 
|---|
| 525 | {NULL, no_argument, NULL, 0} | 
|---|
| 526 | }; | 
|---|
| 527 |  | 
|---|
| 528 | static char const *const option_help[] = | 
|---|
| 529 | { | 
|---|
| 530 | "Input options:", | 
|---|
| 531 | "", | 
|---|
| 532 | "  -p NUM  --strip=NUM  Strip NUM leading components from file names.", | 
|---|
| 533 | "  -F LINES  --fuzz LINES  Set the fuzz factor to LINES for inexact matching.", | 
|---|
| 534 | "  -l  --ignore-whitespace  Ignore white space changes between patch and input.", | 
|---|
| 535 | "", | 
|---|
| 536 | "  -c  --context  Interpret the patch as a context difference.", | 
|---|
| 537 | "  -e  --ed  Interpret the patch as an ed script.", | 
|---|
| 538 | "  -n  --normal  Interpret the patch as a normal difference.", | 
|---|
| 539 | "  -u  --unified  Interpret the patch as a unified difference.", | 
|---|
| 540 | "", | 
|---|
| 541 | "  -N  --forward  Ignore patches that appear to be reversed or already applied.", | 
|---|
| 542 | "  -R  --reverse  Assume patches were created with old and new files swapped.", | 
|---|
| 543 | "", | 
|---|
| 544 | "  -i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.", | 
|---|
| 545 | "", | 
|---|
| 546 | "Output options:", | 
|---|
| 547 | "", | 
|---|
| 548 | "  -o FILE  --output=FILE  Output patched files to FILE.", | 
|---|
| 549 | "  -r FILE  --reject-file=FILE  Output rejects to FILE.", | 
|---|
| 550 | "", | 
|---|
| 551 | "  -D NAME  --ifdef=NAME  Make merged if-then-else output using NAME.", | 
|---|
| 552 | "  -E  --remove-empty-files  Remove output files that are empty after patching.", | 
|---|
| 553 | "", | 
|---|
| 554 | "  -Z  --set-utc  Set times of patched files, assuming diff uses UTC (GMT).", | 
|---|
| 555 | "  -T  --set-time  Likewise, assuming local time.", | 
|---|
| 556 | "", | 
|---|
| 557 | "  --quoting-style=WORD   output file names using quoting style WORD.", | 
|---|
| 558 | "    Valid WORDs are: literal, shell, shell-always, c, escape.", | 
|---|
| 559 | "    Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.", | 
|---|
| 560 | "", | 
|---|
| 561 | "Backup and version control options:", | 
|---|
| 562 | "", | 
|---|
| 563 | "  -b  --backup  Back up the original contents of each file.", | 
|---|
| 564 | "  --backup-if-mismatch  Back up if the patch does not match exactly.", | 
|---|
| 565 | "  --no-backup-if-mismatch  Back up mismatches only if otherwise requested.", | 
|---|
| 566 | "", | 
|---|
| 567 | "  -V STYLE  --version-control=STYLE  Use STYLE version control.", | 
|---|
| 568 | "       STYLE is either 'simple', 'numbered', or 'existing'.", | 
|---|
| 569 | "  -B PREFIX  --prefix=PREFIX  Prepend PREFIX to backup file names.", | 
|---|
| 570 | "  -Y PREFIX  --basename-prefix=PREFIX  Prepend PREFIX to backup file basenames.", | 
|---|
| 571 | "  -z SUFFIX  --suffix=SUFFIX  Append SUFFIX to backup file names.", | 
|---|
| 572 | "", | 
|---|
| 573 | "  -g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.", | 
|---|
| 574 | "", | 
|---|
| 575 | "Miscellaneous options:", | 
|---|
| 576 | "", | 
|---|
| 577 | "  -t  --batch  Ask no questions; skip bad-Prereq patches; assume reversed.", | 
|---|
| 578 | "  -f  --force  Like -t, but ignore bad-Prereq patches, and assume unreversed.", | 
|---|
| 579 | "  -s  --quiet  --silent  Work silently unless an error occurs.", | 
|---|
| 580 | "  --verbose  Output extra information about the work being done.", | 
|---|
| 581 | "  --dry-run  Do not actually change any files; just print what would happen.", | 
|---|
| 582 | "  --posix  Conform to the POSIX standard.", | 
|---|
| 583 | "", | 
|---|
| 584 | "  -d DIR  --directory=DIR  Change the working directory to DIR first.", | 
|---|
| 585 | #if HAVE_SETMODE_DOS | 
|---|
| 586 | "  --binary  Read and write data in binary mode.", | 
|---|
| 587 | #else | 
|---|
| 588 | "  --binary  Read and write data in binary mode (no effect on this platform).", | 
|---|
| 589 | #endif | 
|---|
| 590 | "", | 
|---|
| 591 | "  -v  --version  Output version info.", | 
|---|
| 592 | "  --help  Output this help.", | 
|---|
| 593 | "", | 
|---|
| 594 | "Report bugs to <" PACKAGE_BUGREPORT ">.", | 
|---|
| 595 | 0 | 
|---|
| 596 | }; | 
|---|
| 597 |  | 
|---|
| 598 | static void | 
|---|
| 599 | usage (FILE *stream, int status) | 
|---|
| 600 | { | 
|---|
| 601 | char const * const *p; | 
|---|
| 602 |  | 
|---|
| 603 | if (status != 0) | 
|---|
| 604 | { | 
|---|
| 605 | fprintf (stream, "%s: Try `%s --help' for more information.\n", | 
|---|
| 606 | program_name, Argv[0]); | 
|---|
| 607 | } | 
|---|
| 608 | else | 
|---|
| 609 | { | 
|---|
| 610 | fprintf (stream, "Usage: %s [OPTION]... [ORIGFILE [PATCHFILE]]\n\n", | 
|---|
| 611 | Argv[0]); | 
|---|
| 612 | for (p = option_help;  *p;  p++) | 
|---|
| 613 | fprintf (stream, "%s\n", *p); | 
|---|
| 614 | } | 
|---|
| 615 |  | 
|---|
| 616 | exit (status); | 
|---|
| 617 | } | 
|---|
| 618 |  | 
|---|
| 619 | /* Process switches and filenames.  */ | 
|---|
| 620 |  | 
|---|
| 621 | static void | 
|---|
| 622 | get_some_switches (void) | 
|---|
| 623 | { | 
|---|
| 624 | register int optc; | 
|---|
| 625 |  | 
|---|
| 626 | if (rejname) | 
|---|
| 627 | free (rejname); | 
|---|
| 628 | rejname = 0; | 
|---|
| 629 | if (optind == Argc) | 
|---|
| 630 | return; | 
|---|
| 631 | while ((optc = getopt_long (Argc, Argv, shortopts, longopts, (int *) 0)) | 
|---|
| 632 | != -1) { | 
|---|
| 633 | switch (optc) { | 
|---|
| 634 | case 'b': | 
|---|
| 635 | make_backups = true; | 
|---|
| 636 | /* Special hack for backward compatibility with CVS 1.9. | 
|---|
| 637 | If the last 4 args are `-b SUFFIX ORIGFILE PATCHFILE', | 
|---|
| 638 | treat `-b' as if it were `-b -z'.  */ | 
|---|
| 639 | if (Argc - optind == 3 | 
|---|
| 640 | && strcmp (Argv[optind - 1], "-b") == 0 | 
|---|
| 641 | && ! (Argv[optind + 0][0] == '-' && Argv[optind + 0][1]) | 
|---|
| 642 | && ! (Argv[optind + 1][0] == '-' && Argv[optind + 1][1]) | 
|---|
| 643 | && ! (Argv[optind + 2][0] == '-' && Argv[optind + 2][1])) | 
|---|
| 644 | { | 
|---|
| 645 | optarg = Argv[optind++]; | 
|---|
| 646 | if (verbosity != SILENT) | 
|---|
| 647 | say ("warning: the `-b %s' option is obsolete; use `-b -z %s' instead\n", | 
|---|
| 648 | optarg, optarg); | 
|---|
| 649 | goto case_z; | 
|---|
| 650 | } | 
|---|
| 651 | break; | 
|---|
| 652 | case 'B': | 
|---|
| 653 | if (!*optarg) | 
|---|
| 654 | fatal ("backup prefix is empty"); | 
|---|
| 655 | origprae = savestr (optarg); | 
|---|
| 656 | break; | 
|---|
| 657 | case 'c': | 
|---|
| 658 | diff_type = CONTEXT_DIFF; | 
|---|
| 659 | break; | 
|---|
| 660 | case 'd': | 
|---|
| 661 | if (chdir(optarg) < 0) | 
|---|
| 662 | pfatal ("Can't change to directory %s", quotearg (optarg)); | 
|---|
| 663 | break; | 
|---|
| 664 | case 'D': | 
|---|
| 665 | do_defines = savestr (optarg); | 
|---|
| 666 | break; | 
|---|
| 667 | case 'e': | 
|---|
| 668 | diff_type = ED_DIFF; | 
|---|
| 669 | break; | 
|---|
| 670 | case 'E': | 
|---|
| 671 | remove_empty_files = true; | 
|---|
| 672 | break; | 
|---|
| 673 | case 'f': | 
|---|
| 674 | force = true; | 
|---|
| 675 | break; | 
|---|
| 676 | case 'F': | 
|---|
| 677 | maxfuzz = numeric_string (optarg, false, "fuzz factor"); | 
|---|
| 678 | break; | 
|---|
| 679 | case 'g': | 
|---|
| 680 | patch_get = numeric_string (optarg, true, "get option value"); | 
|---|
| 681 | break; | 
|---|
| 682 | case 'i': | 
|---|
| 683 | patchname = savestr (optarg); | 
|---|
| 684 | break; | 
|---|
| 685 | case 'l': | 
|---|
| 686 | canonicalize = true; | 
|---|
| 687 | break; | 
|---|
| 688 | case 'n': | 
|---|
| 689 | diff_type = NORMAL_DIFF; | 
|---|
| 690 | break; | 
|---|
| 691 | case 'N': | 
|---|
| 692 | noreverse = true; | 
|---|
| 693 | break; | 
|---|
| 694 | case 'o': | 
|---|
| 695 | if (strcmp (optarg, "-") == 0) | 
|---|
| 696 | fatal ("can't output patches to standard output"); | 
|---|
| 697 | outfile = savestr (optarg); | 
|---|
| 698 | break; | 
|---|
| 699 | case 'p': | 
|---|
| 700 | strippath = numeric_string (optarg, false, "strip count"); | 
|---|
| 701 | break; | 
|---|
| 702 | case 'r': | 
|---|
| 703 | rejname = savestr (optarg); | 
|---|
| 704 | break; | 
|---|
| 705 | case 'R': | 
|---|
| 706 | reverse = true; | 
|---|
| 707 | reverse_flag_specified = true; | 
|---|
| 708 | break; | 
|---|
| 709 | case 's': | 
|---|
| 710 | verbosity = SILENT; | 
|---|
| 711 | break; | 
|---|
| 712 | case 't': | 
|---|
| 713 | batch = true; | 
|---|
| 714 | break; | 
|---|
| 715 | case 'T': | 
|---|
| 716 | set_time = true; | 
|---|
| 717 | break; | 
|---|
| 718 | case 'u': | 
|---|
| 719 | diff_type = UNI_DIFF; | 
|---|
| 720 | break; | 
|---|
| 721 | case 'v': | 
|---|
| 722 | version(); | 
|---|
| 723 | exit (0); | 
|---|
| 724 | break; | 
|---|
| 725 | case 'V': | 
|---|
| 726 | version_control = optarg; | 
|---|
| 727 | version_control_context = "--version-control or -V option"; | 
|---|
| 728 | break; | 
|---|
| 729 | #if DEBUGGING | 
|---|
| 730 | case 'x': | 
|---|
| 731 | debug = numeric_string (optarg, true, "debugging option"); | 
|---|
| 732 | break; | 
|---|
| 733 | #endif | 
|---|
| 734 | case 'Y': | 
|---|
| 735 | if (!*optarg) | 
|---|
| 736 | fatal ("backup basename prefix is empty"); | 
|---|
| 737 | origbase = savestr (optarg); | 
|---|
| 738 | break; | 
|---|
| 739 | case 'z': | 
|---|
| 740 | case_z: | 
|---|
| 741 | if (!*optarg) | 
|---|
| 742 | fatal ("backup suffix is empty"); | 
|---|
| 743 | simple_backup_suffix = savestr (optarg); | 
|---|
| 744 | break; | 
|---|
| 745 | case 'Z': | 
|---|
| 746 | set_utc = true; | 
|---|
| 747 | break; | 
|---|
| 748 | case CHAR_MAX + 1: | 
|---|
| 749 | dry_run = true; | 
|---|
| 750 | break; | 
|---|
| 751 | case CHAR_MAX + 2: | 
|---|
| 752 | verbosity = VERBOSE; | 
|---|
| 753 | break; | 
|---|
| 754 | case CHAR_MAX + 3: | 
|---|
| 755 | #if HAVE_SETMODE_DOS | 
|---|
| 756 | binary_transput = O_BINARY; | 
|---|
| 757 | #endif | 
|---|
| 758 | break; | 
|---|
| 759 | case CHAR_MAX + 4: | 
|---|
| 760 | usage (stdout, 0); | 
|---|
| 761 | case CHAR_MAX + 5: | 
|---|
| 762 | backup_if_mismatch = true; | 
|---|
| 763 | break; | 
|---|
| 764 | case CHAR_MAX + 6: | 
|---|
| 765 | backup_if_mismatch = false; | 
|---|
| 766 | break; | 
|---|
| 767 | case CHAR_MAX + 7: | 
|---|
| 768 | posixly_correct = true; | 
|---|
| 769 | break; | 
|---|
| 770 | case CHAR_MAX + 8: | 
|---|
| 771 | { | 
|---|
| 772 | int i = argmatch (optarg, quoting_style_args, 0, 0); | 
|---|
| 773 | if (i < 0) | 
|---|
| 774 | { | 
|---|
| 775 | invalid_arg ("quoting style", optarg, i); | 
|---|
| 776 | usage (stderr, 2); | 
|---|
| 777 | } | 
|---|
| 778 | set_quoting_style ((struct quoting_options *) 0, | 
|---|
| 779 | (enum quoting_style) i); | 
|---|
| 780 | } | 
|---|
| 781 | break; | 
|---|
| 782 | default: | 
|---|
| 783 | usage (stderr, 2); | 
|---|
| 784 | } | 
|---|
| 785 | } | 
|---|
| 786 |  | 
|---|
| 787 | /* Process any filename args.  */ | 
|---|
| 788 | if (optind < Argc) | 
|---|
| 789 | { | 
|---|
| 790 | inname = savestr (Argv[optind++]); | 
|---|
| 791 | invc = -1; | 
|---|
| 792 | if (optind < Argc) | 
|---|
| 793 | { | 
|---|
| 794 | patchname = savestr (Argv[optind++]); | 
|---|
| 795 | if (optind < Argc) | 
|---|
| 796 | { | 
|---|
| 797 | fprintf (stderr, "%s: %s: extra operand\n", | 
|---|
| 798 | program_name, quotearg (Argv[optind])); | 
|---|
| 799 | usage (stderr, 2); | 
|---|
| 800 | } | 
|---|
| 801 | } | 
|---|
| 802 | } | 
|---|
| 803 | } | 
|---|
| 804 |  | 
|---|
| 805 | /* Handle STRING (possibly negative if NEGATIVE_ALLOWED is nonzero) | 
|---|
| 806 | of type ARGTYPE_MSGID by converting it to an integer, | 
|---|
| 807 | returning the result.  */ | 
|---|
| 808 | static int | 
|---|
| 809 | numeric_string (char const *string, | 
|---|
| 810 | bool negative_allowed, | 
|---|
| 811 | char const *argtype_msgid) | 
|---|
| 812 | { | 
|---|
| 813 | int value = 0; | 
|---|
| 814 | char const *p = string; | 
|---|
| 815 | int sign = *p == '-' ? -1 : 1; | 
|---|
| 816 |  | 
|---|
| 817 | p += *p == '-' || *p == '+'; | 
|---|
| 818 |  | 
|---|
| 819 | do | 
|---|
| 820 | { | 
|---|
| 821 | int v10 = value * 10; | 
|---|
| 822 | int digit = *p - '0'; | 
|---|
| 823 | int signed_digit = sign * digit; | 
|---|
| 824 | int next_value = v10 + signed_digit; | 
|---|
| 825 |  | 
|---|
| 826 | if (9 < (unsigned) digit) | 
|---|
| 827 | fatal ("%s %s is not a number", argtype_msgid, quotearg (string)); | 
|---|
| 828 |  | 
|---|
| 829 | if (v10 / 10 != value || (next_value < v10) != (signed_digit < 0)) | 
|---|
| 830 | fatal ("%s %s is too large", argtype_msgid, quotearg (string)); | 
|---|
| 831 |  | 
|---|
| 832 | value = next_value; | 
|---|
| 833 | } | 
|---|
| 834 | while (*++p); | 
|---|
| 835 |  | 
|---|
| 836 | if (value < 0 && ! negative_allowed) | 
|---|
| 837 | fatal ("%s %s is negative", argtype_msgid, quotearg (string)); | 
|---|
| 838 |  | 
|---|
| 839 | return value; | 
|---|
| 840 | } | 
|---|
| 841 |  | 
|---|
| 842 | /* Attempt to find the right place to apply this hunk of patch. */ | 
|---|
| 843 |  | 
|---|
| 844 | static LINENUM | 
|---|
| 845 | locate_hunk (LINENUM fuzz) | 
|---|
| 846 | { | 
|---|
| 847 | register LINENUM first_guess = pch_first () + last_offset; | 
|---|
| 848 | register LINENUM offset; | 
|---|
| 849 | LINENUM pat_lines = pch_ptrn_lines(); | 
|---|
| 850 | LINENUM prefix_context = pch_prefix_context (); | 
|---|
| 851 | LINENUM suffix_context = pch_suffix_context (); | 
|---|
| 852 | LINENUM context = (prefix_context < suffix_context | 
|---|
| 853 | ? suffix_context : prefix_context); | 
|---|
| 854 | LINENUM prefix_fuzz = fuzz + prefix_context - context; | 
|---|
| 855 | LINENUM suffix_fuzz = fuzz + suffix_context - context; | 
|---|
| 856 | LINENUM max_where = input_lines - (pat_lines - suffix_fuzz) + 1; | 
|---|
| 857 | LINENUM min_where = last_frozen_line + 1 - (prefix_context - prefix_fuzz); | 
|---|
| 858 | LINENUM max_pos_offset = max_where - first_guess; | 
|---|
| 859 | LINENUM max_neg_offset = first_guess - min_where; | 
|---|
| 860 | LINENUM max_offset = (max_pos_offset < max_neg_offset | 
|---|
| 861 | ? max_neg_offset : max_pos_offset); | 
|---|
| 862 |  | 
|---|
| 863 | if (!pat_lines)                     /* null range matches always */ | 
|---|
| 864 | return first_guess; | 
|---|
| 865 |  | 
|---|
| 866 | /* Do not try lines <= 0.  */ | 
|---|
| 867 | if (first_guess <= max_neg_offset) | 
|---|
| 868 | max_neg_offset = first_guess - 1; | 
|---|
| 869 |  | 
|---|
| 870 | if (prefix_fuzz < 0) | 
|---|
| 871 | { | 
|---|
| 872 | /* Can only match start of file.  */ | 
|---|
| 873 |  | 
|---|
| 874 | if (suffix_fuzz < 0) | 
|---|
| 875 | /* Can only match entire file.  */ | 
|---|
| 876 | if (pat_lines != input_lines || prefix_context < last_frozen_line) | 
|---|
| 877 | return 0; | 
|---|
| 878 |  | 
|---|
| 879 | offset = 1 - first_guess; | 
|---|
| 880 | if (last_frozen_line <= prefix_context | 
|---|
| 881 | && offset <= max_pos_offset | 
|---|
| 882 | && patch_match (first_guess, offset, (LINENUM) 0, suffix_fuzz)) | 
|---|
| 883 | { | 
|---|
| 884 | last_offset += offset; | 
|---|
| 885 | return first_guess + offset; | 
|---|
| 886 | } | 
|---|
| 887 | else | 
|---|
| 888 | return 0; | 
|---|
| 889 | } | 
|---|
| 890 |  | 
|---|
| 891 | if (suffix_fuzz < 0) | 
|---|
| 892 | { | 
|---|
| 893 | /* Can only match end of file.  */ | 
|---|
| 894 | offset = first_guess - (input_lines - pat_lines + 1); | 
|---|
| 895 | if (offset <= max_neg_offset | 
|---|
| 896 | && patch_match (first_guess, -offset, prefix_fuzz, (LINENUM) 0)) | 
|---|
| 897 | { | 
|---|
| 898 | last_offset -= offset; | 
|---|
| 899 | return first_guess - offset; | 
|---|
| 900 | } | 
|---|
| 901 | else | 
|---|
| 902 | return 0; | 
|---|
| 903 | } | 
|---|
| 904 |  | 
|---|
| 905 | for (offset = 0;  offset <= max_offset;  offset++) { | 
|---|
| 906 | char numbuf0[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 907 | char numbuf1[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 908 | if (offset <= max_pos_offset | 
|---|
| 909 | && patch_match (first_guess, offset, prefix_fuzz, suffix_fuzz)) { | 
|---|
| 910 | if (debug & 1) | 
|---|
| 911 | say ("Offset changing from %s to %s\n", | 
|---|
| 912 | format_linenum (numbuf0, last_offset), | 
|---|
| 913 | format_linenum (numbuf1, last_offset + offset)); | 
|---|
| 914 | last_offset += offset; | 
|---|
| 915 | return first_guess+offset; | 
|---|
| 916 | } | 
|---|
| 917 | if (0 < offset && offset <= max_neg_offset | 
|---|
| 918 | && patch_match (first_guess, -offset, prefix_fuzz, suffix_fuzz)) { | 
|---|
| 919 | if (debug & 1) | 
|---|
| 920 | say ("Offset changing from %s to %s\n", | 
|---|
| 921 | format_linenum (numbuf0, last_offset), | 
|---|
| 922 | format_linenum (numbuf1, last_offset - offset)); | 
|---|
| 923 | last_offset -= offset; | 
|---|
| 924 | return first_guess-offset; | 
|---|
| 925 | } | 
|---|
| 926 | } | 
|---|
| 927 | return 0; | 
|---|
| 928 | } | 
|---|
| 929 |  | 
|---|
| 930 | /* We did not find the pattern, dump out the hunk so they can handle it. */ | 
|---|
| 931 |  | 
|---|
| 932 | static void | 
|---|
| 933 | abort_hunk (void) | 
|---|
| 934 | { | 
|---|
| 935 | register LINENUM i; | 
|---|
| 936 | register LINENUM pat_end = pch_end (); | 
|---|
| 937 | /* add in last_offset to guess the same as the previous successful hunk */ | 
|---|
| 938 | LINENUM oldfirst = pch_first() + last_offset; | 
|---|
| 939 | LINENUM newfirst = pch_newfirst() + last_offset; | 
|---|
| 940 | LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1; | 
|---|
| 941 | LINENUM newlast = newfirst + pch_repl_lines() - 1; | 
|---|
| 942 | char const *stars = | 
|---|
| 943 | (int) NEW_CONTEXT_DIFF <= (int) diff_type ? " ****" : ""; | 
|---|
| 944 | char const *minuses = | 
|---|
| 945 | (int) NEW_CONTEXT_DIFF <= (int) diff_type ? " ----" : " -----"; | 
|---|
| 946 |  | 
|---|
| 947 | fprintf(rejfp, "***************\n"); | 
|---|
| 948 | for (i=0; i<=pat_end; i++) { | 
|---|
| 949 | char numbuf0[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 950 | char numbuf1[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 951 | switch (pch_char(i)) { | 
|---|
| 952 | case '*': | 
|---|
| 953 | if (oldlast < oldfirst) | 
|---|
| 954 | fprintf(rejfp, "*** 0%s\n", stars); | 
|---|
| 955 | else if (oldlast == oldfirst) | 
|---|
| 956 | fprintf (rejfp, "*** %s%s\n", | 
|---|
| 957 | format_linenum (numbuf0, oldfirst), stars); | 
|---|
| 958 | else | 
|---|
| 959 | fprintf (rejfp, "*** %s,%s%s\n", | 
|---|
| 960 | format_linenum (numbuf0, oldfirst), | 
|---|
| 961 | format_linenum (numbuf1, oldlast), stars); | 
|---|
| 962 | break; | 
|---|
| 963 | case '=': | 
|---|
| 964 | if (newlast < newfirst) | 
|---|
| 965 | fprintf(rejfp, "--- 0%s\n", minuses); | 
|---|
| 966 | else if (newlast == newfirst) | 
|---|
| 967 | fprintf (rejfp, "--- %s%s\n", | 
|---|
| 968 | format_linenum (numbuf0, newfirst), minuses); | 
|---|
| 969 | else | 
|---|
| 970 | fprintf (rejfp, "--- %s,%s%s\n", | 
|---|
| 971 | format_linenum (numbuf0, newfirst), | 
|---|
| 972 | format_linenum (numbuf1, newlast), minuses); | 
|---|
| 973 | break; | 
|---|
| 974 | case ' ': case '-': case '+': case '!': | 
|---|
| 975 | fprintf (rejfp, "%c ", pch_char (i)); | 
|---|
| 976 | /* fall into */ | 
|---|
| 977 | case '\n': | 
|---|
| 978 | pch_write_line (i, rejfp); | 
|---|
| 979 | break; | 
|---|
| 980 | default: | 
|---|
| 981 | fatal ("fatal internal error in abort_hunk"); | 
|---|
| 982 | } | 
|---|
| 983 | if (ferror (rejfp)) | 
|---|
| 984 | write_fatal (); | 
|---|
| 985 | } | 
|---|
| 986 | } | 
|---|
| 987 |  | 
|---|
| 988 | /* We found where to apply it (we hope), so do it. */ | 
|---|
| 989 |  | 
|---|
| 990 | static bool | 
|---|
| 991 | apply_hunk (struct outstate *outstate, LINENUM where) | 
|---|
| 992 | { | 
|---|
| 993 | register LINENUM old = 1; | 
|---|
| 994 | register LINENUM lastline = pch_ptrn_lines (); | 
|---|
| 995 | register LINENUM new = lastline+1; | 
|---|
| 996 | register enum {OUTSIDE, IN_IFNDEF, IN_IFDEF, IN_ELSE} def_state = OUTSIDE; | 
|---|
| 997 | register char const *R_do_defines = do_defines; | 
|---|
| 998 | register LINENUM pat_end = pch_end (); | 
|---|
| 999 | register FILE *fp = outstate->ofp; | 
|---|
| 1000 |  | 
|---|
| 1001 | where--; | 
|---|
| 1002 | while (pch_char(new) == '=' || pch_char(new) == '\n') | 
|---|
| 1003 | new++; | 
|---|
| 1004 |  | 
|---|
| 1005 | while (old <= lastline) { | 
|---|
| 1006 | if (pch_char(old) == '-') { | 
|---|
| 1007 | assert (outstate->after_newline); | 
|---|
| 1008 | if (! copy_till (outstate, where + old - 1)) | 
|---|
| 1009 | return false; | 
|---|
| 1010 | if (R_do_defines) { | 
|---|
| 1011 | if (def_state == OUTSIDE) { | 
|---|
| 1012 | fprintf (fp, outstate->after_newline + not_defined, | 
|---|
| 1013 | R_do_defines); | 
|---|
| 1014 | def_state = IN_IFNDEF; | 
|---|
| 1015 | } | 
|---|
| 1016 | else if (def_state == IN_IFDEF) { | 
|---|
| 1017 | fprintf (fp, outstate->after_newline + else_defined); | 
|---|
| 1018 | def_state = IN_ELSE; | 
|---|
| 1019 | } | 
|---|
| 1020 | if (ferror (fp)) | 
|---|
| 1021 | write_fatal (); | 
|---|
| 1022 | outstate->after_newline = pch_write_line (old, fp); | 
|---|
| 1023 | outstate->zero_output = false; | 
|---|
| 1024 | } | 
|---|
| 1025 | last_frozen_line++; | 
|---|
| 1026 | old++; | 
|---|
| 1027 | } | 
|---|
| 1028 | else if (new > pat_end) { | 
|---|
| 1029 | break; | 
|---|
| 1030 | } | 
|---|
| 1031 | else if (pch_char(new) == '+') { | 
|---|
| 1032 | if (! copy_till (outstate, where + old - 1)) | 
|---|
| 1033 | return false; | 
|---|
| 1034 | if (R_do_defines) { | 
|---|
| 1035 | if (def_state == IN_IFNDEF) { | 
|---|
| 1036 | fprintf (fp, outstate->after_newline + else_defined); | 
|---|
| 1037 | def_state = IN_ELSE; | 
|---|
| 1038 | } | 
|---|
| 1039 | else if (def_state == OUTSIDE) { | 
|---|
| 1040 | fprintf (fp, outstate->after_newline + if_defined, | 
|---|
| 1041 | R_do_defines); | 
|---|
| 1042 | def_state = IN_IFDEF; | 
|---|
| 1043 | } | 
|---|
| 1044 | if (ferror (fp)) | 
|---|
| 1045 | write_fatal (); | 
|---|
| 1046 | } | 
|---|
| 1047 | outstate->after_newline = pch_write_line (new, fp); | 
|---|
| 1048 | outstate->zero_output = false; | 
|---|
| 1049 | new++; | 
|---|
| 1050 | } | 
|---|
| 1051 | else if (pch_char(new) != pch_char(old)) { | 
|---|
| 1052 | char numbuf0[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 1053 | char numbuf1[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 1054 | if (debug & 1) | 
|---|
| 1055 | say ("oldchar = '%c', newchar = '%c'\n", | 
|---|
| 1056 | pch_char (old), pch_char (new)); | 
|---|
| 1057 | fatal ("Out-of-sync patch, lines %s,%s -- mangled text or line numbers, maybe?", | 
|---|
| 1058 | format_linenum (numbuf0, pch_hunk_beg() + old), | 
|---|
| 1059 | format_linenum (numbuf1, pch_hunk_beg() + new)); | 
|---|
| 1060 | } | 
|---|
| 1061 | else if (pch_char(new) == '!') { | 
|---|
| 1062 | assert (outstate->after_newline); | 
|---|
| 1063 | if (! copy_till (outstate, where + old - 1)) | 
|---|
| 1064 | return false; | 
|---|
| 1065 | assert (outstate->after_newline); | 
|---|
| 1066 | if (R_do_defines) { | 
|---|
| 1067 | fprintf (fp, 1 + not_defined, R_do_defines); | 
|---|
| 1068 | if (ferror (fp)) | 
|---|
| 1069 | write_fatal (); | 
|---|
| 1070 | def_state = IN_IFNDEF; | 
|---|
| 1071 | } | 
|---|
| 1072 |  | 
|---|
| 1073 | do | 
|---|
| 1074 | { | 
|---|
| 1075 | if (R_do_defines) { | 
|---|
| 1076 | outstate->after_newline = pch_write_line (old, fp); | 
|---|
| 1077 | } | 
|---|
| 1078 | last_frozen_line++; | 
|---|
| 1079 | old++; | 
|---|
| 1080 | } | 
|---|
| 1081 | while (pch_char (old) == '!'); | 
|---|
| 1082 |  | 
|---|
| 1083 | if (R_do_defines) { | 
|---|
| 1084 | fprintf (fp, outstate->after_newline + else_defined); | 
|---|
| 1085 | if (ferror (fp)) | 
|---|
| 1086 | write_fatal (); | 
|---|
| 1087 | def_state = IN_ELSE; | 
|---|
| 1088 | } | 
|---|
| 1089 |  | 
|---|
| 1090 | do | 
|---|
| 1091 | { | 
|---|
| 1092 | outstate->after_newline = pch_write_line (new, fp); | 
|---|
| 1093 | new++; | 
|---|
| 1094 | } | 
|---|
| 1095 | while (pch_char (new) == '!'); | 
|---|
| 1096 | outstate->zero_output = false; | 
|---|
| 1097 | } | 
|---|
| 1098 | else { | 
|---|
| 1099 | assert(pch_char(new) == ' '); | 
|---|
| 1100 | old++; | 
|---|
| 1101 | new++; | 
|---|
| 1102 | if (R_do_defines && def_state != OUTSIDE) { | 
|---|
| 1103 | fprintf (fp, outstate->after_newline + end_defined); | 
|---|
| 1104 | if (ferror (fp)) | 
|---|
| 1105 | write_fatal (); | 
|---|
| 1106 | outstate->after_newline = true; | 
|---|
| 1107 | def_state = OUTSIDE; | 
|---|
| 1108 | } | 
|---|
| 1109 | } | 
|---|
| 1110 | } | 
|---|
| 1111 | if (new <= pat_end && pch_char(new) == '+') { | 
|---|
| 1112 | if (! copy_till (outstate, where + old - 1)) | 
|---|
| 1113 | return false; | 
|---|
| 1114 | if (R_do_defines) { | 
|---|
| 1115 | if (def_state == OUTSIDE) { | 
|---|
| 1116 | fprintf (fp, outstate->after_newline + if_defined, | 
|---|
| 1117 | R_do_defines); | 
|---|
| 1118 | def_state = IN_IFDEF; | 
|---|
| 1119 | } | 
|---|
| 1120 | else if (def_state == IN_IFNDEF) { | 
|---|
| 1121 | fprintf (fp, outstate->after_newline + else_defined); | 
|---|
| 1122 | def_state = IN_ELSE; | 
|---|
| 1123 | } | 
|---|
| 1124 | if (ferror (fp)) | 
|---|
| 1125 | write_fatal (); | 
|---|
| 1126 | outstate->zero_output = false; | 
|---|
| 1127 | } | 
|---|
| 1128 |  | 
|---|
| 1129 | do | 
|---|
| 1130 | { | 
|---|
| 1131 | if (! outstate->after_newline  &&  putc ('\n', fp) == EOF) | 
|---|
| 1132 | write_fatal (); | 
|---|
| 1133 | outstate->after_newline = pch_write_line (new, fp); | 
|---|
| 1134 | outstate->zero_output = false; | 
|---|
| 1135 | new++; | 
|---|
| 1136 | } | 
|---|
| 1137 | while (new <= pat_end && pch_char (new) == '+'); | 
|---|
| 1138 | } | 
|---|
| 1139 | if (R_do_defines && def_state != OUTSIDE) { | 
|---|
| 1140 | fprintf (fp, outstate->after_newline + end_defined); | 
|---|
| 1141 | if (ferror (fp)) | 
|---|
| 1142 | write_fatal (); | 
|---|
| 1143 | outstate->after_newline = true; | 
|---|
| 1144 | } | 
|---|
| 1145 | return true; | 
|---|
| 1146 | } | 
|---|
| 1147 |  | 
|---|
| 1148 | /* Create an output file.  */ | 
|---|
| 1149 |  | 
|---|
| 1150 | static FILE * | 
|---|
| 1151 | create_output_file (char const *name, int open_flags) | 
|---|
| 1152 | { | 
|---|
| 1153 | int fd = create_file (name, O_WRONLY | binary_transput | open_flags, | 
|---|
| 1154 | instat.st_mode); | 
|---|
| 1155 | FILE *f = fdopen (fd, binary_transput ? "wb" : "w"); | 
|---|
| 1156 | if (! f) | 
|---|
| 1157 | pfatal ("Can't create file %s", quotearg (name)); | 
|---|
| 1158 | return f; | 
|---|
| 1159 | } | 
|---|
| 1160 |  | 
|---|
| 1161 | /* Open the new file. */ | 
|---|
| 1162 |  | 
|---|
| 1163 | static void | 
|---|
| 1164 | init_output (char const *name, int open_flags, struct outstate *outstate) | 
|---|
| 1165 | { | 
|---|
| 1166 | outstate->ofp = name ? create_output_file (name, open_flags) : (FILE *) 0; | 
|---|
| 1167 | outstate->after_newline = true; | 
|---|
| 1168 | outstate->zero_output = true; | 
|---|
| 1169 | } | 
|---|
| 1170 |  | 
|---|
| 1171 | /* Open a file to put hunks we can't locate. */ | 
|---|
| 1172 |  | 
|---|
| 1173 | static void | 
|---|
| 1174 | init_reject (void) | 
|---|
| 1175 | { | 
|---|
| 1176 | int exclusive = TMPREJNAME_needs_removal ? 0 : O_EXCL; | 
|---|
| 1177 | TMPREJNAME_needs_removal = 1; | 
|---|
| 1178 | rejfp = create_output_file (TMPREJNAME, exclusive); | 
|---|
| 1179 | } | 
|---|
| 1180 |  | 
|---|
| 1181 | /* Copy input file to output, up to wherever hunk is to be applied. */ | 
|---|
| 1182 |  | 
|---|
| 1183 | static bool | 
|---|
| 1184 | copy_till (register struct outstate *outstate, register LINENUM lastline) | 
|---|
| 1185 | { | 
|---|
| 1186 | register LINENUM R_last_frozen_line = last_frozen_line; | 
|---|
| 1187 | register FILE *fp = outstate->ofp; | 
|---|
| 1188 | register char const *s; | 
|---|
| 1189 | size_t size; | 
|---|
| 1190 |  | 
|---|
| 1191 | if (R_last_frozen_line > lastline) | 
|---|
| 1192 | { | 
|---|
| 1193 | say ("misordered hunks! output would be garbled\n"); | 
|---|
| 1194 | return false; | 
|---|
| 1195 | } | 
|---|
| 1196 | while (R_last_frozen_line < lastline) | 
|---|
| 1197 | { | 
|---|
| 1198 | s = ifetch (++R_last_frozen_line, false, &size); | 
|---|
| 1199 | if (size) | 
|---|
| 1200 | { | 
|---|
| 1201 | if ((! outstate->after_newline  &&  putc ('\n', fp) == EOF) | 
|---|
| 1202 | || ! fwrite (s, sizeof *s, size, fp)) | 
|---|
| 1203 | write_fatal (); | 
|---|
| 1204 | outstate->after_newline = s[size - 1] == '\n'; | 
|---|
| 1205 | outstate->zero_output = false; | 
|---|
| 1206 | } | 
|---|
| 1207 | } | 
|---|
| 1208 | last_frozen_line = R_last_frozen_line; | 
|---|
| 1209 | return true; | 
|---|
| 1210 | } | 
|---|
| 1211 |  | 
|---|
| 1212 | /* Finish copying the input file to the output file. */ | 
|---|
| 1213 |  | 
|---|
| 1214 | static bool | 
|---|
| 1215 | spew_output (struct outstate *outstate) | 
|---|
| 1216 | { | 
|---|
| 1217 | if (debug & 256) | 
|---|
| 1218 | { | 
|---|
| 1219 | char numbuf0[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 1220 | char numbuf1[LINENUM_LENGTH_BOUND + 1]; | 
|---|
| 1221 | say ("il=%s lfl=%s\n", | 
|---|
| 1222 | format_linenum (numbuf0, input_lines), | 
|---|
| 1223 | format_linenum (numbuf1, last_frozen_line)); | 
|---|
| 1224 | } | 
|---|
| 1225 |  | 
|---|
| 1226 | if (last_frozen_line < input_lines) | 
|---|
| 1227 | if (! copy_till (outstate, input_lines)) | 
|---|
| 1228 | return false; | 
|---|
| 1229 |  | 
|---|
| 1230 | if (outstate->ofp && ! outfile) | 
|---|
| 1231 | { | 
|---|
| 1232 | if (fclose (outstate->ofp) != 0) | 
|---|
| 1233 | write_fatal (); | 
|---|
| 1234 | outstate->ofp = 0; | 
|---|
| 1235 | } | 
|---|
| 1236 |  | 
|---|
| 1237 | return true; | 
|---|
| 1238 | } | 
|---|
| 1239 |  | 
|---|
| 1240 | /* Does the patch pattern match at line base+offset? */ | 
|---|
| 1241 |  | 
|---|
| 1242 | static bool | 
|---|
| 1243 | patch_match (LINENUM base, LINENUM offset, | 
|---|
| 1244 | LINENUM prefix_fuzz, LINENUM suffix_fuzz) | 
|---|
| 1245 | { | 
|---|
| 1246 | register LINENUM pline = 1 + prefix_fuzz; | 
|---|
| 1247 | register LINENUM iline; | 
|---|
| 1248 | register LINENUM pat_lines = pch_ptrn_lines () - suffix_fuzz; | 
|---|
| 1249 | size_t size; | 
|---|
| 1250 | register char const *p; | 
|---|
| 1251 |  | 
|---|
| 1252 | for (iline=base+offset+prefix_fuzz; pline <= pat_lines; pline++,iline++) { | 
|---|
| 1253 | p = ifetch (iline, offset >= 0, &size); | 
|---|
| 1254 | if (canonicalize) { | 
|---|
| 1255 | if (!similar(p, size, | 
|---|
| 1256 | pfetch(pline), | 
|---|
| 1257 | pch_line_len(pline) )) | 
|---|
| 1258 | return false; | 
|---|
| 1259 | } | 
|---|
| 1260 | else if (size != pch_line_len (pline) | 
|---|
| 1261 | || memcmp (p, pfetch (pline), size) != 0) | 
|---|
| 1262 | return false; | 
|---|
| 1263 | } | 
|---|
| 1264 | return true; | 
|---|
| 1265 | } | 
|---|
| 1266 |  | 
|---|
| 1267 | /* Do two lines match with canonicalized white space? */ | 
|---|
| 1268 |  | 
|---|
| 1269 | static bool | 
|---|
| 1270 | similar (register char const *a, register size_t alen, | 
|---|
| 1271 | register char const *b, register size_t blen) | 
|---|
| 1272 | { | 
|---|
| 1273 | /* Ignore presence or absence of trailing newlines.  */ | 
|---|
| 1274 | alen  -=  alen && a[alen - 1] == '\n'; | 
|---|
| 1275 | blen  -=  blen && b[blen - 1] == '\n'; | 
|---|
| 1276 |  | 
|---|
| 1277 | for (;;) | 
|---|
| 1278 | { | 
|---|
| 1279 | if (!blen || (*b == ' ' || *b == '\t')) | 
|---|
| 1280 | { | 
|---|
| 1281 | while (blen && (*b == ' ' || *b == '\t')) | 
|---|
| 1282 | b++, blen--; | 
|---|
| 1283 | if (alen) | 
|---|
| 1284 | { | 
|---|
| 1285 | if (!(*a == ' ' || *a == '\t')) | 
|---|
| 1286 | return false; | 
|---|
| 1287 | do a++, alen--; | 
|---|
| 1288 | while (alen && (*a == ' ' || *a == '\t')); | 
|---|
| 1289 | } | 
|---|
| 1290 | if (!alen || !blen) | 
|---|
| 1291 | return alen == blen; | 
|---|
| 1292 | } | 
|---|
| 1293 | else if (!alen || *a++ != *b++) | 
|---|
| 1294 | return false; | 
|---|
| 1295 | else | 
|---|
| 1296 | alen--, blen--; | 
|---|
| 1297 | } | 
|---|
| 1298 | } | 
|---|
| 1299 |  | 
|---|
| 1300 | /* Make a temporary file.  */ | 
|---|
| 1301 |  | 
|---|
| 1302 | #if HAVE_MKTEMP && ! HAVE_DECL_MKTEMP && ! defined mktemp | 
|---|
| 1303 | char *mktemp (char *); | 
|---|
| 1304 | #endif | 
|---|
| 1305 |  | 
|---|
| 1306 | #ifndef TMPDIR | 
|---|
| 1307 | #define TMPDIR "/tmp" | 
|---|
| 1308 | #endif | 
|---|
| 1309 |  | 
|---|
| 1310 | static char const * | 
|---|
| 1311 | make_temp (char letter) | 
|---|
| 1312 | { | 
|---|
| 1313 | char *r; | 
|---|
| 1314 | #if HAVE_MKTEMP | 
|---|
| 1315 | char const *tmpdir = getenv ("TMPDIR");       /* Unix tradition */ | 
|---|
| 1316 | if (!tmpdir) tmpdir = getenv ("TMP");         /* DOS tradition */ | 
|---|
| 1317 | if (!tmpdir) tmpdir = getenv ("TEMP");        /* another DOS tradition */ | 
|---|
| 1318 | if (!tmpdir) tmpdir = TMPDIR; | 
|---|
| 1319 | r = xmalloc (strlen (tmpdir) + 10); | 
|---|
| 1320 | sprintf (r, "%s/p%cXXXXXX", tmpdir, letter); | 
|---|
| 1321 |  | 
|---|
| 1322 | /* It is OK to use mktemp here, since the rest of the code always | 
|---|
| 1323 | opens temp files with O_EXCL.  It might be better to use mkstemp | 
|---|
| 1324 | to avoid some DoS problems, but simply substituting mkstemp for | 
|---|
| 1325 | mktemp here will not fix the DoS problems; a more extensive | 
|---|
| 1326 | change would be needed.  */ | 
|---|
| 1327 | mktemp (r); | 
|---|
| 1328 |  | 
|---|
| 1329 | if (!*r) | 
|---|
| 1330 | pfatal ("mktemp"); | 
|---|
| 1331 | #else | 
|---|
| 1332 | r = xmalloc (L_tmpnam); | 
|---|
| 1333 | if (! (tmpnam (r) == r && *r)) | 
|---|
| 1334 | pfatal ("tmpnam"); | 
|---|
| 1335 | #endif | 
|---|
| 1336 | return r; | 
|---|
| 1337 | } | 
|---|
| 1338 |  | 
|---|
| 1339 | /* Fatal exit with cleanup. */ | 
|---|
| 1340 |  | 
|---|
| 1341 | void | 
|---|
| 1342 | fatal_exit (int sig) | 
|---|
| 1343 | { | 
|---|
| 1344 | cleanup (); | 
|---|
| 1345 |  | 
|---|
| 1346 | if (sig) | 
|---|
| 1347 | exit_with_signal (sig); | 
|---|
| 1348 |  | 
|---|
| 1349 | exit (2); | 
|---|
| 1350 | } | 
|---|
| 1351 |  | 
|---|
| 1352 | static void | 
|---|
| 1353 | remove_if_needed (char const *name, int volatile *needs_removal) | 
|---|
| 1354 | { | 
|---|
| 1355 | if (*needs_removal) | 
|---|
| 1356 | { | 
|---|
| 1357 | unlink (name); | 
|---|
| 1358 | *needs_removal = 0; | 
|---|
| 1359 | } | 
|---|
| 1360 | } | 
|---|
| 1361 |  | 
|---|
| 1362 | static void | 
|---|
| 1363 | cleanup (void) | 
|---|
| 1364 | { | 
|---|
| 1365 | remove_if_needed (TMPINNAME, &TMPINNAME_needs_removal); | 
|---|
| 1366 | remove_if_needed (TMPOUTNAME, &TMPOUTNAME_needs_removal); | 
|---|
| 1367 | remove_if_needed (TMPPATNAME, &TMPPATNAME_needs_removal); | 
|---|
| 1368 | remove_if_needed (TMPREJNAME, &TMPREJNAME_needs_removal); | 
|---|
| 1369 | } | 
|---|