toutkeep = TRUE;
toutkeep = true;
main(int argc, char *argv[]){	int	error = 0, hunk, failed, patch_seen = 0, i, fd;	LINENUM	where, newwhere, fuzz, mymaxfuzz;	const	char *tmpdir;	char	*v;	setbuf(stderr, serrbuf);	for (i = 0; i < MAXFILEC; i++)		filearg[i] = NULL;	/* Cons up the names of the temporary files.  */	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')		tmpdir = _PATH_TMP;	for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)		;	i++;	if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)		fatal("cannot allocate memory");	if ((fd = mkstemp(TMPOUTNAME)) < 0)		pfatal("can't create %s", TMPOUTNAME);	close(fd);	if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)		fatal("cannot allocate memory");	if ((fd = mkstemp(TMPINNAME)) < 0)		pfatal("can't create %s", TMPINNAME);	close(fd);	if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)		fatal("cannot allocate memory");	if ((fd = mkstemp(TMPREJNAME)) < 0)		pfatal("can't create %s", TMPREJNAME);	close(fd);	if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)		fatal("cannot allocate memory");	if ((fd = mkstemp(TMPPATNAME)) < 0)		pfatal("can't create %s", TMPPATNAME);	close(fd);	v = getenv("SIMPLE_BACKUP_SUFFIX");	if (v)		simple_backup_suffix = v;	else		simple_backup_suffix = ORIGEXT;	/* parse switches */	Argc = argc;	Argv = argv;	get_some_switches();	if (backup_type == none) {		v = getenv("VERSION_CONTROL");#ifdef notyet		if (v != NULL)#endif			backup_type = get_version(v);	/* OK to pass NULL. */	}	/* make sure we clean up /tmp in case of disaster */	set_signals(0);	for (open_patch_file(filearg[1]); there_is_another_patch();	    reinitialize_almost_everything()) {		/* for each patch in patch file */		patch_seen = TRUE;		warn_on_invalid_line = TRUE;		if (outname == NULL)			outname = savestr(filearg[0]);		/* for ed script just up and do it and exit */		if (diff_type == ED_DIFF) {			do_ed_script();			continue;		}		/* initialize the patched file */		if (!skip_rest_of_patch)			init_output(TMPOUTNAME);		/* initialize reject file */		init_reject(TMPREJNAME);		/* find out where all the lines are */		if (!skip_rest_of_patch)			scan_input(filearg[0]);		/* from here on, open no standard i/o files, because malloc */		/* might misfire and we can't catch it easily */		/* apply each hunk of patch */		hunk = 0;		failed = 0;		out_of_mem = FALSE;		while (another_hunk()) {			hunk++;			fuzz = 0;			mymaxfuzz = pch_context();			if (maxfuzz < mymaxfuzz)				mymaxfuzz = maxfuzz;			if (!skip_rest_of_patch) {				do {					where = locate_hunk(fuzz);					if (hunk == 1 && where == 0 && !force) {						/* dwim for reversed patch? */						if (!pch_swap()) {							if (fuzz == 0)								say("Not enough memory to try swapped hunk!  Assuming unswapped.\n");							continue;						}						reverse = !reverse;						/* try again */						where = locate_hunk(fuzz);						if (where == 0) {							/* didn't find it swapped */							if (!pch_swap())								/* put it back to normal */								fatal("lost hunk on alloc error!\n");							reverse = !reverse;						} else if (noreverse) {							if (!pch_swap())								/* put it back to normal */								fatal("lost hunk on alloc error!\n");							reverse = !reverse;							say("Ignoring previously applied (or reversed) patch.\n");							skip_rest_of_patch = TRUE;						} else if (batch) {							if (verbose)								say("%seversed (or previously applied) patch detected!  %s -R.",								    reverse ? "R" : "Unr",								    reverse ? "Assuming" : "Ignoring");						} else {							ask("%seversed (or previously applied) patch detected!  %s -R? [y] ",							    reverse ? "R" : "Unr",							    reverse ? "Assume" : "Ignore");							if (*buf == 'n') {								ask("Apply anyway? [n] ");								if (*buf != 'y')									skip_rest_of_patch = TRUE;								where = 0;								reverse = !reverse;								if (!pch_swap())									/* put it back to normal */									fatal("lost hunk on alloc error!\n");							}						}					}				} while (!skip_rest_of_patch && where == 0 &&				    ++fuzz <= mymaxfuzz);				if (skip_rest_of_patch) {	/* just got decided */					fclose(ofp);					ofp = NULL;				}			}			newwhere = pch_newfirst() + last_offset;			if (skip_rest_of_patch) {				abort_hunk();				failed++;				if (verbose)					say("Hunk #%d ignored at %ld.\n",					    hunk, newwhere);			} else if (where == 0) {				abort_hunk();				failed++;				if (verbose)					say("Hunk #%d failed at %ld.\n",					    hunk, newwhere);			} else {				apply_hunk(where);				if (verbose) {					say("Hunk #%d succeeded at %ld",					    hunk, newwhere);					if (fuzz != 0)						say(" with fuzz %ld", fuzz);					if (last_offset)						say(" (offset %ld line%s)",						    last_offset,						    last_offset == 1L ? "" : "s");					say(".\n");				}			}		}		if (out_of_mem && using_plan_a) {			Argc = Argc_last;			Argv = Argv_last;			say("\n\nRan out of memory using Plan A--trying again...\n\n");			if (ofp)				fclose(ofp);			ofp = NULL;			if (rejfp)				fclose(rejfp);			rejfp = NULL;			continue;		}		assert(hunk);		/* finish spewing out the new file */		if (!skip_rest_of_patch)			spew_output();		/* and put the output where desired */		ignore_signals();		if (!skip_rest_of_patch) {			struct stat	statbuf;			char	*realout = outname;			if (!check_only) {				if (move_file(TMPOUTNAME, outname) < 0) {					toutkeep = TRUE;					realout = TMPOUTNAME;					chmod(TMPOUTNAME, filemode);				} else					chmod(outname, filemode);				if (remove_empty_files &&				    stat(realout, &statbuf) == 0 &&				    statbuf.st_size == 0) {					if (verbose)						say("Removing %s (empty after patching).\n",						    realout);					unlink(realout);				}			}		}		fclose(rejfp);		rejfp = NULL;		if (failed) {			error = 1;			if (*rejname == '\0') {				if (strlcpy(rejname, outname,				    sizeof(rejname)) >= sizeof(rejname))					fatal("filename %s is too long\n", outname);				if (strlcat(rejname, REJEXT,				    sizeof(rejname)) >= sizeof(rejname))					fatal("filename %s is too long\n", outname);			}			if (skip_rest_of_patch) {				say("%d out of %d hunks ignored--saving rejects to %s\n",				    failed, hunk, rejname);			} else {				say("%d out of %d hunks failed--saving rejects to %s\n",				    failed, hunk, rejname);			}			if (!check_only && move_file(TMPREJNAME, rejname) < 0)				trejkeep = TRUE;		}		set_signals(1);	}	my_exit(error);	/* NOTREACHED */}