xref: /freebsd/usr.bin/patch/patch.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1 /*-
2  * Copyright 1986, Larry Wall
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following condition is met:
6  * 1. Redistributions of source code must retain the above copyright notice,
7  * this condition and the following disclaimer.
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19  * SUCH DAMAGE.
20  *
21  * patch - a program to apply diffs to original files
22  *
23  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24  * behaviour
25  *
26  * $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $
27  * $FreeBSD$
28  *
29  */
30 
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 
34 #include <ctype.h>
35 #include <getopt.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 
42 #include "common.h"
43 #include "util.h"
44 #include "pch.h"
45 #include "inp.h"
46 #include "backupfile.h"
47 #include "pathnames.h"
48 
49 mode_t		filemode = 0644;
50 
51 char		*buf;			/* general purpose buffer */
52 size_t		buf_size;		/* size of the general purpose buffer */
53 
54 bool		using_plan_a = true;	/* try to keep everything in memory */
55 bool		out_of_mem = false;	/* ran out of memory in plan a */
56 
57 #define MAXFILEC 2
58 
59 char		*filearg[MAXFILEC];
60 bool		ok_to_create_file = false;
61 char		*outname = NULL;
62 char		*origprae = NULL;
63 char		*TMPOUTNAME;
64 char		*TMPINNAME;
65 char		*TMPREJNAME;
66 char		*TMPPATNAME;
67 bool		toutkeep = false;
68 bool		trejkeep = false;
69 bool		warn_on_invalid_line;
70 bool		last_line_missing_eol;
71 
72 #ifdef DEBUGGING
73 int		debug = 0;
74 #endif
75 
76 bool		force = false;
77 bool		batch = false;
78 bool		verbose = true;
79 bool		reverse = false;
80 bool		noreverse = false;
81 bool		skip_rest_of_patch = false;
82 int		strippath = 957;
83 bool		canonicalize = false;
84 bool		check_only = false;
85 int		diff_type = 0;
86 char		*revision = NULL;	/* prerequisite revision, if any */
87 LINENUM		input_lines = 0;	/* how long is input file in lines */
88 int		posix = 0;		/* strict POSIX mode? */
89 
90 static void	reinitialize_almost_everything(void);
91 static void	get_some_switches(void);
92 static LINENUM	locate_hunk(LINENUM);
93 static void	abort_context_hunk(void);
94 static void	rej_line(int, LINENUM);
95 static void	abort_hunk(void);
96 static void	apply_hunk(LINENUM);
97 static void	init_output(const char *);
98 static void	init_reject(const char *);
99 static void	copy_till(LINENUM, bool);
100 static bool	spew_output(void);
101 static void	dump_line(LINENUM, bool);
102 static bool	patch_match(LINENUM, LINENUM, LINENUM);
103 static bool	similar(const char *, const char *, int);
104 static void	usage(void);
105 
106 /* true if -E was specified on command line.  */
107 static bool	remove_empty_files = false;
108 
109 /* true if -R was specified on command line.  */
110 static bool	reverse_flag_specified = false;
111 
112 static bool	Vflag = false;
113 
114 /* buffer holding the name of the rejected patch file. */
115 static char	rejname[NAME_MAX + 1];
116 
117 /* how many input lines have been irretractibly output */
118 static LINENUM	last_frozen_line = 0;
119 
120 static int	Argc;		/* guess */
121 static char	**Argv;
122 static int	Argc_last;	/* for restarting plan_b */
123 static char	**Argv_last;
124 
125 static FILE	*ofp = NULL;	/* output file pointer */
126 static FILE	*rejfp = NULL;	/* reject file pointer */
127 
128 static int	filec = 0;	/* how many file arguments? */
129 static LINENUM	last_offset = 0;
130 static LINENUM	maxfuzz = 2;
131 
132 /* patch using ifdef, ifndef, etc. */
133 static bool		do_defines = false;
134 /* #ifdef xyzzy */
135 static char		if_defined[128];
136 /* #ifndef xyzzy */
137 static char		not_defined[128];
138 /* #else */
139 static const char	else_defined[] = "#else\n";
140 /* #endif xyzzy */
141 static char		end_defined[128];
142 
143 
144 /* Apply a set of diffs as appropriate. */
145 
146 int
147 main(int argc, char *argv[])
148 {
149 	int	error = 0, hunk, failed, i, fd;
150 	bool	patch_seen, reverse_seen;
151 	LINENUM	where = 0, newwhere, fuzz, mymaxfuzz;
152 	const	char *tmpdir;
153 	char	*v;
154 
155 	setvbuf(stdout, NULL, _IOLBF, 0);
156 	setvbuf(stderr, NULL, _IOLBF, 0);
157 	for (i = 0; i < MAXFILEC; i++)
158 		filearg[i] = NULL;
159 
160 	buf_size = INITLINELEN;
161 	buf = malloc((unsigned)(buf_size));
162 	if (buf == NULL)
163 		fatal("out of memory\n");
164 
165 	/* Cons up the names of the temporary files.  */
166 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
167 		tmpdir = _PATH_TMP;
168 	for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
169 		;
170 	i++;
171 	if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
172 		fatal("cannot allocate memory");
173 	if ((fd = mkstemp(TMPOUTNAME)) < 0)
174 		pfatal("can't create %s", TMPOUTNAME);
175 	close(fd);
176 
177 	if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
178 		fatal("cannot allocate memory");
179 	if ((fd = mkstemp(TMPINNAME)) < 0)
180 		pfatal("can't create %s", TMPINNAME);
181 	close(fd);
182 
183 	if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
184 		fatal("cannot allocate memory");
185 	if ((fd = mkstemp(TMPREJNAME)) < 0)
186 		pfatal("can't create %s", TMPREJNAME);
187 	close(fd);
188 
189 	if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
190 		fatal("cannot allocate memory");
191 	if ((fd = mkstemp(TMPPATNAME)) < 0)
192 		pfatal("can't create %s", TMPPATNAME);
193 	close(fd);
194 
195 	v = getenv("SIMPLE_BACKUP_SUFFIX");
196 	if (v)
197 		simple_backup_suffix = v;
198 	else
199 		simple_backup_suffix = ORIGEXT;
200 
201 	/* parse switches */
202 	Argc = argc;
203 	Argv = argv;
204 	get_some_switches();
205 
206 	if (!Vflag) {
207 		if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
208 			v = getenv("VERSION_CONTROL");
209 		if (v != NULL || !posix)
210 			backup_type = get_version(v);	/* OK to pass NULL. */
211 	}
212 
213 	/* make sure we clean up /tmp in case of disaster */
214 	set_signals(0);
215 
216 	patch_seen = false;
217 	for (open_patch_file(filearg[1]); there_is_another_patch();
218 	    reinitialize_almost_everything()) {
219 		/* for each patch in patch file */
220 
221 		patch_seen = true;
222 
223 		warn_on_invalid_line = true;
224 
225 		if (outname == NULL)
226 			outname = xstrdup(filearg[0]);
227 
228 		/* for ed script just up and do it and exit */
229 		if (diff_type == ED_DIFF) {
230 			do_ed_script();
231 			continue;
232 		}
233 		/* initialize the patched file */
234 		if (!skip_rest_of_patch)
235 			init_output(TMPOUTNAME);
236 
237 		/* initialize reject file */
238 		init_reject(TMPREJNAME);
239 
240 		/* find out where all the lines are */
241 		if (!skip_rest_of_patch)
242 			scan_input(filearg[0]);
243 
244 		/*
245 		 * from here on, open no standard i/o files, because
246 		 * malloc might misfire and we can't catch it easily
247 		 */
248 
249 		/* apply each hunk of patch */
250 		hunk = 0;
251 		failed = 0;
252 		reverse_seen = false;
253 		out_of_mem = false;
254 		while (another_hunk()) {
255 			hunk++;
256 			fuzz = 0;
257 			mymaxfuzz = pch_context();
258 			if (maxfuzz < mymaxfuzz)
259 				mymaxfuzz = maxfuzz;
260 			if (!skip_rest_of_patch) {
261 				do {
262 					where = locate_hunk(fuzz);
263 					if (hunk == 1 && where == 0 && !force && !reverse_seen) {
264 						/* dwim for reversed patch? */
265 						if (!pch_swap()) {
266 							if (fuzz == 0)
267 								say("Not enough memory to try swapped hunk!  Assuming unswapped.\n");
268 							continue;
269 						}
270 						reverse = !reverse;
271 						/* try again */
272 						where = locate_hunk(fuzz);
273 						if (where == 0) {
274 							/* didn't find it swapped */
275 							if (!pch_swap())
276 								/* put it back to normal */
277 								fatal("lost hunk on alloc error!\n");
278 							reverse = !reverse;
279 						} else if (noreverse) {
280 							if (!pch_swap())
281 								/* put it back to normal */
282 								fatal("lost hunk on alloc error!\n");
283 							reverse = !reverse;
284 							say("Ignoring previously applied (or reversed) patch.\n");
285 							skip_rest_of_patch = true;
286 						} else if (batch) {
287 							if (verbose)
288 								say("%seversed (or previously applied) patch detected!  %s -R.",
289 								    reverse ? "R" : "Unr",
290 								    reverse ? "Assuming" : "Ignoring");
291 						} else {
292 							ask("%seversed (or previously applied) patch detected!  %s -R? [y] ",
293 							    reverse ? "R" : "Unr",
294 							    reverse ? "Assume" : "Ignore");
295 							if (*buf == 'n') {
296 								ask("Apply anyway? [n] ");
297 								if (*buf != 'y')
298 									skip_rest_of_patch = true;
299 								else
300 									reverse_seen = true;
301 								where = 0;
302 								reverse = !reverse;
303 								if (!pch_swap())
304 									/* put it back to normal */
305 									fatal("lost hunk on alloc error!\n");
306 							}
307 						}
308 					}
309 				} while (!skip_rest_of_patch && where == 0 &&
310 				    ++fuzz <= mymaxfuzz);
311 
312 				if (skip_rest_of_patch) {	/* just got decided */
313 					if (ferror(ofp) || fclose(ofp)) {
314 						say("Error writing %s\n",
315 						    TMPOUTNAME);
316 						error = 1;
317 					}
318 					ofp = NULL;
319 				}
320 			}
321 			newwhere = pch_newfirst() + last_offset;
322 			if (skip_rest_of_patch) {
323 				abort_hunk();
324 				failed++;
325 				if (verbose)
326 					say("Hunk #%d ignored at %ld.\n",
327 					    hunk, newwhere);
328 			} else if (where == 0) {
329 				abort_hunk();
330 				failed++;
331 				if (verbose)
332 					say("Hunk #%d failed at %ld.\n",
333 					    hunk, newwhere);
334 			} else {
335 				apply_hunk(where);
336 				if (verbose) {
337 					say("Hunk #%d succeeded at %ld",
338 					    hunk, newwhere);
339 					if (fuzz != 0)
340 						say(" with fuzz %ld", fuzz);
341 					if (last_offset)
342 						say(" (offset %ld line%s)",
343 						    last_offset,
344 						    last_offset == 1L ? "" : "s");
345 					say(".\n");
346 				}
347 			}
348 		}
349 
350 		if (out_of_mem && using_plan_a) {
351 			Argc = Argc_last;
352 			Argv = Argv_last;
353 			say("\n\nRan out of memory using Plan A--trying again...\n\n");
354 			if (ofp)
355 				fclose(ofp);
356 			ofp = NULL;
357 			if (rejfp)
358 				fclose(rejfp);
359 			rejfp = NULL;
360 			continue;
361 		}
362 		if (hunk == 0)
363 			fatal("Internal error: hunk should not be 0\n");
364 
365 		/* finish spewing out the new file */
366 		if (!skip_rest_of_patch && !spew_output()) {
367 			say("Can't write %s\n", TMPOUTNAME);
368 			error = 1;
369 		}
370 
371 		/* and put the output where desired */
372 		ignore_signals();
373 		if (!skip_rest_of_patch) {
374 			struct stat	statbuf;
375 			char	*realout = outname;
376 
377 			if (!check_only) {
378 				if (move_file(TMPOUTNAME, outname) < 0) {
379 					toutkeep = true;
380 					realout = TMPOUTNAME;
381 					chmod(TMPOUTNAME, filemode);
382 				} else
383 					chmod(outname, filemode);
384 
385 				if (remove_empty_files &&
386 				    stat(realout, &statbuf) == 0 &&
387 				    statbuf.st_size == 0) {
388 					if (verbose)
389 						say("Removing %s (empty after patching).\n",
390 						    realout);
391 					unlink(realout);
392 				}
393 			}
394 		}
395 		if (ferror(rejfp) || fclose(rejfp)) {
396 			say("Error writing %s\n", rejname);
397 			error = 1;
398 		}
399 		rejfp = NULL;
400 		if (failed) {
401 			error = 1;
402 			if (*rejname == '\0') {
403 				if (strlcpy(rejname, outname,
404 				    sizeof(rejname)) >= sizeof(rejname))
405 					fatal("filename %s is too long\n", outname);
406 				if (strlcat(rejname, REJEXT,
407 				    sizeof(rejname)) >= sizeof(rejname))
408 					fatal("filename %s is too long\n", outname);
409 			}
410 			if (!check_only)
411 				say("%d out of %d hunks %s--saving rejects to %s\n",
412 				    failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname);
413 			else
414 				say("%d out of %d hunks %s while patching %s\n",
415 				    failed, hunk, skip_rest_of_patch ? "ignored" : "failed", filearg[0]);
416 			if (!check_only && move_file(TMPREJNAME, rejname) < 0)
417 				trejkeep = true;
418 		}
419 		set_signals(1);
420 	}
421 
422 	if (!patch_seen)
423 		error = 2;
424 
425 	my_exit(error);
426 	/* NOTREACHED */
427 }
428 
429 /* Prepare to find the next patch to do in the patch file. */
430 
431 static void
432 reinitialize_almost_everything(void)
433 {
434 	re_patch();
435 	re_input();
436 
437 	input_lines = 0;
438 	last_frozen_line = 0;
439 
440 	filec = 0;
441 	if (!out_of_mem) {
442 		free(filearg[0]);
443 		filearg[0] = NULL;
444 	}
445 
446 	free(outname);
447 	outname = NULL;
448 
449 	last_offset = 0;
450 	diff_type = 0;
451 
452 	free(revision);
453 	revision = NULL;
454 
455 	reverse = reverse_flag_specified;
456 	skip_rest_of_patch = false;
457 
458 	get_some_switches();
459 }
460 
461 /* Process switches and filenames. */
462 
463 static void
464 get_some_switches(void)
465 {
466 	const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
467 	static struct option longopts[] = {
468 		{"backup",		no_argument,		0,	'b'},
469 		{"batch",		no_argument,		0,	't'},
470 		{"check",		no_argument,		0,	'C'},
471 		{"context",		no_argument,		0,	'c'},
472 		{"debug",		required_argument,	0,	'x'},
473 		{"directory",		required_argument,	0,	'd'},
474 		{"dry-run",		no_argument,		0,	'C'},
475 		{"ed",			no_argument,		0,	'e'},
476 		{"force",		no_argument,		0,	'f'},
477 		{"forward",		no_argument,		0,	'N'},
478 		{"fuzz",		required_argument,	0,	'F'},
479 		{"ifdef",		required_argument,	0,	'D'},
480 		{"input",		required_argument,	0,	'i'},
481 		{"ignore-whitespace",	no_argument,		0,	'l'},
482 		{"normal",		no_argument,		0,	'n'},
483 		{"output",		required_argument,	0,	'o'},
484 		{"prefix",		required_argument,	0,	'B'},
485 		{"quiet",		no_argument,		0,	's'},
486 		{"reject-file",		required_argument,	0,	'r'},
487 		{"remove-empty-files",	no_argument,		0,	'E'},
488 		{"reverse",		no_argument,		0,	'R'},
489 		{"silent",		no_argument,		0,	's'},
490 		{"strip",		required_argument,	0,	'p'},
491 		{"suffix",		required_argument,	0,	'z'},
492 		{"unified",		no_argument,		0,	'u'},
493 		{"version",		no_argument,		0,	'v'},
494 		{"version-control",	required_argument,	0,	'V'},
495 		{"posix",		no_argument,		&posix,	1},
496 		{NULL,			0,			0,	0}
497 	};
498 	int ch;
499 
500 	rejname[0] = '\0';
501 	Argc_last = Argc;
502 	Argv_last = Argv;
503 	if (!Argc)
504 		return;
505 	optreset = optind = 1;
506 	while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
507 		switch (ch) {
508 		case 'b':
509 			if (backup_type == none)
510 				backup_type = numbered_existing;
511 			if (optarg == NULL)
512 				break;
513 			if (verbose)
514 				say("Warning, the ``-b suffix'' option has been"
515 				    " obsoleted by the -z option.\n");
516 			/* FALLTHROUGH */
517 		case 'z':
518 			/* must directly follow 'b' case for backwards compat */
519 			simple_backup_suffix = xstrdup(optarg);
520 			break;
521 		case 'B':
522 			origprae = xstrdup(optarg);
523 			break;
524 		case 'c':
525 			diff_type = CONTEXT_DIFF;
526 			break;
527 		case 'C':
528 			check_only = true;
529 			break;
530 		case 'd':
531 			if (chdir(optarg) < 0)
532 				pfatal("can't cd to %s", optarg);
533 			break;
534 		case 'D':
535 			do_defines = true;
536 			if (!isalpha((unsigned char)*optarg) && *optarg != '_')
537 				fatal("argument to -D is not an identifier\n");
538 			snprintf(if_defined, sizeof if_defined,
539 			    "#ifdef %s\n", optarg);
540 			snprintf(not_defined, sizeof not_defined,
541 			    "#ifndef %s\n", optarg);
542 			snprintf(end_defined, sizeof end_defined,
543 			    "#endif /* %s */\n", optarg);
544 			break;
545 		case 'e':
546 			diff_type = ED_DIFF;
547 			break;
548 		case 'E':
549 			remove_empty_files = true;
550 			break;
551 		case 'f':
552 			force = true;
553 			break;
554 		case 'F':
555 			maxfuzz = atoi(optarg);
556 			break;
557 		case 'i':
558 			if (++filec == MAXFILEC)
559 				fatal("too many file arguments\n");
560 			filearg[filec] = xstrdup(optarg);
561 			break;
562 		case 'l':
563 			canonicalize = true;
564 			break;
565 		case 'n':
566 			diff_type = NORMAL_DIFF;
567 			break;
568 		case 'N':
569 			noreverse = true;
570 			break;
571 		case 'o':
572 			outname = xstrdup(optarg);
573 			break;
574 		case 'p':
575 			strippath = atoi(optarg);
576 			break;
577 		case 'r':
578 			if (strlcpy(rejname, optarg,
579 			    sizeof(rejname)) >= sizeof(rejname))
580 				fatal("argument for -r is too long\n");
581 			break;
582 		case 'R':
583 			reverse = true;
584 			reverse_flag_specified = true;
585 			break;
586 		case 's':
587 			verbose = false;
588 			break;
589 		case 't':
590 			batch = true;
591 			break;
592 		case 'u':
593 			diff_type = UNI_DIFF;
594 			break;
595 		case 'v':
596 			version();
597 			break;
598 		case 'V':
599 			backup_type = get_version(optarg);
600 			Vflag = true;
601 			break;
602 #ifdef DEBUGGING
603 		case 'x':
604 			debug = atoi(optarg);
605 			break;
606 #endif
607 		default:
608 			if (ch != '\0')
609 				usage();
610 			break;
611 		}
612 	}
613 	Argc -= optind;
614 	Argv += optind;
615 
616 	if (Argc > 0) {
617 		filearg[0] = xstrdup(*Argv++);
618 		Argc--;
619 		while (Argc > 0) {
620 			if (++filec == MAXFILEC)
621 				fatal("too many file arguments\n");
622 			filearg[filec] = xstrdup(*Argv++);
623 			Argc--;
624 		}
625 	}
626 
627 	if (getenv("POSIXLY_CORRECT") != NULL)
628 		posix = 1;
629 }
630 
631 static void
632 usage(void)
633 {
634 	fprintf(stderr,
635 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
636 "             [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
637 "             [-r rej-name] [-V t | nil | never | none] [-x number]\n"
638 "             [-z backup-ext] [--posix] [origfile [patchfile]]\n"
639 "       patch <patchfile\n");
640 	my_exit(EXIT_FAILURE);
641 }
642 
643 /*
644  * Attempt to find the right place to apply this hunk of patch.
645  */
646 static LINENUM
647 locate_hunk(LINENUM fuzz)
648 {
649 	LINENUM	first_guess = pch_first() + last_offset;
650 	LINENUM	offset;
651 	LINENUM	pat_lines = pch_ptrn_lines();
652 	LINENUM	max_pos_offset = input_lines - first_guess - pat_lines + 1;
653 	LINENUM	max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
654 
655 	if (pat_lines == 0) {		/* null range matches always */
656 		if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
657 		    || diff_type == NEW_CONTEXT_DIFF
658 		    || diff_type == UNI_DIFF)) {
659 			say("Empty context always matches.\n");
660 		}
661 		return (first_guess);
662 	}
663 	if (max_neg_offset >= first_guess)	/* do not try lines < 0 */
664 		max_neg_offset = first_guess - 1;
665 	if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
666 		return first_guess;
667 	for (offset = 1; ; offset++) {
668 		bool	check_after = (offset <= max_pos_offset);
669 		bool	check_before = (offset <= max_neg_offset);
670 
671 		if (check_after && patch_match(first_guess, offset, fuzz)) {
672 #ifdef DEBUGGING
673 			if (debug & 1)
674 				say("Offset changing from %ld to %ld\n",
675 				    last_offset, offset);
676 #endif
677 			last_offset = offset;
678 			return first_guess + offset;
679 		} else if (check_before && patch_match(first_guess, -offset, fuzz)) {
680 #ifdef DEBUGGING
681 			if (debug & 1)
682 				say("Offset changing from %ld to %ld\n",
683 				    last_offset, -offset);
684 #endif
685 			last_offset = -offset;
686 			return first_guess - offset;
687 		} else if (!check_before && !check_after)
688 			return 0;
689 	}
690 }
691 
692 /* We did not find the pattern, dump out the hunk so they can handle it. */
693 
694 static void
695 abort_context_hunk(void)
696 {
697 	LINENUM	i;
698 	const LINENUM	pat_end = pch_end();
699 	/*
700 	 * add in last_offset to guess the same as the previous successful
701 	 * hunk
702 	 */
703 	const LINENUM	oldfirst = pch_first() + last_offset;
704 	const LINENUM	newfirst = pch_newfirst() + last_offset;
705 	const LINENUM	oldlast = oldfirst + pch_ptrn_lines() - 1;
706 	const LINENUM	newlast = newfirst + pch_repl_lines() - 1;
707 	const char	*stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
708 	const char	*minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
709 
710 	fprintf(rejfp, "***************\n");
711 	for (i = 0; i <= pat_end; i++) {
712 		switch (pch_char(i)) {
713 		case '*':
714 			if (oldlast < oldfirst)
715 				fprintf(rejfp, "*** 0%s\n", stars);
716 			else if (oldlast == oldfirst)
717 				fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
718 			else
719 				fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
720 				    oldlast, stars);
721 			break;
722 		case '=':
723 			if (newlast < newfirst)
724 				fprintf(rejfp, "--- 0%s\n", minuses);
725 			else if (newlast == newfirst)
726 				fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
727 			else
728 				fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
729 				    newlast, minuses);
730 			break;
731 		case '\n':
732 			fprintf(rejfp, "%s", pfetch(i));
733 			break;
734 		case ' ':
735 		case '-':
736 		case '+':
737 		case '!':
738 			fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
739 			break;
740 		default:
741 			fatal("fatal internal error in abort_context_hunk\n");
742 		}
743 	}
744 }
745 
746 static void
747 rej_line(int ch, LINENUM i)
748 {
749 	size_t len;
750 	const char *line = pfetch(i);
751 
752 	len = strlen(line);
753 
754 	fprintf(rejfp, "%c%s", ch, line);
755 	if (len == 0 || line[len - 1] != '\n') {
756 		if (len >= USHRT_MAX)
757 			fprintf(rejfp, "\n\\ Line too long\n");
758 		else
759 			fprintf(rejfp, "\n\\ No newline at end of line\n");
760 	}
761 }
762 
763 static void
764 abort_hunk(void)
765 {
766 	LINENUM		i, j, split;
767 	int		ch1, ch2;
768 	const LINENUM	pat_end = pch_end();
769 	const LINENUM	oldfirst = pch_first() + last_offset;
770 	const LINENUM	newfirst = pch_newfirst() + last_offset;
771 
772 	if (diff_type != UNI_DIFF) {
773 		abort_context_hunk();
774 		return;
775 	}
776 	split = -1;
777 	for (i = 0; i <= pat_end; i++) {
778 		if (pch_char(i) == '=') {
779 			split = i;
780 			break;
781 		}
782 	}
783 	if (split == -1) {
784 		fprintf(rejfp, "malformed hunk: no split found\n");
785 		return;
786 	}
787 	i = 0;
788 	j = split + 1;
789 	fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
790 	    pch_ptrn_lines() ? oldfirst : 0,
791 	    pch_ptrn_lines(), newfirst, pch_repl_lines());
792 	while (i < split || j <= pat_end) {
793 		ch1 = i < split ? pch_char(i) : -1;
794 		ch2 = j <= pat_end ? pch_char(j) : -1;
795 		if (ch1 == '-') {
796 			rej_line('-', i);
797 			i++;
798 		} else if (ch1 == ' ' && ch2 == ' ') {
799 			rej_line(' ', i);
800 			i++;
801 			j++;
802 		} else if (ch1 == '!' && ch2 == '!') {
803 			while (i < split && ch1 == '!') {
804 				rej_line('-', i);
805 				i++;
806 				ch1 = i < split ? pch_char(i) : -1;
807 			}
808 			while (j <= pat_end && ch2 == '!') {
809 				rej_line('+', j);
810 				j++;
811 				ch2 = j <= pat_end ? pch_char(j) : -1;
812 			}
813 		} else if (ch1 == '*') {
814 			i++;
815 		} else if (ch2 == '+' || ch2 == ' ') {
816 			rej_line(ch2, j);
817 			j++;
818 		} else {
819 			fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
820 			    i, split, j);
821 			rej_line(ch1, i);
822 			rej_line(ch2, j);
823 			return;
824 		}
825 	}
826 }
827 
828 /* We found where to apply it (we hope), so do it. */
829 
830 static void
831 apply_hunk(LINENUM where)
832 {
833 	LINENUM		old = 1;
834 	const LINENUM	lastline = pch_ptrn_lines();
835 	LINENUM		new = lastline + 1;
836 #define OUTSIDE 0
837 #define IN_IFNDEF 1
838 #define IN_IFDEF 2
839 #define IN_ELSE 3
840 	int		def_state = OUTSIDE;
841 	const LINENUM	pat_end = pch_end();
842 
843 	where--;
844 	while (pch_char(new) == '=' || pch_char(new) == '\n')
845 		new++;
846 
847 	while (old <= lastline) {
848 		if (pch_char(old) == '-') {
849 			copy_till(where + old - 1, false);
850 			if (do_defines) {
851 				if (def_state == OUTSIDE) {
852 					fputs(not_defined, ofp);
853 					def_state = IN_IFNDEF;
854 				} else if (def_state == IN_IFDEF) {
855 					fputs(else_defined, ofp);
856 					def_state = IN_ELSE;
857 				}
858 				fputs(pfetch(old), ofp);
859 			}
860 			last_frozen_line++;
861 			old++;
862 		} else if (new > pat_end) {
863 			break;
864 		} else if (pch_char(new) == '+') {
865 			copy_till(where + old - 1, false);
866 			if (do_defines) {
867 				if (def_state == IN_IFNDEF) {
868 					fputs(else_defined, ofp);
869 					def_state = IN_ELSE;
870 				} else if (def_state == OUTSIDE) {
871 					fputs(if_defined, ofp);
872 					def_state = IN_IFDEF;
873 				}
874 			}
875 			fputs(pfetch(new), ofp);
876 			new++;
877 		} else if (pch_char(new) != pch_char(old)) {
878 			say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
879 			    pch_hunk_beg() + old,
880 			    pch_hunk_beg() + new);
881 #ifdef DEBUGGING
882 			say("oldchar = '%c', newchar = '%c'\n",
883 			    pch_char(old), pch_char(new));
884 #endif
885 			my_exit(2);
886 		} else if (pch_char(new) == '!') {
887 			copy_till(where + old - 1, false);
888 			if (do_defines) {
889 				fputs(not_defined, ofp);
890 				def_state = IN_IFNDEF;
891 			}
892 			while (pch_char(old) == '!') {
893 				if (do_defines) {
894 					fputs(pfetch(old), ofp);
895 				}
896 				last_frozen_line++;
897 				old++;
898 			}
899 			if (do_defines) {
900 				fputs(else_defined, ofp);
901 				def_state = IN_ELSE;
902 			}
903 			while (pch_char(new) == '!') {
904 				fputs(pfetch(new), ofp);
905 				new++;
906 			}
907 		} else {
908 			if (pch_char(new) != ' ')
909 				fatal("Internal error: expected ' '\n");
910 			old++;
911 			new++;
912 			if (do_defines && def_state != OUTSIDE) {
913 				fputs(end_defined, ofp);
914 				def_state = OUTSIDE;
915 			}
916 		}
917 	}
918 	if (new <= pat_end && pch_char(new) == '+') {
919 		copy_till(where + old - 1, false);
920 		if (do_defines) {
921 			if (def_state == OUTSIDE) {
922 				fputs(if_defined, ofp);
923 				def_state = IN_IFDEF;
924 			} else if (def_state == IN_IFNDEF) {
925 				fputs(else_defined, ofp);
926 				def_state = IN_ELSE;
927 			}
928 		}
929 		while (new <= pat_end && pch_char(new) == '+') {
930 			fputs(pfetch(new), ofp);
931 			new++;
932 		}
933 	}
934 	if (do_defines && def_state != OUTSIDE) {
935 		fputs(end_defined, ofp);
936 	}
937 }
938 
939 /*
940  * Open the new file.
941  */
942 static void
943 init_output(const char *name)
944 {
945 	ofp = fopen(name, "w");
946 	if (ofp == NULL)
947 		pfatal("can't create %s", name);
948 }
949 
950 /*
951  * Open a file to put hunks we can't locate.
952  */
953 static void
954 init_reject(const char *name)
955 {
956 	rejfp = fopen(name, "w");
957 	if (rejfp == NULL)
958 		pfatal("can't create %s", name);
959 }
960 
961 /*
962  * Copy input file to output, up to wherever hunk is to be applied.
963  * If endoffile is true, treat the last line specially since it may
964  * lack a newline.
965  */
966 static void
967 copy_till(LINENUM lastline, bool endoffile)
968 {
969 	if (last_frozen_line > lastline)
970 		fatal("misordered hunks! output would be garbled\n");
971 	while (last_frozen_line < lastline) {
972 		if (++last_frozen_line == lastline && endoffile)
973 			dump_line(last_frozen_line, !last_line_missing_eol);
974 		else
975 			dump_line(last_frozen_line, true);
976 	}
977 }
978 
979 /*
980  * Finish copying the input file to the output file.
981  */
982 static bool
983 spew_output(void)
984 {
985 	int rv;
986 
987 #ifdef DEBUGGING
988 	if (debug & 256)
989 		say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
990 #endif
991 	if (input_lines)
992 		copy_till(input_lines, true);	/* dump remainder of file */
993 	rv = ferror(ofp) == 0 && fclose(ofp) == 0;
994 	ofp = NULL;
995 	return rv;
996 }
997 
998 /*
999  * Copy one line from input to output.
1000  */
1001 static void
1002 dump_line(LINENUM line, bool write_newline)
1003 {
1004 	char	*s;
1005 
1006 	s = ifetch(line, 0);
1007 	if (s == NULL)
1008 		return;
1009 	/* Note: string is not NUL terminated. */
1010 	for (; *s != '\n'; s++)
1011 		putc(*s, ofp);
1012 	if (write_newline)
1013 		putc('\n', ofp);
1014 }
1015 
1016 /*
1017  * Does the patch pattern match at line base+offset?
1018  */
1019 static bool
1020 patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
1021 {
1022 	LINENUM		pline = 1 + fuzz;
1023 	LINENUM		iline;
1024 	LINENUM		pat_lines = pch_ptrn_lines() - fuzz;
1025 	const char	*ilineptr;
1026 	const char	*plineptr;
1027 	unsigned short	plinelen;
1028 
1029 	/* Patch does not match if we don't have any more context to use */
1030 	if (pline > pat_lines)
1031 		return false;
1032 	for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
1033 		ilineptr = ifetch(iline, offset >= 0);
1034 		if (ilineptr == NULL)
1035 			return false;
1036 		plineptr = pfetch(pline);
1037 		plinelen = pch_line_len(pline);
1038 		if (canonicalize) {
1039 			if (!similar(ilineptr, plineptr, plinelen))
1040 				return false;
1041 		} else if (strnNE(ilineptr, plineptr, plinelen))
1042 			return false;
1043 		if (iline == input_lines) {
1044 			/*
1045 			 * We are looking at the last line of the file.
1046 			 * If the file has no eol, the patch line should
1047 			 * not have one either and vice-versa. Note that
1048 			 * plinelen > 0.
1049 			 */
1050 			if (last_line_missing_eol) {
1051 				if (plineptr[plinelen - 1] == '\n')
1052 					return false;
1053 			} else {
1054 				if (plineptr[plinelen - 1] != '\n')
1055 					return false;
1056 			}
1057 		}
1058 	}
1059 	return true;
1060 }
1061 
1062 /*
1063  * Do two lines match with canonicalized white space?
1064  */
1065 static bool
1066 similar(const char *a, const char *b, int len)
1067 {
1068 	while (len) {
1069 		if (isspace((unsigned char)*b)) {	/* whitespace (or \n) to match? */
1070 			if (!isspace((unsigned char)*a))	/* no corresponding whitespace? */
1071 				return false;
1072 			while (len && isspace((unsigned char)*b) && *b != '\n')
1073 				b++, len--;	/* skip pattern whitespace */
1074 			while (isspace((unsigned char)*a) && *a != '\n')
1075 				a++;	/* skip target whitespace */
1076 			if (*a == '\n' || *b == '\n')
1077 				return (*a == *b);	/* should end in sync */
1078 		} else if (*a++ != *b++)	/* match non-whitespace chars */
1079 			return false;
1080 		else
1081 			len--;	/* probably not necessary */
1082 	}
1083 	return true;		/* actually, this is not reached */
1084 	/* since there is always a \n */
1085 }
1086