xref: /freebsd/usr.bin/patch/patch.c (revision 851a9da38f070675c42a6d69c41c47a5d29ee3d0)
12dd076b8SGabor Kovesdan /*-
22dd076b8SGabor Kovesdan  * Copyright 1986, Larry Wall
32dd076b8SGabor Kovesdan  *
42dd076b8SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
52dd076b8SGabor Kovesdan  * modification, are permitted provided that the following condition is met:
62dd076b8SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright notice,
72dd076b8SGabor Kovesdan  * this condition and the following disclaimer.
82dd076b8SGabor Kovesdan  *
92dd076b8SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
102dd076b8SGabor Kovesdan  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
112dd076b8SGabor Kovesdan  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
122dd076b8SGabor Kovesdan  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
132dd076b8SGabor Kovesdan  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
142dd076b8SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
152dd076b8SGabor Kovesdan  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
162dd076b8SGabor Kovesdan  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
172dd076b8SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
182dd076b8SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
192dd076b8SGabor Kovesdan  * SUCH DAMAGE.
202dd076b8SGabor Kovesdan  *
212dd076b8SGabor Kovesdan  * patch - a program to apply diffs to original files
222dd076b8SGabor Kovesdan  *
232dd076b8SGabor Kovesdan  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
242dd076b8SGabor Kovesdan  * behaviour
252dd076b8SGabor Kovesdan  *
26547e0acbSPedro F. Giffuni  * $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $
272dd076b8SGabor Kovesdan  *
282dd076b8SGabor Kovesdan  */
292dd076b8SGabor Kovesdan 
302dd076b8SGabor Kovesdan #include <sys/types.h>
312dd076b8SGabor Kovesdan #include <sys/stat.h>
322dd076b8SGabor Kovesdan 
3350dacbf6SKyle Evans #include <assert.h>
342dd076b8SGabor Kovesdan #include <ctype.h>
352dd076b8SGabor Kovesdan #include <getopt.h>
362dd076b8SGabor Kovesdan #include <limits.h>
372dd076b8SGabor Kovesdan #include <stdio.h>
382dd076b8SGabor Kovesdan #include <string.h>
392dd076b8SGabor Kovesdan #include <stdlib.h>
402dd076b8SGabor Kovesdan #include <unistd.h>
412dd076b8SGabor Kovesdan 
422dd076b8SGabor Kovesdan #include "common.h"
432dd076b8SGabor Kovesdan #include "util.h"
442dd076b8SGabor Kovesdan #include "pch.h"
452dd076b8SGabor Kovesdan #include "inp.h"
462dd076b8SGabor Kovesdan #include "backupfile.h"
472dd076b8SGabor Kovesdan #include "pathnames.h"
482dd076b8SGabor Kovesdan 
492dd076b8SGabor Kovesdan mode_t		filemode = 0644;
502dd076b8SGabor Kovesdan 
512dd076b8SGabor Kovesdan char		*buf;			/* general purpose buffer */
522dd076b8SGabor Kovesdan size_t		buf_size;		/* size of the general purpose buffer */
532dd076b8SGabor Kovesdan 
542dd076b8SGabor Kovesdan bool		using_plan_a = true;	/* try to keep everything in memory */
552dd076b8SGabor Kovesdan bool		out_of_mem = false;	/* ran out of memory in plan a */
56ef30b5a8SKyle Evans bool		nonempty_patchf_seen = false;	/* seen nonempty patch file? */
572dd076b8SGabor Kovesdan 
582dd076b8SGabor Kovesdan #define MAXFILEC 2
592dd076b8SGabor Kovesdan 
602dd076b8SGabor Kovesdan char		*filearg[MAXFILEC];
612dd076b8SGabor Kovesdan bool		ok_to_create_file = false;
622dd076b8SGabor Kovesdan char		*outname = NULL;
632dd076b8SGabor Kovesdan char		*origprae = NULL;
642dd076b8SGabor Kovesdan char		*TMPOUTNAME;
652dd076b8SGabor Kovesdan char		*TMPINNAME;
662dd076b8SGabor Kovesdan char		*TMPREJNAME;
672dd076b8SGabor Kovesdan char		*TMPPATNAME;
682dd076b8SGabor Kovesdan bool		toutkeep = false;
692dd076b8SGabor Kovesdan bool		trejkeep = false;
702dd076b8SGabor Kovesdan bool		warn_on_invalid_line;
712dd076b8SGabor Kovesdan bool		last_line_missing_eol;
722dd076b8SGabor Kovesdan 
732dd076b8SGabor Kovesdan #ifdef DEBUGGING
742dd076b8SGabor Kovesdan int		debug = 0;
752dd076b8SGabor Kovesdan #endif
762dd076b8SGabor Kovesdan 
772dd076b8SGabor Kovesdan bool		force = false;
782dd076b8SGabor Kovesdan bool		batch = false;
792dd076b8SGabor Kovesdan bool		verbose = true;
802dd076b8SGabor Kovesdan bool		reverse = false;
812dd076b8SGabor Kovesdan bool		noreverse = false;
822dd076b8SGabor Kovesdan bool		skip_rest_of_patch = false;
832dd076b8SGabor Kovesdan int		strippath = 957;
842dd076b8SGabor Kovesdan bool		canonicalize = false;
852dd076b8SGabor Kovesdan bool		check_only = false;
862dd076b8SGabor Kovesdan int		diff_type = 0;
872dd076b8SGabor Kovesdan char		*revision = NULL;	/* prerequisite revision, if any */
882dd076b8SGabor Kovesdan LINENUM		input_lines = 0;	/* how long is input file in lines */
892dd076b8SGabor Kovesdan int		posix = 0;		/* strict POSIX mode? */
902dd076b8SGabor Kovesdan 
912dd076b8SGabor Kovesdan static void	reinitialize_almost_everything(void);
922dd076b8SGabor Kovesdan static void	get_some_switches(void);
932dd076b8SGabor Kovesdan static LINENUM	locate_hunk(LINENUM);
942dd076b8SGabor Kovesdan static void	abort_context_hunk(void);
952dd076b8SGabor Kovesdan static void	rej_line(int, LINENUM);
962dd076b8SGabor Kovesdan static void	abort_hunk(void);
972dd076b8SGabor Kovesdan static void	apply_hunk(LINENUM);
982dd076b8SGabor Kovesdan static void	init_output(const char *);
992dd076b8SGabor Kovesdan static void	init_reject(const char *);
1002dd076b8SGabor Kovesdan static void	copy_till(LINENUM, bool);
1012dd076b8SGabor Kovesdan static bool	spew_output(void);
1022dd076b8SGabor Kovesdan static void	dump_line(LINENUM, bool);
1032dd076b8SGabor Kovesdan static bool	patch_match(LINENUM, LINENUM, LINENUM);
1042dd076b8SGabor Kovesdan static bool	similar(const char *, const char *, int);
1052dd076b8SGabor Kovesdan static void	usage(void);
10650dacbf6SKyle Evans static bool	handle_creation(bool, bool *);
1072dd076b8SGabor Kovesdan 
1082dd076b8SGabor Kovesdan /* true if -E was specified on command line.  */
1092dd076b8SGabor Kovesdan static bool	remove_empty_files = false;
1102dd076b8SGabor Kovesdan 
1112dd076b8SGabor Kovesdan /* true if -R was specified on command line.  */
1122dd076b8SGabor Kovesdan static bool	reverse_flag_specified = false;
1132dd076b8SGabor Kovesdan 
114300ca9a8SConrad Meyer static bool	Vflag = false;
115300ca9a8SConrad Meyer 
1162dd076b8SGabor Kovesdan /* buffer holding the name of the rejected patch file. */
117c844f14eSPedro F. Giffuni static char	rejname[PATH_MAX];
1182dd076b8SGabor Kovesdan 
1192dd076b8SGabor Kovesdan /* how many input lines have been irretractibly output */
1202dd076b8SGabor Kovesdan static LINENUM	last_frozen_line = 0;
1212dd076b8SGabor Kovesdan 
1222dd076b8SGabor Kovesdan static int	Argc;		/* guess */
1232dd076b8SGabor Kovesdan static char	**Argv;
1242dd076b8SGabor Kovesdan static int	Argc_last;	/* for restarting plan_b */
1252dd076b8SGabor Kovesdan static char	**Argv_last;
1262dd076b8SGabor Kovesdan 
1272dd076b8SGabor Kovesdan static FILE	*ofp = NULL;	/* output file pointer */
1282dd076b8SGabor Kovesdan static FILE	*rejfp = NULL;	/* reject file pointer */
1292dd076b8SGabor Kovesdan 
1302dd076b8SGabor Kovesdan static int	filec = 0;	/* how many file arguments? */
1312dd076b8SGabor Kovesdan static LINENUM	last_offset = 0;
1322dd076b8SGabor Kovesdan static LINENUM	maxfuzz = 2;
1332dd076b8SGabor Kovesdan 
1342dd076b8SGabor Kovesdan /* patch using ifdef, ifndef, etc. */
1352dd076b8SGabor Kovesdan static bool		do_defines = false;
1362dd076b8SGabor Kovesdan /* #ifdef xyzzy */
1372dd076b8SGabor Kovesdan static char		if_defined[128];
1382dd076b8SGabor Kovesdan /* #ifndef xyzzy */
1392dd076b8SGabor Kovesdan static char		not_defined[128];
1402dd076b8SGabor Kovesdan /* #else */
1412dd076b8SGabor Kovesdan static const char	else_defined[] = "#else\n";
1422dd076b8SGabor Kovesdan /* #endif xyzzy */
1432dd076b8SGabor Kovesdan static char		end_defined[128];
1442dd076b8SGabor Kovesdan 
1452dd076b8SGabor Kovesdan 
1462dd076b8SGabor Kovesdan /* Apply a set of diffs as appropriate. */
1472dd076b8SGabor Kovesdan 
1482dd076b8SGabor Kovesdan int
main(int argc,char * argv[])1492dd076b8SGabor Kovesdan main(int argc, char *argv[])
1502dd076b8SGabor Kovesdan {
15150dacbf6SKyle Evans 	struct stat statbuf;
1522dd076b8SGabor Kovesdan 	int	error = 0, hunk, failed, i, fd;
15350dacbf6SKyle Evans 	bool	out_creating, out_existed, patch_seen, remove_file;
15450dacbf6SKyle Evans 	bool	reverse_seen;
1552dd076b8SGabor Kovesdan 	LINENUM	where = 0, newwhere, fuzz, mymaxfuzz;
1562dd076b8SGabor Kovesdan 	const	char *tmpdir;
1572dd076b8SGabor Kovesdan 	char	*v;
1582dd076b8SGabor Kovesdan 
159ab761cdbSPedro F. Giffuni 	setvbuf(stdout, NULL, _IOLBF, 0);
160ab761cdbSPedro F. Giffuni 	setvbuf(stderr, NULL, _IOLBF, 0);
1612dd076b8SGabor Kovesdan 	for (i = 0; i < MAXFILEC; i++)
1622dd076b8SGabor Kovesdan 		filearg[i] = NULL;
1632dd076b8SGabor Kovesdan 
1642dd076b8SGabor Kovesdan 	buf_size = INITLINELEN;
1652dd076b8SGabor Kovesdan 	buf = malloc((unsigned)(buf_size));
1662dd076b8SGabor Kovesdan 	if (buf == NULL)
1672dd076b8SGabor Kovesdan 		fatal("out of memory\n");
1682dd076b8SGabor Kovesdan 
1692dd076b8SGabor Kovesdan 	/* Cons up the names of the temporary files.  */
1702dd076b8SGabor Kovesdan 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
1712dd076b8SGabor Kovesdan 		tmpdir = _PATH_TMP;
1722dd076b8SGabor Kovesdan 	for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
1732dd076b8SGabor Kovesdan 		;
1742dd076b8SGabor Kovesdan 	i++;
1752dd076b8SGabor Kovesdan 	if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
1762dd076b8SGabor Kovesdan 		fatal("cannot allocate memory");
1772dd076b8SGabor Kovesdan 	if ((fd = mkstemp(TMPOUTNAME)) < 0)
1782dd076b8SGabor Kovesdan 		pfatal("can't create %s", TMPOUTNAME);
1792dd076b8SGabor Kovesdan 	close(fd);
1802dd076b8SGabor Kovesdan 
1812dd076b8SGabor Kovesdan 	if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
1822dd076b8SGabor Kovesdan 		fatal("cannot allocate memory");
1832dd076b8SGabor Kovesdan 	if ((fd = mkstemp(TMPINNAME)) < 0)
1842dd076b8SGabor Kovesdan 		pfatal("can't create %s", TMPINNAME);
1852dd076b8SGabor Kovesdan 	close(fd);
1862dd076b8SGabor Kovesdan 
1872dd076b8SGabor Kovesdan 	if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
1882dd076b8SGabor Kovesdan 		fatal("cannot allocate memory");
1892dd076b8SGabor Kovesdan 	if ((fd = mkstemp(TMPREJNAME)) < 0)
1902dd076b8SGabor Kovesdan 		pfatal("can't create %s", TMPREJNAME);
1912dd076b8SGabor Kovesdan 	close(fd);
1922dd076b8SGabor Kovesdan 
1932dd076b8SGabor Kovesdan 	if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
1942dd076b8SGabor Kovesdan 		fatal("cannot allocate memory");
1952dd076b8SGabor Kovesdan 	if ((fd = mkstemp(TMPPATNAME)) < 0)
1962dd076b8SGabor Kovesdan 		pfatal("can't create %s", TMPPATNAME);
1972dd076b8SGabor Kovesdan 	close(fd);
1982dd076b8SGabor Kovesdan 
1992dd076b8SGabor Kovesdan 	v = getenv("SIMPLE_BACKUP_SUFFIX");
2002dd076b8SGabor Kovesdan 	if (v)
2012dd076b8SGabor Kovesdan 		simple_backup_suffix = v;
2022dd076b8SGabor Kovesdan 	else
2032dd076b8SGabor Kovesdan 		simple_backup_suffix = ORIGEXT;
2042dd076b8SGabor Kovesdan 
2052dd076b8SGabor Kovesdan 	/* parse switches */
2062dd076b8SGabor Kovesdan 	Argc = argc;
2072dd076b8SGabor Kovesdan 	Argv = argv;
2082dd076b8SGabor Kovesdan 	get_some_switches();
2092dd076b8SGabor Kovesdan 
210300ca9a8SConrad Meyer 	if (!Vflag) {
2112dd076b8SGabor Kovesdan 		if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
2122dd076b8SGabor Kovesdan 			v = getenv("VERSION_CONTROL");
2132dd076b8SGabor Kovesdan 		if (v != NULL || !posix)
2142dd076b8SGabor Kovesdan 			backup_type = get_version(v);	/* OK to pass NULL. */
2152dd076b8SGabor Kovesdan 	}
2162dd076b8SGabor Kovesdan 
2172dd076b8SGabor Kovesdan 	/* make sure we clean up /tmp in case of disaster */
2182dd076b8SGabor Kovesdan 	set_signals(0);
2192dd076b8SGabor Kovesdan 
220e56ef7d3SXin LI 	patch_seen = false;
2212dd076b8SGabor Kovesdan 	for (open_patch_file(filearg[1]); there_is_another_patch();
2222dd076b8SGabor Kovesdan 	    reinitialize_almost_everything()) {
2232dd076b8SGabor Kovesdan 		/* for each patch in patch file */
2242dd076b8SGabor Kovesdan 
22550dacbf6SKyle Evans 		if (source_file != NULL && (diff_type == CONTEXT_DIFF ||
22650dacbf6SKyle Evans 		    diff_type == NEW_CONTEXT_DIFF ||
22750dacbf6SKyle Evans 		    diff_type == UNI_DIFF))
22850dacbf6SKyle Evans 			out_creating = strcmp(source_file, _PATH_DEVNULL) == 0;
22950dacbf6SKyle Evans 		else
23050dacbf6SKyle Evans 			out_creating = false;
231e56ef7d3SXin LI 		patch_seen = true;
232e56ef7d3SXin LI 
2332dd076b8SGabor Kovesdan 		warn_on_invalid_line = true;
2342dd076b8SGabor Kovesdan 
2352dd076b8SGabor Kovesdan 		if (outname == NULL)
236547e0acbSPedro F. Giffuni 			outname = xstrdup(filearg[0]);
2372dd076b8SGabor Kovesdan 
23850dacbf6SKyle Evans 		/*
23950dacbf6SKyle Evans 		 * At this point, we know if we're supposed to be creating the
24050dacbf6SKyle Evans 		 * file and we know if we should be trying to handle a conflict
24150dacbf6SKyle Evans 		 * between the patch and the file already existing.  We defer
24250dacbf6SKyle Evans 		 * handling it until hunk processing because we want to swap
24350dacbf6SKyle Evans 		 * the hunk if they opt to reverse it, but we want to make sure
24450dacbf6SKyle Evans 		 * we *can* swap the hunk without running into memory issues
24550dacbf6SKyle Evans 		 * before we offer it.  We also want to be verbose if flags or
24650dacbf6SKyle Evans 		 * user decision cause us to skip -- this is explained a little
24750dacbf6SKyle Evans 		 * more later.
24850dacbf6SKyle Evans 		 */
24950dacbf6SKyle Evans 		out_existed = stat(outname, &statbuf) == 0;
25050dacbf6SKyle Evans 
2512dd076b8SGabor Kovesdan 		/* for ed script just up and do it and exit */
2522dd076b8SGabor Kovesdan 		if (diff_type == ED_DIFF) {
2532dd076b8SGabor Kovesdan 			do_ed_script();
2542dd076b8SGabor Kovesdan 			continue;
2552dd076b8SGabor Kovesdan 		}
2562dd076b8SGabor Kovesdan 		/* initialize the patched file */
2572dd076b8SGabor Kovesdan 		if (!skip_rest_of_patch)
2582dd076b8SGabor Kovesdan 			init_output(TMPOUTNAME);
2592dd076b8SGabor Kovesdan 
2602dd076b8SGabor Kovesdan 		/* initialize reject file */
2612dd076b8SGabor Kovesdan 		init_reject(TMPREJNAME);
2622dd076b8SGabor Kovesdan 
2632dd076b8SGabor Kovesdan 		/* find out where all the lines are */
2642dd076b8SGabor Kovesdan 		if (!skip_rest_of_patch)
2652dd076b8SGabor Kovesdan 			scan_input(filearg[0]);
2662dd076b8SGabor Kovesdan 
267b9740ba1SPedro F. Giffuni 		/*
268b9740ba1SPedro F. Giffuni 		 * from here on, open no standard i/o files, because
269b9740ba1SPedro F. Giffuni 		 * malloc might misfire and we can't catch it easily
270b9740ba1SPedro F. Giffuni 		 */
2712dd076b8SGabor Kovesdan 
2722dd076b8SGabor Kovesdan 		/* apply each hunk of patch */
2732dd076b8SGabor Kovesdan 		hunk = 0;
2742dd076b8SGabor Kovesdan 		failed = 0;
275e11cd3bcSXin LI 		reverse_seen = false;
2762dd076b8SGabor Kovesdan 		out_of_mem = false;
27750dacbf6SKyle Evans 		remove_file = false;
2782dd076b8SGabor Kovesdan 		while (another_hunk()) {
27950dacbf6SKyle Evans 			assert(!out_creating || hunk == 0);
2802dd076b8SGabor Kovesdan 			hunk++;
2812dd076b8SGabor Kovesdan 			fuzz = 0;
28250dacbf6SKyle Evans 
28350dacbf6SKyle Evans 			/*
28450dacbf6SKyle Evans 			 * There are only three cases in handle_creation() that
28550dacbf6SKyle Evans 			 * results in us skipping hunk location, in order:
28650dacbf6SKyle Evans 			 *
28750dacbf6SKyle Evans 			 * 1.) Potentially reversed but -f/--force'd,
28850dacbf6SKyle Evans 			 * 2.) Potentially reversed but -N/--forward'd
28950dacbf6SKyle Evans 			 * 3.) Reversed and the user's opted to not apply it.
29050dacbf6SKyle Evans 			 *
29150dacbf6SKyle Evans 			 * In all three cases, we still want to inform the user
29250dacbf6SKyle Evans 			 * that we're ignoring it in the standard way, which is
29350dacbf6SKyle Evans 			 * also tied to this hunk processing loop.
29450dacbf6SKyle Evans 			 */
29550dacbf6SKyle Evans 			if (out_creating)
29650dacbf6SKyle Evans 				reverse_seen = handle_creation(out_existed,
29750dacbf6SKyle Evans 				    &remove_file);
29850dacbf6SKyle Evans 
2992dd076b8SGabor Kovesdan 			mymaxfuzz = pch_context();
3002dd076b8SGabor Kovesdan 			if (maxfuzz < mymaxfuzz)
3012dd076b8SGabor Kovesdan 				mymaxfuzz = maxfuzz;
3022dd076b8SGabor Kovesdan 			if (!skip_rest_of_patch) {
3032dd076b8SGabor Kovesdan 				do {
3042dd076b8SGabor Kovesdan 					where = locate_hunk(fuzz);
305e11cd3bcSXin LI 					if (hunk == 1 && where == 0 && !force && !reverse_seen) {
3062dd076b8SGabor Kovesdan 						/* dwim for reversed patch? */
3072dd076b8SGabor Kovesdan 						if (!pch_swap()) {
3082dd076b8SGabor Kovesdan 							if (fuzz == 0)
3092dd076b8SGabor Kovesdan 								say("Not enough memory to try swapped hunk!  Assuming unswapped.\n");
3102dd076b8SGabor Kovesdan 							continue;
3112dd076b8SGabor Kovesdan 						}
3122dd076b8SGabor Kovesdan 						reverse = !reverse;
3132dd076b8SGabor Kovesdan 						/* try again */
3142dd076b8SGabor Kovesdan 						where = locate_hunk(fuzz);
3152dd076b8SGabor Kovesdan 						if (where == 0) {
3162dd076b8SGabor Kovesdan 							/* didn't find it swapped */
3172dd076b8SGabor Kovesdan 							if (!pch_swap())
3182dd076b8SGabor Kovesdan 								/* put it back to normal */
3192dd076b8SGabor Kovesdan 								fatal("lost hunk on alloc error!\n");
3202dd076b8SGabor Kovesdan 							reverse = !reverse;
3212dd076b8SGabor Kovesdan 						} else if (noreverse) {
3222dd076b8SGabor Kovesdan 							if (!pch_swap())
3232dd076b8SGabor Kovesdan 								/* put it back to normal */
3242dd076b8SGabor Kovesdan 								fatal("lost hunk on alloc error!\n");
3252dd076b8SGabor Kovesdan 							reverse = !reverse;
3262dd076b8SGabor Kovesdan 							say("Ignoring previously applied (or reversed) patch.\n");
3272dd076b8SGabor Kovesdan 							skip_rest_of_patch = true;
3282dd076b8SGabor Kovesdan 						} else if (batch) {
3292dd076b8SGabor Kovesdan 							if (verbose)
3302dd076b8SGabor Kovesdan 								say("%seversed (or previously applied) patch detected!  %s -R.",
3312dd076b8SGabor Kovesdan 								    reverse ? "R" : "Unr",
3322dd076b8SGabor Kovesdan 								    reverse ? "Assuming" : "Ignoring");
3332dd076b8SGabor Kovesdan 						} else {
3342dd076b8SGabor Kovesdan 							ask("%seversed (or previously applied) patch detected!  %s -R? [y] ",
3352dd076b8SGabor Kovesdan 							    reverse ? "R" : "Unr",
3362dd076b8SGabor Kovesdan 							    reverse ? "Assume" : "Ignore");
3372dd076b8SGabor Kovesdan 							if (*buf == 'n') {
3382dd076b8SGabor Kovesdan 								ask("Apply anyway? [n] ");
3392dd076b8SGabor Kovesdan 								if (*buf != 'y')
3402dd076b8SGabor Kovesdan 									skip_rest_of_patch = true;
341e11cd3bcSXin LI 								else
342e11cd3bcSXin LI 									reverse_seen = true;
3432dd076b8SGabor Kovesdan 								where = 0;
3442dd076b8SGabor Kovesdan 								reverse = !reverse;
3452dd076b8SGabor Kovesdan 								if (!pch_swap())
3462dd076b8SGabor Kovesdan 									/* put it back to normal */
3472dd076b8SGabor Kovesdan 									fatal("lost hunk on alloc error!\n");
3482dd076b8SGabor Kovesdan 							}
3492dd076b8SGabor Kovesdan 						}
3502dd076b8SGabor Kovesdan 					}
3512dd076b8SGabor Kovesdan 				} while (!skip_rest_of_patch && where == 0 &&
3522dd076b8SGabor Kovesdan 				    ++fuzz <= mymaxfuzz);
3532dd076b8SGabor Kovesdan 
3542dd076b8SGabor Kovesdan 				if (skip_rest_of_patch) {	/* just got decided */
355ffca5883SGlen Barber 					if (ferror(ofp) || fclose(ofp)) {
3562dd076b8SGabor Kovesdan 						say("Error writing %s\n",
3572dd076b8SGabor Kovesdan 						    TMPOUTNAME);
3582dd076b8SGabor Kovesdan 						error = 1;
3592dd076b8SGabor Kovesdan 					}
3602dd076b8SGabor Kovesdan 					ofp = NULL;
3612dd076b8SGabor Kovesdan 				}
3622dd076b8SGabor Kovesdan 			}
3632dd076b8SGabor Kovesdan 			newwhere = pch_newfirst() + last_offset;
3642dd076b8SGabor Kovesdan 			if (skip_rest_of_patch) {
3652dd076b8SGabor Kovesdan 				abort_hunk();
3662dd076b8SGabor Kovesdan 				failed++;
3672dd076b8SGabor Kovesdan 				if (verbose)
3682dd076b8SGabor Kovesdan 					say("Hunk #%d ignored at %ld.\n",
3692dd076b8SGabor Kovesdan 					    hunk, newwhere);
3702dd076b8SGabor Kovesdan 			} else if (where == 0) {
3712dd076b8SGabor Kovesdan 				abort_hunk();
3722dd076b8SGabor Kovesdan 				failed++;
3732dd076b8SGabor Kovesdan 				if (verbose)
3742dd076b8SGabor Kovesdan 					say("Hunk #%d failed at %ld.\n",
3752dd076b8SGabor Kovesdan 					    hunk, newwhere);
3762dd076b8SGabor Kovesdan 			} else {
3772dd076b8SGabor Kovesdan 				apply_hunk(where);
3782dd076b8SGabor Kovesdan 				if (verbose) {
3792dd076b8SGabor Kovesdan 					say("Hunk #%d succeeded at %ld",
3802dd076b8SGabor Kovesdan 					    hunk, newwhere);
3812dd076b8SGabor Kovesdan 					if (fuzz != 0)
3822dd076b8SGabor Kovesdan 						say(" with fuzz %ld", fuzz);
3832dd076b8SGabor Kovesdan 					if (last_offset)
3842dd076b8SGabor Kovesdan 						say(" (offset %ld line%s)",
3852dd076b8SGabor Kovesdan 						    last_offset,
3862dd076b8SGabor Kovesdan 						    last_offset == 1L ? "" : "s");
3872dd076b8SGabor Kovesdan 					say(".\n");
3882dd076b8SGabor Kovesdan 				}
3892dd076b8SGabor Kovesdan 			}
3902dd076b8SGabor Kovesdan 		}
3912dd076b8SGabor Kovesdan 
3922dd076b8SGabor Kovesdan 		if (out_of_mem && using_plan_a) {
3932dd076b8SGabor Kovesdan 			Argc = Argc_last;
3942dd076b8SGabor Kovesdan 			Argv = Argv_last;
3952dd076b8SGabor Kovesdan 			say("\n\nRan out of memory using Plan A--trying again...\n\n");
3962dd076b8SGabor Kovesdan 			if (ofp)
3972dd076b8SGabor Kovesdan 				fclose(ofp);
3982dd076b8SGabor Kovesdan 			ofp = NULL;
3992dd076b8SGabor Kovesdan 			if (rejfp)
4002dd076b8SGabor Kovesdan 				fclose(rejfp);
4012dd076b8SGabor Kovesdan 			rejfp = NULL;
4022dd076b8SGabor Kovesdan 			continue;
4032dd076b8SGabor Kovesdan 		}
4042dd076b8SGabor Kovesdan 		if (hunk == 0)
4052dd076b8SGabor Kovesdan 			fatal("Internal error: hunk should not be 0\n");
4062dd076b8SGabor Kovesdan 
4072dd076b8SGabor Kovesdan 		/* finish spewing out the new file */
4082dd076b8SGabor Kovesdan 		if (!skip_rest_of_patch && !spew_output()) {
4092dd076b8SGabor Kovesdan 			say("Can't write %s\n", TMPOUTNAME);
4102dd076b8SGabor Kovesdan 			error = 1;
4112dd076b8SGabor Kovesdan 		}
4122dd076b8SGabor Kovesdan 
4132dd076b8SGabor Kovesdan 		/* and put the output where desired */
4142dd076b8SGabor Kovesdan 		ignore_signals();
4152dd076b8SGabor Kovesdan 		if (!skip_rest_of_patch) {
4162dd076b8SGabor Kovesdan 			char	*realout = outname;
4172dd076b8SGabor Kovesdan 
4182dd076b8SGabor Kovesdan 			if (!check_only) {
4192dd076b8SGabor Kovesdan 				if (move_file(TMPOUTNAME, outname) < 0) {
4202dd076b8SGabor Kovesdan 					toutkeep = true;
4212dd076b8SGabor Kovesdan 					realout = TMPOUTNAME;
4222dd076b8SGabor Kovesdan 					chmod(TMPOUTNAME, filemode);
4232dd076b8SGabor Kovesdan 				} else
4242dd076b8SGabor Kovesdan 					chmod(outname, filemode);
4252dd076b8SGabor Kovesdan 
42650dacbf6SKyle Evans 				/*
42750dacbf6SKyle Evans 				 * remove_file is a per-patch flag indicating
42850dacbf6SKyle Evans 				 * whether it's OK to remove the empty file.
42950dacbf6SKyle Evans 				 * This is specifically set when we're reversing
43050dacbf6SKyle Evans 				 * the creation of a file and it ends up empty.
43150dacbf6SKyle Evans 				 * This is an exception to the global policy
43250dacbf6SKyle Evans 				 * (remove_empty_files) because the user would
43350dacbf6SKyle Evans 				 * likely not expect the reverse of file
43450dacbf6SKyle Evans 				 * creation to leave an empty file laying
43550dacbf6SKyle Evans 				 * around.
43650dacbf6SKyle Evans 				 */
43750dacbf6SKyle Evans 				if ((remove_empty_files || remove_file) &&
4382dd076b8SGabor Kovesdan 				    stat(realout, &statbuf) == 0 &&
4392dd076b8SGabor Kovesdan 				    statbuf.st_size == 0) {
4402dd076b8SGabor Kovesdan 					if (verbose)
4412dd076b8SGabor Kovesdan 						say("Removing %s (empty after patching).\n",
4422dd076b8SGabor Kovesdan 						    realout);
4432dd076b8SGabor Kovesdan 					unlink(realout);
4442dd076b8SGabor Kovesdan 				}
4452dd076b8SGabor Kovesdan 			}
4462dd076b8SGabor Kovesdan 		}
447ffca5883SGlen Barber 		if (ferror(rejfp) || fclose(rejfp)) {
4482dd076b8SGabor Kovesdan 			say("Error writing %s\n", rejname);
4492dd076b8SGabor Kovesdan 			error = 1;
4502dd076b8SGabor Kovesdan 		}
4512dd076b8SGabor Kovesdan 		rejfp = NULL;
4522dd076b8SGabor Kovesdan 		if (failed) {
4532dd076b8SGabor Kovesdan 			error = 1;
4542dd076b8SGabor Kovesdan 			if (*rejname == '\0') {
4552dd076b8SGabor Kovesdan 				if (strlcpy(rejname, outname,
4562dd076b8SGabor Kovesdan 				    sizeof(rejname)) >= sizeof(rejname))
4572dd076b8SGabor Kovesdan 					fatal("filename %s is too long\n", outname);
4582dd076b8SGabor Kovesdan 				if (strlcat(rejname, REJEXT,
4592dd076b8SGabor Kovesdan 				    sizeof(rejname)) >= sizeof(rejname))
4602dd076b8SGabor Kovesdan 					fatal("filename %s is too long\n", outname);
4612dd076b8SGabor Kovesdan 			}
462e56ef7d3SXin LI 			if (!check_only)
463e56ef7d3SXin LI 				say("%d out of %d hunks %s--saving rejects to %s\n",
464e56ef7d3SXin LI 				    failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname);
4657e688ed4SKyle Evans 			else if (filearg[0] != NULL)
466e11cd3bcSXin LI 				say("%d out of %d hunks %s while patching %s\n",
467e11cd3bcSXin LI 				    failed, hunk, skip_rest_of_patch ? "ignored" : "failed", filearg[0]);
4687e688ed4SKyle Evans 			else
4697e688ed4SKyle Evans 				/* File prompt ignored, just note # hunks. */
4707e688ed4SKyle Evans 				say("%d out of %d hunks %s\n",
4717e688ed4SKyle Evans 				    failed, hunk, skip_rest_of_patch ? "ignored" : "failed");
4722dd076b8SGabor Kovesdan 			if (!check_only && move_file(TMPREJNAME, rejname) < 0)
4732dd076b8SGabor Kovesdan 				trejkeep = true;
4742dd076b8SGabor Kovesdan 		}
4752dd076b8SGabor Kovesdan 		set_signals(1);
4762dd076b8SGabor Kovesdan 	}
477e56ef7d3SXin LI 
478ef30b5a8SKyle Evans 	if (!patch_seen && nonempty_patchf_seen)
479e56ef7d3SXin LI 		error = 2;
480e56ef7d3SXin LI 
4812dd076b8SGabor Kovesdan 	my_exit(error);
4822dd076b8SGabor Kovesdan 	/* NOTREACHED */
4832dd076b8SGabor Kovesdan }
4842dd076b8SGabor Kovesdan 
4852dd076b8SGabor Kovesdan /* Prepare to find the next patch to do in the patch file. */
4862dd076b8SGabor Kovesdan 
4872dd076b8SGabor Kovesdan static void
reinitialize_almost_everything(void)4882dd076b8SGabor Kovesdan reinitialize_almost_everything(void)
4892dd076b8SGabor Kovesdan {
4902dd076b8SGabor Kovesdan 	re_patch();
4912dd076b8SGabor Kovesdan 	re_input();
4922dd076b8SGabor Kovesdan 
4932dd076b8SGabor Kovesdan 	input_lines = 0;
4942dd076b8SGabor Kovesdan 	last_frozen_line = 0;
4952dd076b8SGabor Kovesdan 
4962dd076b8SGabor Kovesdan 	filec = 0;
4972dd076b8SGabor Kovesdan 	if (!out_of_mem) {
4982dd076b8SGabor Kovesdan 		free(filearg[0]);
4992dd076b8SGabor Kovesdan 		filearg[0] = NULL;
5002dd076b8SGabor Kovesdan 	}
5012dd076b8SGabor Kovesdan 
50250dacbf6SKyle Evans 	free(source_file);
50350dacbf6SKyle Evans 	source_file = NULL;
50450dacbf6SKyle Evans 
5052dd076b8SGabor Kovesdan 	free(outname);
5062dd076b8SGabor Kovesdan 	outname = NULL;
5072dd076b8SGabor Kovesdan 
5082dd076b8SGabor Kovesdan 	last_offset = 0;
5092dd076b8SGabor Kovesdan 	diff_type = 0;
5102dd076b8SGabor Kovesdan 
5112dd076b8SGabor Kovesdan 	free(revision);
5122dd076b8SGabor Kovesdan 	revision = NULL;
5132dd076b8SGabor Kovesdan 
5142dd076b8SGabor Kovesdan 	reverse = reverse_flag_specified;
5152dd076b8SGabor Kovesdan 	skip_rest_of_patch = false;
5162dd076b8SGabor Kovesdan 
5172dd076b8SGabor Kovesdan 	get_some_switches();
5182dd076b8SGabor Kovesdan }
5192dd076b8SGabor Kovesdan 
5202dd076b8SGabor Kovesdan /* Process switches and filenames. */
5212dd076b8SGabor Kovesdan 
5222dd076b8SGabor Kovesdan static void
get_some_switches(void)5232dd076b8SGabor Kovesdan get_some_switches(void)
5242dd076b8SGabor Kovesdan {
5252dd076b8SGabor Kovesdan 	const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
5262dd076b8SGabor Kovesdan 	static struct option longopts[] = {
5272dd076b8SGabor Kovesdan 		{"backup",		no_argument,		0,	'b'},
5282dd076b8SGabor Kovesdan 		{"batch",		no_argument,		0,	't'},
5292dd076b8SGabor Kovesdan 		{"check",		no_argument,		0,	'C'},
5302dd076b8SGabor Kovesdan 		{"context",		no_argument,		0,	'c'},
5312dd076b8SGabor Kovesdan 		{"debug",		required_argument,	0,	'x'},
5322dd076b8SGabor Kovesdan 		{"directory",		required_argument,	0,	'd'},
5335e64d66cSPedro F. Giffuni 		{"dry-run",		no_argument,		0,	'C'},
5342dd076b8SGabor Kovesdan 		{"ed",			no_argument,		0,	'e'},
5352dd076b8SGabor Kovesdan 		{"force",		no_argument,		0,	'f'},
5362dd076b8SGabor Kovesdan 		{"forward",		no_argument,		0,	'N'},
5372dd076b8SGabor Kovesdan 		{"fuzz",		required_argument,	0,	'F'},
5382dd076b8SGabor Kovesdan 		{"ifdef",		required_argument,	0,	'D'},
5392dd076b8SGabor Kovesdan 		{"input",		required_argument,	0,	'i'},
5402dd076b8SGabor Kovesdan 		{"ignore-whitespace",	no_argument,		0,	'l'},
5412dd076b8SGabor Kovesdan 		{"normal",		no_argument,		0,	'n'},
5422dd076b8SGabor Kovesdan 		{"output",		required_argument,	0,	'o'},
5432dd076b8SGabor Kovesdan 		{"prefix",		required_argument,	0,	'B'},
5442dd076b8SGabor Kovesdan 		{"quiet",		no_argument,		0,	's'},
5452dd076b8SGabor Kovesdan 		{"reject-file",		required_argument,	0,	'r'},
5462dd076b8SGabor Kovesdan 		{"remove-empty-files",	no_argument,		0,	'E'},
5472dd076b8SGabor Kovesdan 		{"reverse",		no_argument,		0,	'R'},
5482dd076b8SGabor Kovesdan 		{"silent",		no_argument,		0,	's'},
5492dd076b8SGabor Kovesdan 		{"strip",		required_argument,	0,	'p'},
5502dd076b8SGabor Kovesdan 		{"suffix",		required_argument,	0,	'z'},
5512dd076b8SGabor Kovesdan 		{"unified",		no_argument,		0,	'u'},
5522dd076b8SGabor Kovesdan 		{"version",		no_argument,		0,	'v'},
5532dd076b8SGabor Kovesdan 		{"version-control",	required_argument,	0,	'V'},
5542dd076b8SGabor Kovesdan 		{"posix",		no_argument,		&posix,	1},
5552dd076b8SGabor Kovesdan 		{NULL,			0,			0,	0}
5562dd076b8SGabor Kovesdan 	};
5572dd076b8SGabor Kovesdan 	int ch;
5582dd076b8SGabor Kovesdan 
5592dd076b8SGabor Kovesdan 	rejname[0] = '\0';
5602dd076b8SGabor Kovesdan 	Argc_last = Argc;
5612dd076b8SGabor Kovesdan 	Argv_last = Argv;
5622dd076b8SGabor Kovesdan 	if (!Argc)
5632dd076b8SGabor Kovesdan 		return;
5642dd076b8SGabor Kovesdan 	optreset = optind = 1;
5652dd076b8SGabor Kovesdan 	while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
5662dd076b8SGabor Kovesdan 		switch (ch) {
5672dd076b8SGabor Kovesdan 		case 'b':
5682dd076b8SGabor Kovesdan 			if (backup_type == none)
5692dd076b8SGabor Kovesdan 				backup_type = numbered_existing;
5702dd076b8SGabor Kovesdan 			if (optarg == NULL)
5712dd076b8SGabor Kovesdan 				break;
5722dd076b8SGabor Kovesdan 			if (verbose)
5732dd076b8SGabor Kovesdan 				say("Warning, the ``-b suffix'' option has been"
5742dd076b8SGabor Kovesdan 				    " obsoleted by the -z option.\n");
5752dd076b8SGabor Kovesdan 			/* FALLTHROUGH */
5762dd076b8SGabor Kovesdan 		case 'z':
5772dd076b8SGabor Kovesdan 			/* must directly follow 'b' case for backwards compat */
578547e0acbSPedro F. Giffuni 			simple_backup_suffix = xstrdup(optarg);
5792dd076b8SGabor Kovesdan 			break;
5802dd076b8SGabor Kovesdan 		case 'B':
581547e0acbSPedro F. Giffuni 			origprae = xstrdup(optarg);
5822dd076b8SGabor Kovesdan 			break;
5832dd076b8SGabor Kovesdan 		case 'c':
5842dd076b8SGabor Kovesdan 			diff_type = CONTEXT_DIFF;
5852dd076b8SGabor Kovesdan 			break;
5862dd076b8SGabor Kovesdan 		case 'C':
5872dd076b8SGabor Kovesdan 			check_only = true;
5882dd076b8SGabor Kovesdan 			break;
5892dd076b8SGabor Kovesdan 		case 'd':
5902dd076b8SGabor Kovesdan 			if (chdir(optarg) < 0)
5912dd076b8SGabor Kovesdan 				pfatal("can't cd to %s", optarg);
5922dd076b8SGabor Kovesdan 			break;
5932dd076b8SGabor Kovesdan 		case 'D':
5942dd076b8SGabor Kovesdan 			do_defines = true;
5952dd076b8SGabor Kovesdan 			if (!isalpha((unsigned char)*optarg) && *optarg != '_')
5962dd076b8SGabor Kovesdan 				fatal("argument to -D is not an identifier\n");
5972dd076b8SGabor Kovesdan 			snprintf(if_defined, sizeof if_defined,
5982dd076b8SGabor Kovesdan 			    "#ifdef %s\n", optarg);
5992dd076b8SGabor Kovesdan 			snprintf(not_defined, sizeof not_defined,
6002dd076b8SGabor Kovesdan 			    "#ifndef %s\n", optarg);
6012dd076b8SGabor Kovesdan 			snprintf(end_defined, sizeof end_defined,
6022dd076b8SGabor Kovesdan 			    "#endif /* %s */\n", optarg);
6032dd076b8SGabor Kovesdan 			break;
6042dd076b8SGabor Kovesdan 		case 'e':
6052dd076b8SGabor Kovesdan 			diff_type = ED_DIFF;
6062dd076b8SGabor Kovesdan 			break;
6072dd076b8SGabor Kovesdan 		case 'E':
6082dd076b8SGabor Kovesdan 			remove_empty_files = true;
6092dd076b8SGabor Kovesdan 			break;
6102dd076b8SGabor Kovesdan 		case 'f':
6112dd076b8SGabor Kovesdan 			force = true;
6122dd076b8SGabor Kovesdan 			break;
6132dd076b8SGabor Kovesdan 		case 'F':
6142dd076b8SGabor Kovesdan 			maxfuzz = atoi(optarg);
6152dd076b8SGabor Kovesdan 			break;
6162dd076b8SGabor Kovesdan 		case 'i':
6172dd076b8SGabor Kovesdan 			if (++filec == MAXFILEC)
6182dd076b8SGabor Kovesdan 				fatal("too many file arguments\n");
619547e0acbSPedro F. Giffuni 			filearg[filec] = xstrdup(optarg);
6202dd076b8SGabor Kovesdan 			break;
6212dd076b8SGabor Kovesdan 		case 'l':
6222dd076b8SGabor Kovesdan 			canonicalize = true;
6232dd076b8SGabor Kovesdan 			break;
6242dd076b8SGabor Kovesdan 		case 'n':
6252dd076b8SGabor Kovesdan 			diff_type = NORMAL_DIFF;
6262dd076b8SGabor Kovesdan 			break;
6272dd076b8SGabor Kovesdan 		case 'N':
6282dd076b8SGabor Kovesdan 			noreverse = true;
6292dd076b8SGabor Kovesdan 			break;
6302dd076b8SGabor Kovesdan 		case 'o':
631547e0acbSPedro F. Giffuni 			outname = xstrdup(optarg);
6322dd076b8SGabor Kovesdan 			break;
6332dd076b8SGabor Kovesdan 		case 'p':
6342dd076b8SGabor Kovesdan 			strippath = atoi(optarg);
6352dd076b8SGabor Kovesdan 			break;
6362dd076b8SGabor Kovesdan 		case 'r':
6372dd076b8SGabor Kovesdan 			if (strlcpy(rejname, optarg,
6382dd076b8SGabor Kovesdan 			    sizeof(rejname)) >= sizeof(rejname))
6392dd076b8SGabor Kovesdan 				fatal("argument for -r is too long\n");
6402dd076b8SGabor Kovesdan 			break;
6412dd076b8SGabor Kovesdan 		case 'R':
6422dd076b8SGabor Kovesdan 			reverse = true;
6432dd076b8SGabor Kovesdan 			reverse_flag_specified = true;
6442dd076b8SGabor Kovesdan 			break;
6452dd076b8SGabor Kovesdan 		case 's':
6462dd076b8SGabor Kovesdan 			verbose = false;
6472dd076b8SGabor Kovesdan 			break;
6482dd076b8SGabor Kovesdan 		case 't':
6492dd076b8SGabor Kovesdan 			batch = true;
6502dd076b8SGabor Kovesdan 			break;
6512dd076b8SGabor Kovesdan 		case 'u':
6522dd076b8SGabor Kovesdan 			diff_type = UNI_DIFF;
6532dd076b8SGabor Kovesdan 			break;
6542dd076b8SGabor Kovesdan 		case 'v':
6552dd076b8SGabor Kovesdan 			version();
6562dd076b8SGabor Kovesdan 			break;
6572dd076b8SGabor Kovesdan 		case 'V':
6582dd076b8SGabor Kovesdan 			backup_type = get_version(optarg);
659300ca9a8SConrad Meyer 			Vflag = true;
6602dd076b8SGabor Kovesdan 			break;
6612dd076b8SGabor Kovesdan #ifdef DEBUGGING
6622dd076b8SGabor Kovesdan 		case 'x':
6632dd076b8SGabor Kovesdan 			debug = atoi(optarg);
6642dd076b8SGabor Kovesdan 			break;
6652dd076b8SGabor Kovesdan #endif
6662dd076b8SGabor Kovesdan 		default:
6672dd076b8SGabor Kovesdan 			if (ch != '\0')
6682dd076b8SGabor Kovesdan 				usage();
6692dd076b8SGabor Kovesdan 			break;
6702dd076b8SGabor Kovesdan 		}
6712dd076b8SGabor Kovesdan 	}
6722dd076b8SGabor Kovesdan 	Argc -= optind;
6732dd076b8SGabor Kovesdan 	Argv += optind;
6742dd076b8SGabor Kovesdan 
6752dd076b8SGabor Kovesdan 	if (Argc > 0) {
676547e0acbSPedro F. Giffuni 		filearg[0] = xstrdup(*Argv++);
6772dd076b8SGabor Kovesdan 		Argc--;
6782dd076b8SGabor Kovesdan 		while (Argc > 0) {
6792dd076b8SGabor Kovesdan 			if (++filec == MAXFILEC)
6802dd076b8SGabor Kovesdan 				fatal("too many file arguments\n");
681547e0acbSPedro F. Giffuni 			filearg[filec] = xstrdup(*Argv++);
6822dd076b8SGabor Kovesdan 			Argc--;
6832dd076b8SGabor Kovesdan 		}
6842dd076b8SGabor Kovesdan 	}
6852dd076b8SGabor Kovesdan 
6862dd076b8SGabor Kovesdan 	if (getenv("POSIXLY_CORRECT") != NULL)
6872dd076b8SGabor Kovesdan 		posix = 1;
6882dd076b8SGabor Kovesdan }
6892dd076b8SGabor Kovesdan 
6902dd076b8SGabor Kovesdan static void
usage(void)6912dd076b8SGabor Kovesdan usage(void)
6922dd076b8SGabor Kovesdan {
6932dd076b8SGabor Kovesdan 	fprintf(stderr,
6942dd076b8SGabor Kovesdan "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
6952dd076b8SGabor Kovesdan "             [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
696300ca9a8SConrad Meyer "             [-r rej-name] [-V t | nil | never | none] [-x number]\n"
697300ca9a8SConrad Meyer "             [-z backup-ext] [--posix] [origfile [patchfile]]\n"
6982dd076b8SGabor Kovesdan "       patch <patchfile\n");
699f718bedcSPedro F. Giffuni 	my_exit(EXIT_FAILURE);
7002dd076b8SGabor Kovesdan }
7012dd076b8SGabor Kovesdan 
7022dd076b8SGabor Kovesdan /*
7032dd076b8SGabor Kovesdan  * Attempt to find the right place to apply this hunk of patch.
7042dd076b8SGabor Kovesdan  */
7052dd076b8SGabor Kovesdan static LINENUM
locate_hunk(LINENUM fuzz)7062dd076b8SGabor Kovesdan locate_hunk(LINENUM fuzz)
7072dd076b8SGabor Kovesdan {
7082dd076b8SGabor Kovesdan 	LINENUM	first_guess = pch_first() + last_offset;
7092dd076b8SGabor Kovesdan 	LINENUM	offset;
7102dd076b8SGabor Kovesdan 	LINENUM	pat_lines = pch_ptrn_lines();
7112dd076b8SGabor Kovesdan 	LINENUM	max_pos_offset = input_lines - first_guess - pat_lines + 1;
7122dd076b8SGabor Kovesdan 	LINENUM	max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
7132dd076b8SGabor Kovesdan 
7142dd076b8SGabor Kovesdan 	if (pat_lines == 0) {		/* null range matches always */
7152dd076b8SGabor Kovesdan 		if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
7162dd076b8SGabor Kovesdan 		    || diff_type == NEW_CONTEXT_DIFF
7172dd076b8SGabor Kovesdan 		    || diff_type == UNI_DIFF)) {
7182dd076b8SGabor Kovesdan 			say("Empty context always matches.\n");
7192dd076b8SGabor Kovesdan 		}
7207e8afbb6SPedro F. Giffuni 		if (first_guess == 0)	/* empty file */
7217e8afbb6SPedro F. Giffuni 			return 1;
7222dd076b8SGabor Kovesdan 		return (first_guess);
7232dd076b8SGabor Kovesdan 	}
7242dd076b8SGabor Kovesdan 	if (max_neg_offset >= first_guess)	/* do not try lines < 0 */
7252dd076b8SGabor Kovesdan 		max_neg_offset = first_guess - 1;
7262dd076b8SGabor Kovesdan 	if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
7272dd076b8SGabor Kovesdan 		return first_guess;
7282dd076b8SGabor Kovesdan 	for (offset = 1; ; offset++) {
7292dd076b8SGabor Kovesdan 		bool	check_after = (offset <= max_pos_offset);
7302dd076b8SGabor Kovesdan 		bool	check_before = (offset <= max_neg_offset);
7312dd076b8SGabor Kovesdan 
7322dd076b8SGabor Kovesdan 		if (check_after && patch_match(first_guess, offset, fuzz)) {
7332dd076b8SGabor Kovesdan #ifdef DEBUGGING
7342dd076b8SGabor Kovesdan 			if (debug & 1)
7352dd076b8SGabor Kovesdan 				say("Offset changing from %ld to %ld\n",
7362dd076b8SGabor Kovesdan 				    last_offset, offset);
7372dd076b8SGabor Kovesdan #endif
7382dd076b8SGabor Kovesdan 			last_offset = offset;
7392dd076b8SGabor Kovesdan 			return first_guess + offset;
7402dd076b8SGabor Kovesdan 		} else if (check_before && patch_match(first_guess, -offset, fuzz)) {
7412dd076b8SGabor Kovesdan #ifdef DEBUGGING
7422dd076b8SGabor Kovesdan 			if (debug & 1)
7432dd076b8SGabor Kovesdan 				say("Offset changing from %ld to %ld\n",
7442dd076b8SGabor Kovesdan 				    last_offset, -offset);
7452dd076b8SGabor Kovesdan #endif
7462dd076b8SGabor Kovesdan 			last_offset = -offset;
7472dd076b8SGabor Kovesdan 			return first_guess - offset;
7482dd076b8SGabor Kovesdan 		} else if (!check_before && !check_after)
7492dd076b8SGabor Kovesdan 			return 0;
7502dd076b8SGabor Kovesdan 	}
7512dd076b8SGabor Kovesdan }
7522dd076b8SGabor Kovesdan 
7532dd076b8SGabor Kovesdan /* We did not find the pattern, dump out the hunk so they can handle it. */
7542dd076b8SGabor Kovesdan 
7552dd076b8SGabor Kovesdan static void
abort_context_hunk(void)7562dd076b8SGabor Kovesdan abort_context_hunk(void)
7572dd076b8SGabor Kovesdan {
7582dd076b8SGabor Kovesdan 	LINENUM	i;
7592dd076b8SGabor Kovesdan 	const LINENUM	pat_end = pch_end();
7602dd076b8SGabor Kovesdan 	/*
7612dd076b8SGabor Kovesdan 	 * add in last_offset to guess the same as the previous successful
7622dd076b8SGabor Kovesdan 	 * hunk
7632dd076b8SGabor Kovesdan 	 */
7642dd076b8SGabor Kovesdan 	const LINENUM	oldfirst = pch_first() + last_offset;
7652dd076b8SGabor Kovesdan 	const LINENUM	newfirst = pch_newfirst() + last_offset;
7662dd076b8SGabor Kovesdan 	const LINENUM	oldlast = oldfirst + pch_ptrn_lines() - 1;
7672dd076b8SGabor Kovesdan 	const LINENUM	newlast = newfirst + pch_repl_lines() - 1;
7682dd076b8SGabor Kovesdan 	const char	*stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
7692dd076b8SGabor Kovesdan 	const char	*minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
7702dd076b8SGabor Kovesdan 
7712dd076b8SGabor Kovesdan 	fprintf(rejfp, "***************\n");
7722dd076b8SGabor Kovesdan 	for (i = 0; i <= pat_end; i++) {
7732dd076b8SGabor Kovesdan 		switch (pch_char(i)) {
7742dd076b8SGabor Kovesdan 		case '*':
7752dd076b8SGabor Kovesdan 			if (oldlast < oldfirst)
7762dd076b8SGabor Kovesdan 				fprintf(rejfp, "*** 0%s\n", stars);
7772dd076b8SGabor Kovesdan 			else if (oldlast == oldfirst)
7782dd076b8SGabor Kovesdan 				fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
7792dd076b8SGabor Kovesdan 			else
7802dd076b8SGabor Kovesdan 				fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
7812dd076b8SGabor Kovesdan 				    oldlast, stars);
7822dd076b8SGabor Kovesdan 			break;
7832dd076b8SGabor Kovesdan 		case '=':
7842dd076b8SGabor Kovesdan 			if (newlast < newfirst)
7852dd076b8SGabor Kovesdan 				fprintf(rejfp, "--- 0%s\n", minuses);
7862dd076b8SGabor Kovesdan 			else if (newlast == newfirst)
7872dd076b8SGabor Kovesdan 				fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
7882dd076b8SGabor Kovesdan 			else
7892dd076b8SGabor Kovesdan 				fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
7902dd076b8SGabor Kovesdan 				    newlast, minuses);
7912dd076b8SGabor Kovesdan 			break;
7922dd076b8SGabor Kovesdan 		case '\n':
7932dd076b8SGabor Kovesdan 			fprintf(rejfp, "%s", pfetch(i));
7942dd076b8SGabor Kovesdan 			break;
7952dd076b8SGabor Kovesdan 		case ' ':
7962dd076b8SGabor Kovesdan 		case '-':
7972dd076b8SGabor Kovesdan 		case '+':
7982dd076b8SGabor Kovesdan 		case '!':
7992dd076b8SGabor Kovesdan 			fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
8002dd076b8SGabor Kovesdan 			break;
8012dd076b8SGabor Kovesdan 		default:
8022dd076b8SGabor Kovesdan 			fatal("fatal internal error in abort_context_hunk\n");
8032dd076b8SGabor Kovesdan 		}
8042dd076b8SGabor Kovesdan 	}
8052dd076b8SGabor Kovesdan }
8062dd076b8SGabor Kovesdan 
8072dd076b8SGabor Kovesdan static void
rej_line(int ch,LINENUM i)8082dd076b8SGabor Kovesdan rej_line(int ch, LINENUM i)
8092dd076b8SGabor Kovesdan {
8103548708cSPedro F. Giffuni 	size_t len;
8112dd076b8SGabor Kovesdan 	const char *line = pfetch(i);
8122dd076b8SGabor Kovesdan 
813fa812237SPedro F. Giffuni 	len = strlen(line);
8142dd076b8SGabor Kovesdan 
8152dd076b8SGabor Kovesdan 	fprintf(rejfp, "%c%s", ch, line);
8164f548c19SPedro F. Giffuni 	if (len == 0 || line[len - 1] != '\n') {
8174f548c19SPedro F. Giffuni 		if (len >= USHRT_MAX)
818ad8469feSPedro F. Giffuni 			fprintf(rejfp, "\n\\ Line too long\n");
8194f548c19SPedro F. Giffuni 		else
8204f548c19SPedro F. Giffuni 			fprintf(rejfp, "\n\\ No newline at end of line\n");
8214f548c19SPedro F. Giffuni 	}
8222dd076b8SGabor Kovesdan }
8232dd076b8SGabor Kovesdan 
8242dd076b8SGabor Kovesdan static void
abort_hunk(void)8252dd076b8SGabor Kovesdan abort_hunk(void)
8262dd076b8SGabor Kovesdan {
8272dd076b8SGabor Kovesdan 	LINENUM		i, j, split;
8282dd076b8SGabor Kovesdan 	int		ch1, ch2;
8292dd076b8SGabor Kovesdan 	const LINENUM	pat_end = pch_end();
8302dd076b8SGabor Kovesdan 	const LINENUM	oldfirst = pch_first() + last_offset;
8312dd076b8SGabor Kovesdan 	const LINENUM	newfirst = pch_newfirst() + last_offset;
8322dd076b8SGabor Kovesdan 
8332dd076b8SGabor Kovesdan 	if (diff_type != UNI_DIFF) {
8342dd076b8SGabor Kovesdan 		abort_context_hunk();
8352dd076b8SGabor Kovesdan 		return;
8362dd076b8SGabor Kovesdan 	}
8372dd076b8SGabor Kovesdan 	split = -1;
8382dd076b8SGabor Kovesdan 	for (i = 0; i <= pat_end; i++) {
8392dd076b8SGabor Kovesdan 		if (pch_char(i) == '=') {
8402dd076b8SGabor Kovesdan 			split = i;
8412dd076b8SGabor Kovesdan 			break;
8422dd076b8SGabor Kovesdan 		}
8432dd076b8SGabor Kovesdan 	}
8442dd076b8SGabor Kovesdan 	if (split == -1) {
8452dd076b8SGabor Kovesdan 		fprintf(rejfp, "malformed hunk: no split found\n");
8462dd076b8SGabor Kovesdan 		return;
8472dd076b8SGabor Kovesdan 	}
8482dd076b8SGabor Kovesdan 	i = 0;
8492dd076b8SGabor Kovesdan 	j = split + 1;
8502dd076b8SGabor Kovesdan 	fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
8512dd076b8SGabor Kovesdan 	    pch_ptrn_lines() ? oldfirst : 0,
8522dd076b8SGabor Kovesdan 	    pch_ptrn_lines(), newfirst, pch_repl_lines());
8532dd076b8SGabor Kovesdan 	while (i < split || j <= pat_end) {
8542dd076b8SGabor Kovesdan 		ch1 = i < split ? pch_char(i) : -1;
8552dd076b8SGabor Kovesdan 		ch2 = j <= pat_end ? pch_char(j) : -1;
8562dd076b8SGabor Kovesdan 		if (ch1 == '-') {
8572dd076b8SGabor Kovesdan 			rej_line('-', i);
8582dd076b8SGabor Kovesdan 			i++;
8592dd076b8SGabor Kovesdan 		} else if (ch1 == ' ' && ch2 == ' ') {
8602dd076b8SGabor Kovesdan 			rej_line(' ', i);
8612dd076b8SGabor Kovesdan 			i++;
8622dd076b8SGabor Kovesdan 			j++;
8632dd076b8SGabor Kovesdan 		} else if (ch1 == '!' && ch2 == '!') {
8642dd076b8SGabor Kovesdan 			while (i < split && ch1 == '!') {
8652dd076b8SGabor Kovesdan 				rej_line('-', i);
8662dd076b8SGabor Kovesdan 				i++;
8672dd076b8SGabor Kovesdan 				ch1 = i < split ? pch_char(i) : -1;
8682dd076b8SGabor Kovesdan 			}
8692dd076b8SGabor Kovesdan 			while (j <= pat_end && ch2 == '!') {
8702dd076b8SGabor Kovesdan 				rej_line('+', j);
8712dd076b8SGabor Kovesdan 				j++;
8722dd076b8SGabor Kovesdan 				ch2 = j <= pat_end ? pch_char(j) : -1;
8732dd076b8SGabor Kovesdan 			}
8742dd076b8SGabor Kovesdan 		} else if (ch1 == '*') {
8752dd076b8SGabor Kovesdan 			i++;
8762dd076b8SGabor Kovesdan 		} else if (ch2 == '+' || ch2 == ' ') {
8772dd076b8SGabor Kovesdan 			rej_line(ch2, j);
8782dd076b8SGabor Kovesdan 			j++;
8792dd076b8SGabor Kovesdan 		} else {
8802dd076b8SGabor Kovesdan 			fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
8812dd076b8SGabor Kovesdan 			    i, split, j);
8822dd076b8SGabor Kovesdan 			rej_line(ch1, i);
8832dd076b8SGabor Kovesdan 			rej_line(ch2, j);
8842dd076b8SGabor Kovesdan 			return;
8852dd076b8SGabor Kovesdan 		}
8862dd076b8SGabor Kovesdan 	}
8872dd076b8SGabor Kovesdan }
8882dd076b8SGabor Kovesdan 
8892dd076b8SGabor Kovesdan /* We found where to apply it (we hope), so do it. */
8902dd076b8SGabor Kovesdan 
8912dd076b8SGabor Kovesdan static void
apply_hunk(LINENUM where)8922dd076b8SGabor Kovesdan apply_hunk(LINENUM where)
8932dd076b8SGabor Kovesdan {
8942dd076b8SGabor Kovesdan 	LINENUM		old = 1;
8952dd076b8SGabor Kovesdan 	const LINENUM	lastline = pch_ptrn_lines();
8962dd076b8SGabor Kovesdan 	LINENUM		new = lastline + 1;
8972dd076b8SGabor Kovesdan #define OUTSIDE 0
8982dd076b8SGabor Kovesdan #define IN_IFNDEF 1
8992dd076b8SGabor Kovesdan #define IN_IFDEF 2
9002dd076b8SGabor Kovesdan #define IN_ELSE 3
9012dd076b8SGabor Kovesdan 	int		def_state = OUTSIDE;
9022dd076b8SGabor Kovesdan 	const LINENUM	pat_end = pch_end();
9032dd076b8SGabor Kovesdan 
9042dd076b8SGabor Kovesdan 	where--;
9052dd076b8SGabor Kovesdan 	while (pch_char(new) == '=' || pch_char(new) == '\n')
9062dd076b8SGabor Kovesdan 		new++;
9072dd076b8SGabor Kovesdan 
9082dd076b8SGabor Kovesdan 	while (old <= lastline) {
9092dd076b8SGabor Kovesdan 		if (pch_char(old) == '-') {
9102dd076b8SGabor Kovesdan 			copy_till(where + old - 1, false);
9112dd076b8SGabor Kovesdan 			if (do_defines) {
9122dd076b8SGabor Kovesdan 				if (def_state == OUTSIDE) {
9132dd076b8SGabor Kovesdan 					fputs(not_defined, ofp);
9142dd076b8SGabor Kovesdan 					def_state = IN_IFNDEF;
9152dd076b8SGabor Kovesdan 				} else if (def_state == IN_IFDEF) {
9162dd076b8SGabor Kovesdan 					fputs(else_defined, ofp);
9172dd076b8SGabor Kovesdan 					def_state = IN_ELSE;
9182dd076b8SGabor Kovesdan 				}
9192dd076b8SGabor Kovesdan 				fputs(pfetch(old), ofp);
9202dd076b8SGabor Kovesdan 			}
9212dd076b8SGabor Kovesdan 			last_frozen_line++;
9222dd076b8SGabor Kovesdan 			old++;
9232dd076b8SGabor Kovesdan 		} else if (new > pat_end) {
9242dd076b8SGabor Kovesdan 			break;
9252dd076b8SGabor Kovesdan 		} else if (pch_char(new) == '+') {
9262dd076b8SGabor Kovesdan 			copy_till(where + old - 1, false);
9272dd076b8SGabor Kovesdan 			if (do_defines) {
9282dd076b8SGabor Kovesdan 				if (def_state == IN_IFNDEF) {
9292dd076b8SGabor Kovesdan 					fputs(else_defined, ofp);
9302dd076b8SGabor Kovesdan 					def_state = IN_ELSE;
9312dd076b8SGabor Kovesdan 				} else if (def_state == OUTSIDE) {
9322dd076b8SGabor Kovesdan 					fputs(if_defined, ofp);
9332dd076b8SGabor Kovesdan 					def_state = IN_IFDEF;
9342dd076b8SGabor Kovesdan 				}
9352dd076b8SGabor Kovesdan 			}
9362dd076b8SGabor Kovesdan 			fputs(pfetch(new), ofp);
9372dd076b8SGabor Kovesdan 			new++;
9382dd076b8SGabor Kovesdan 		} else if (pch_char(new) != pch_char(old)) {
9392dd076b8SGabor Kovesdan 			say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
9402dd076b8SGabor Kovesdan 			    pch_hunk_beg() + old,
9412dd076b8SGabor Kovesdan 			    pch_hunk_beg() + new);
9422dd076b8SGabor Kovesdan #ifdef DEBUGGING
9432dd076b8SGabor Kovesdan 			say("oldchar = '%c', newchar = '%c'\n",
9442dd076b8SGabor Kovesdan 			    pch_char(old), pch_char(new));
9452dd076b8SGabor Kovesdan #endif
9462dd076b8SGabor Kovesdan 			my_exit(2);
9472dd076b8SGabor Kovesdan 		} else if (pch_char(new) == '!') {
9482dd076b8SGabor Kovesdan 			copy_till(where + old - 1, false);
9492dd076b8SGabor Kovesdan 			if (do_defines) {
9502dd076b8SGabor Kovesdan 				fputs(not_defined, ofp);
9512dd076b8SGabor Kovesdan 				def_state = IN_IFNDEF;
9522dd076b8SGabor Kovesdan 			}
9532dd076b8SGabor Kovesdan 			while (pch_char(old) == '!') {
9542dd076b8SGabor Kovesdan 				if (do_defines) {
9552dd076b8SGabor Kovesdan 					fputs(pfetch(old), ofp);
9562dd076b8SGabor Kovesdan 				}
9572dd076b8SGabor Kovesdan 				last_frozen_line++;
9582dd076b8SGabor Kovesdan 				old++;
9592dd076b8SGabor Kovesdan 			}
9602dd076b8SGabor Kovesdan 			if (do_defines) {
9612dd076b8SGabor Kovesdan 				fputs(else_defined, ofp);
9622dd076b8SGabor Kovesdan 				def_state = IN_ELSE;
9632dd076b8SGabor Kovesdan 			}
9642dd076b8SGabor Kovesdan 			while (pch_char(new) == '!') {
9652dd076b8SGabor Kovesdan 				fputs(pfetch(new), ofp);
9662dd076b8SGabor Kovesdan 				new++;
9672dd076b8SGabor Kovesdan 			}
9682dd076b8SGabor Kovesdan 		} else {
9692dd076b8SGabor Kovesdan 			if (pch_char(new) != ' ')
9702dd076b8SGabor Kovesdan 				fatal("Internal error: expected ' '\n");
9712dd076b8SGabor Kovesdan 			old++;
9722dd076b8SGabor Kovesdan 			new++;
9732dd076b8SGabor Kovesdan 			if (do_defines && def_state != OUTSIDE) {
9742dd076b8SGabor Kovesdan 				fputs(end_defined, ofp);
9752dd076b8SGabor Kovesdan 				def_state = OUTSIDE;
9762dd076b8SGabor Kovesdan 			}
9772dd076b8SGabor Kovesdan 		}
9782dd076b8SGabor Kovesdan 	}
9792dd076b8SGabor Kovesdan 	if (new <= pat_end && pch_char(new) == '+') {
9802dd076b8SGabor Kovesdan 		copy_till(where + old - 1, false);
9812dd076b8SGabor Kovesdan 		if (do_defines) {
9822dd076b8SGabor Kovesdan 			if (def_state == OUTSIDE) {
9832dd076b8SGabor Kovesdan 				fputs(if_defined, ofp);
9842dd076b8SGabor Kovesdan 				def_state = IN_IFDEF;
9852dd076b8SGabor Kovesdan 			} else if (def_state == IN_IFNDEF) {
9862dd076b8SGabor Kovesdan 				fputs(else_defined, ofp);
9872dd076b8SGabor Kovesdan 				def_state = IN_ELSE;
9882dd076b8SGabor Kovesdan 			}
9892dd076b8SGabor Kovesdan 		}
9902dd076b8SGabor Kovesdan 		while (new <= pat_end && pch_char(new) == '+') {
9912dd076b8SGabor Kovesdan 			fputs(pfetch(new), ofp);
9922dd076b8SGabor Kovesdan 			new++;
9932dd076b8SGabor Kovesdan 		}
9942dd076b8SGabor Kovesdan 	}
9952dd076b8SGabor Kovesdan 	if (do_defines && def_state != OUTSIDE) {
9962dd076b8SGabor Kovesdan 		fputs(end_defined, ofp);
9972dd076b8SGabor Kovesdan 	}
9982dd076b8SGabor Kovesdan }
9992dd076b8SGabor Kovesdan 
10002dd076b8SGabor Kovesdan /*
10012dd076b8SGabor Kovesdan  * Open the new file.
10022dd076b8SGabor Kovesdan  */
10032dd076b8SGabor Kovesdan static void
init_output(const char * name)10042dd076b8SGabor Kovesdan init_output(const char *name)
10052dd076b8SGabor Kovesdan {
10062dd076b8SGabor Kovesdan 	ofp = fopen(name, "w");
10072dd076b8SGabor Kovesdan 	if (ofp == NULL)
10082dd076b8SGabor Kovesdan 		pfatal("can't create %s", name);
10092dd076b8SGabor Kovesdan }
10102dd076b8SGabor Kovesdan 
10112dd076b8SGabor Kovesdan /*
10122dd076b8SGabor Kovesdan  * Open a file to put hunks we can't locate.
10132dd076b8SGabor Kovesdan  */
10142dd076b8SGabor Kovesdan static void
init_reject(const char * name)10152dd076b8SGabor Kovesdan init_reject(const char *name)
10162dd076b8SGabor Kovesdan {
10172dd076b8SGabor Kovesdan 	rejfp = fopen(name, "w");
10182dd076b8SGabor Kovesdan 	if (rejfp == NULL)
10192dd076b8SGabor Kovesdan 		pfatal("can't create %s", name);
10202dd076b8SGabor Kovesdan }
10212dd076b8SGabor Kovesdan 
10222dd076b8SGabor Kovesdan /*
10232dd076b8SGabor Kovesdan  * Copy input file to output, up to wherever hunk is to be applied.
10242dd076b8SGabor Kovesdan  * If endoffile is true, treat the last line specially since it may
10252dd076b8SGabor Kovesdan  * lack a newline.
10262dd076b8SGabor Kovesdan  */
10272dd076b8SGabor Kovesdan static void
copy_till(LINENUM lastline,bool endoffile)10282dd076b8SGabor Kovesdan copy_till(LINENUM lastline, bool endoffile)
10292dd076b8SGabor Kovesdan {
10302dd076b8SGabor Kovesdan 	if (last_frozen_line > lastline)
10312dd076b8SGabor Kovesdan 		fatal("misordered hunks! output would be garbled\n");
10322dd076b8SGabor Kovesdan 	while (last_frozen_line < lastline) {
10332dd076b8SGabor Kovesdan 		if (++last_frozen_line == lastline && endoffile)
10342dd076b8SGabor Kovesdan 			dump_line(last_frozen_line, !last_line_missing_eol);
10352dd076b8SGabor Kovesdan 		else
10362dd076b8SGabor Kovesdan 			dump_line(last_frozen_line, true);
10372dd076b8SGabor Kovesdan 	}
10382dd076b8SGabor Kovesdan }
10392dd076b8SGabor Kovesdan 
10402dd076b8SGabor Kovesdan /*
10412dd076b8SGabor Kovesdan  * Finish copying the input file to the output file.
10422dd076b8SGabor Kovesdan  */
10432dd076b8SGabor Kovesdan static bool
spew_output(void)10442dd076b8SGabor Kovesdan spew_output(void)
10452dd076b8SGabor Kovesdan {
10462dd076b8SGabor Kovesdan 	int rv;
10472dd076b8SGabor Kovesdan 
10482dd076b8SGabor Kovesdan #ifdef DEBUGGING
10492dd076b8SGabor Kovesdan 	if (debug & 256)
10502dd076b8SGabor Kovesdan 		say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
10512dd076b8SGabor Kovesdan #endif
10522dd076b8SGabor Kovesdan 	if (input_lines)
10532dd076b8SGabor Kovesdan 		copy_till(input_lines, true);	/* dump remainder of file */
1054ffca5883SGlen Barber 	rv = ferror(ofp) == 0 && fclose(ofp) == 0;
10552dd076b8SGabor Kovesdan 	ofp = NULL;
10562dd076b8SGabor Kovesdan 	return rv;
10572dd076b8SGabor Kovesdan }
10582dd076b8SGabor Kovesdan 
10592dd076b8SGabor Kovesdan /*
10602dd076b8SGabor Kovesdan  * Copy one line from input to output.
10612dd076b8SGabor Kovesdan  */
10622dd076b8SGabor Kovesdan static void
dump_line(LINENUM line,bool write_newline)10632dd076b8SGabor Kovesdan dump_line(LINENUM line, bool write_newline)
10642dd076b8SGabor Kovesdan {
10652dd076b8SGabor Kovesdan 	char	*s;
10662dd076b8SGabor Kovesdan 
10672dd076b8SGabor Kovesdan 	s = ifetch(line, 0);
10682dd076b8SGabor Kovesdan 	if (s == NULL)
10692dd076b8SGabor Kovesdan 		return;
10702dd076b8SGabor Kovesdan 	/* Note: string is not NUL terminated. */
10712dd076b8SGabor Kovesdan 	for (; *s != '\n'; s++)
10722dd076b8SGabor Kovesdan 		putc(*s, ofp);
10732dd076b8SGabor Kovesdan 	if (write_newline)
10742dd076b8SGabor Kovesdan 		putc('\n', ofp);
10752dd076b8SGabor Kovesdan }
10762dd076b8SGabor Kovesdan 
10772dd076b8SGabor Kovesdan /*
10782dd076b8SGabor Kovesdan  * Does the patch pattern match at line base+offset?
10792dd076b8SGabor Kovesdan  */
10802dd076b8SGabor Kovesdan static bool
patch_match(LINENUM base,LINENUM offset,LINENUM fuzz)10812dd076b8SGabor Kovesdan patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
10822dd076b8SGabor Kovesdan {
10832dd076b8SGabor Kovesdan 	LINENUM		pline = 1 + fuzz;
10842dd076b8SGabor Kovesdan 	LINENUM		iline;
10852dd076b8SGabor Kovesdan 	LINENUM		pat_lines = pch_ptrn_lines() - fuzz;
10862dd076b8SGabor Kovesdan 	const char	*ilineptr;
10872dd076b8SGabor Kovesdan 	const char	*plineptr;
1088*851a9da3SDag-Erling Smørgrav 	size_t		plinelen;
10892dd076b8SGabor Kovesdan 
1090bc4f0fe3SKyle Evans 	/* Patch does not match if we don't have any more context to use */
1091bc4f0fe3SKyle Evans 	if (pline > pat_lines)
1092bc4f0fe3SKyle Evans 		return false;
10932dd076b8SGabor Kovesdan 	for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
10942dd076b8SGabor Kovesdan 		ilineptr = ifetch(iline, offset >= 0);
10952dd076b8SGabor Kovesdan 		if (ilineptr == NULL)
10962dd076b8SGabor Kovesdan 			return false;
10972dd076b8SGabor Kovesdan 		plineptr = pfetch(pline);
10982dd076b8SGabor Kovesdan 		plinelen = pch_line_len(pline);
10992dd076b8SGabor Kovesdan 		if (canonicalize) {
11002dd076b8SGabor Kovesdan 			if (!similar(ilineptr, plineptr, plinelen))
11012dd076b8SGabor Kovesdan 				return false;
11022dd076b8SGabor Kovesdan 		} else if (strnNE(ilineptr, plineptr, plinelen))
11032dd076b8SGabor Kovesdan 			return false;
11042dd076b8SGabor Kovesdan 		if (iline == input_lines) {
11052dd076b8SGabor Kovesdan 			/*
11062dd076b8SGabor Kovesdan 			 * We are looking at the last line of the file.
11072dd076b8SGabor Kovesdan 			 * If the file has no eol, the patch line should
11082dd076b8SGabor Kovesdan 			 * not have one either and vice-versa. Note that
11092dd076b8SGabor Kovesdan 			 * plinelen > 0.
11102dd076b8SGabor Kovesdan 			 */
11112dd076b8SGabor Kovesdan 			if (last_line_missing_eol) {
11122dd076b8SGabor Kovesdan 				if (plineptr[plinelen - 1] == '\n')
11132dd076b8SGabor Kovesdan 					return false;
11142dd076b8SGabor Kovesdan 			} else {
11152dd076b8SGabor Kovesdan 				if (plineptr[plinelen - 1] != '\n')
11162dd076b8SGabor Kovesdan 					return false;
11172dd076b8SGabor Kovesdan 			}
11182dd076b8SGabor Kovesdan 		}
11192dd076b8SGabor Kovesdan 	}
11202dd076b8SGabor Kovesdan 	return true;
11212dd076b8SGabor Kovesdan }
11222dd076b8SGabor Kovesdan 
11232dd076b8SGabor Kovesdan /*
11242dd076b8SGabor Kovesdan  * Do two lines match with canonicalized white space?
11252dd076b8SGabor Kovesdan  */
11262dd076b8SGabor Kovesdan static bool
similar(const char * a,const char * b,int len)11272dd076b8SGabor Kovesdan similar(const char *a, const char *b, int len)
11282dd076b8SGabor Kovesdan {
11292dd076b8SGabor Kovesdan 	while (len) {
11302dd076b8SGabor Kovesdan 		if (isspace((unsigned char)*b)) {	/* whitespace (or \n) to match? */
11312dd076b8SGabor Kovesdan 			if (!isspace((unsigned char)*a))	/* no corresponding whitespace? */
11322dd076b8SGabor Kovesdan 				return false;
11332dd076b8SGabor Kovesdan 			while (len && isspace((unsigned char)*b) && *b != '\n')
11342dd076b8SGabor Kovesdan 				b++, len--;	/* skip pattern whitespace */
11352dd076b8SGabor Kovesdan 			while (isspace((unsigned char)*a) && *a != '\n')
11362dd076b8SGabor Kovesdan 				a++;	/* skip target whitespace */
11372dd076b8SGabor Kovesdan 			if (*a == '\n' || *b == '\n')
11382dd076b8SGabor Kovesdan 				return (*a == *b);	/* should end in sync */
11392dd076b8SGabor Kovesdan 		} else if (*a++ != *b++)	/* match non-whitespace chars */
11402dd076b8SGabor Kovesdan 			return false;
11412dd076b8SGabor Kovesdan 		else
11422dd076b8SGabor Kovesdan 			len--;	/* probably not necessary */
11432dd076b8SGabor Kovesdan 	}
11442dd076b8SGabor Kovesdan 	return true;		/* actually, this is not reached */
11452dd076b8SGabor Kovesdan 	/* since there is always a \n */
11462dd076b8SGabor Kovesdan }
114750dacbf6SKyle Evans 
114850dacbf6SKyle Evans static bool
handle_creation(bool out_existed,bool * remove)114950dacbf6SKyle Evans handle_creation(bool out_existed, bool *remove)
115050dacbf6SKyle Evans {
115150dacbf6SKyle Evans 	bool reverse_seen;
115250dacbf6SKyle Evans 
115350dacbf6SKyle Evans 	reverse_seen = false;
115450dacbf6SKyle Evans 	if (reverse && out_existed) {
115550dacbf6SKyle Evans 		/*
115650dacbf6SKyle Evans 		 * If the patch creates the file and we're reversing the patch,
115750dacbf6SKyle Evans 		 * then we need to indicate to the patch processor that it's OK
115850dacbf6SKyle Evans 		 * to remove this file.
115950dacbf6SKyle Evans 		 */
116050dacbf6SKyle Evans 		*remove = true;
116150dacbf6SKyle Evans 	} else if (!reverse && out_existed) {
116250dacbf6SKyle Evans 		/*
116350dacbf6SKyle Evans 		 * Otherwise, we need to blow the horn because the patch appears
116450dacbf6SKyle Evans 		 * to be reversed/already applied.  For non-batch jobs, we'll
116550dacbf6SKyle Evans 		 * prompt to figure out what we should be trying to do to raise
116650dacbf6SKyle Evans 		 * awareness of the issue.  batch (-t) processing suppresses the
116750dacbf6SKyle Evans 		 * questions and just assumes that we're reversed if it looks
116850dacbf6SKyle Evans 		 * like we are, which is always the case if we've reached this
116950dacbf6SKyle Evans 		 * branch.
117050dacbf6SKyle Evans 		 */
117150dacbf6SKyle Evans 		if (force) {
117250dacbf6SKyle Evans 			skip_rest_of_patch = true;
117350dacbf6SKyle Evans 			return (false);
117450dacbf6SKyle Evans 		}
117550dacbf6SKyle Evans 		if (noreverse) {
117650dacbf6SKyle Evans 			/* If -N is supplied, however, we bail out/ignore. */
117750dacbf6SKyle Evans 			say("Ignoring previously applied (or reversed) patch.\n");
117850dacbf6SKyle Evans 			skip_rest_of_patch = true;
117950dacbf6SKyle Evans 			return (false);
118050dacbf6SKyle Evans 		}
118150dacbf6SKyle Evans 
118250dacbf6SKyle Evans 		/* Unreversed... suspicious if the file existed. */
118350dacbf6SKyle Evans 		if (!pch_swap())
118450dacbf6SKyle Evans 			fatal("lost hunk on alloc error!\n");
118550dacbf6SKyle Evans 
118650dacbf6SKyle Evans 		reverse = !reverse;
118750dacbf6SKyle Evans 
118850dacbf6SKyle Evans 		if (batch) {
118950dacbf6SKyle Evans 			if (verbose)
119050dacbf6SKyle Evans 				say("Patch creates file that already exists, %s %seversed",
119150dacbf6SKyle Evans 				    reverse ? "Assuming" : "Ignoring",
119250dacbf6SKyle Evans 				    reverse ? "R" : "Unr");
119350dacbf6SKyle Evans 		} else {
119450dacbf6SKyle Evans 			ask("Patch creates file that already exists!  %s -R? [y] ",
119550dacbf6SKyle Evans 			    reverse ? "Assume" : "Ignore");
119650dacbf6SKyle Evans 
119750dacbf6SKyle Evans 			if (*buf == 'n') {
119850dacbf6SKyle Evans 				ask("Apply anyway? [n]");
119950dacbf6SKyle Evans 				if (*buf != 'y')
120050dacbf6SKyle Evans 					/* Don't apply; error out. */
120150dacbf6SKyle Evans 					skip_rest_of_patch = true;
120250dacbf6SKyle Evans 				else
120350dacbf6SKyle Evans 					/* Attempt to apply. */
120450dacbf6SKyle Evans 					reverse_seen = true;
120550dacbf6SKyle Evans 				reverse = !reverse;
120650dacbf6SKyle Evans 				if (!pch_swap())
120750dacbf6SKyle Evans 					fatal("lost hunk on alloc error!\n");
120850dacbf6SKyle Evans 			} else {
120950dacbf6SKyle Evans 				/*
121050dacbf6SKyle Evans 				 * They've opted to assume -R; effectively the
121150dacbf6SKyle Evans 				 * same as the first branch in this function,
121250dacbf6SKyle Evans 				 * but the decision is here rather than in a
121350dacbf6SKyle Evans 				 * prior patch/hunk as in that branch.
121450dacbf6SKyle Evans 				 */
121550dacbf6SKyle Evans 				*remove = true;
121650dacbf6SKyle Evans 			}
121750dacbf6SKyle Evans 		}
121850dacbf6SKyle Evans 	}
121950dacbf6SKyle Evans 
122050dacbf6SKyle Evans 	/*
122150dacbf6SKyle Evans 	 * The return value indicates if we offered a chance to reverse but the
122250dacbf6SKyle Evans 	 * user declined.  This keeps the main patch processor in the loop since
122350dacbf6SKyle Evans 	 * we've taken this out of the normal flow of hunk processing to
122450dacbf6SKyle Evans 	 * simplify logic a little bit.
122550dacbf6SKyle Evans 	 */
122650dacbf6SKyle Evans 	return (reverse_seen);
122750dacbf6SKyle Evans }
1228