int tmpname_len;
main(argc,argv)int argc;char **argv;{    LINENUM where;    LINENUM newwhere;    LINENUM fuzz;    LINENUM mymaxfuzz;    int hunk = 0;    int failed = 0;    int failtotal = 0;    int patch_seen = 0;    int i;    setbuf(stderr, serrbuf);    for (i = 0; i<MAXFILEC; i++)	filearg[i] = Nullch;    myuid = getuid();    /* Cons up the names of the temporary files.  */    {      /* Directory for temporary files.  */      char *tmpdir;      int tmpname_len;      tmpdir = getenv ("TMPDIR");      if (tmpdir == NULL) {	tmpdir = "/tmp";      }      tmpname_len = strlen (tmpdir) + 20;      TMPOUTNAME = (char *) malloc (tmpname_len);      if (TMPOUTNAME == NULL)	fatal1("cannot allocate memory");      strcpy (TMPOUTNAME, tmpdir);      strcat (TMPOUTNAME, "/patchoXXXXXX");      if ((i = mkstemp(TMPOUTNAME)) < 0)	pfatal2("can't create %s", TMPOUTNAME);      Close(i);      TMPINNAME = (char *) malloc (tmpname_len);      if (TMPINNAME == NULL)	fatal1("cannot allocate memory");      strcpy (TMPINNAME, tmpdir);      strcat (TMPINNAME, "/patchiXXXXXX");      if ((i = mkstemp(TMPINNAME)) < 0)	pfatal2("can't create %s", TMPINNAME);      Close(i);      TMPREJNAME = (char *) malloc (tmpname_len);      if (TMPREJNAME == NULL)	fatal1("cannot allocate memory");      strcpy (TMPREJNAME, tmpdir);      strcat (TMPREJNAME, "/patchrXXXXXX");      if ((i = mkstemp(TMPREJNAME)) < 0)	pfatal2("can't create %s", TMPREJNAME);      Close(i);      TMPPATNAME = (char *) malloc (tmpname_len);      if (TMPPATNAME == NULL)	fatal1("cannot allocate memory");      strcpy (TMPPATNAME, tmpdir);      strcat (TMPPATNAME, "/patchpXXXXXX");      if ((i = mkstemp(TMPPATNAME)) < 0)	pfatal2("can't create %s", TMPPATNAME);      Close(i);    }    {      char *v;      v = getenv ("SIMPLE_BACKUP_SUFFIX");      if (v)	simple_backup_suffix = v;      else	simple_backup_suffix = ORIGEXT;#ifndef NODIR      v = getenv ("VERSION_CONTROL");      backup_type = get_version (v); /* OK to pass NULL. */#endif    }    /* parse switches */    Argc = argc;    Argv = argv;    get_some_switches();        /* 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;	if (outname == Nullch)	    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 = Nulline;	    mymaxfuzz = pch_context();	    if (maxfuzz < mymaxfuzz)		mymaxfuzz = maxfuzz;	    if (!skip_rest_of_patch) {		do {		    where = locate_hunk(fuzz);		    if (hunk == 1 && where == Nulline && !force) {						/* dwim for reversed patch? */			if (!pch_swap()) {			    if (fuzz == Nulline)				say1("Not enough memory to try swapped hunk!  Assuming unswapped.\n");			    continue;			}			reverse = !reverse;			where = locate_hunk(fuzz);  /* try again */			if (where == Nulline) {	    /* didn't find it swapped */			    if (!pch_swap())         /* put it back to normal */				fatal1("lost hunk on alloc error!\n");			    reverse = !reverse;			}			else if (noreverse) {			    if (!pch_swap())         /* put it back to normal */				fatal1("lost hunk on alloc error!\n");			    reverse = !reverse;			    say1("Ignoring previously applied (or reversed) patch.\n");			    skip_rest_of_patch = TRUE;			}			else if (batch) {			    if (verbose)				say3("%seversed (or previously applied) patch detected!  %s -R.",				reverse ? "R" : "Unr",				reverse ? "Assuming" : "Ignoring");			}			else {			    ask3("%seversed (or previously applied) patch detected!  %s -R? [y] ",				reverse ? "R" : "Unr",				reverse ? "Assume" : "Ignore");			    if (*buf == 'n') {				ask1("Apply anyway? [n] ");				if (*buf != 'y')				    skip_rest_of_patch = TRUE;				where = Nulline;				reverse = !reverse;				if (!pch_swap())  /* put it back to normal */				    fatal1("lost hunk on alloc error!\n");			    }			}		    }		} while (!skip_rest_of_patch && where == Nulline &&		    ++fuzz <= mymaxfuzz);		if (skip_rest_of_patch) {		/* just got decided */		    Fclose(ofp);		    ofp = Nullfp;		}	    }	    newwhere = pch_newfirst() + last_offset;	    if (skip_rest_of_patch) {		abort_hunk();		failed++;		if (verbose)		    say3("Hunk #%d ignored at %ld.\n", hunk, newwhere);	    }	    else if (where == Nulline) {		abort_hunk();		failed++;		if (verbose)		    say3("Hunk #%d failed at %ld.\n", hunk, newwhere);	    }	    else {		apply_hunk(where);		if (verbose) {		    say3("Hunk #%d succeeded at %ld", hunk, newwhere);		    if (fuzz)			say2(" with fuzz %ld", fuzz);		    if (last_offset)			say3(" (offset %ld line%s)",			    last_offset, last_offset==1L?"":"s");		    say1(".\n");		}	    }	}	if (out_of_mem && using_plan_a) {	    Argc = Argc_last;	    Argv = Argv_last;	    say1("\n\nRan out of memory using Plan A--trying again...\n\n");	    if (ofp)	        Fclose(ofp);	    ofp = Nullfp;	    if (rejfp)	        Fclose(rejfp);	    rejfp = Nullfp;	    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)			say2("Removing %s (empty after patching).\n", realout);		    while (unlink(realout) >= 0) ; /* while is for Eunice.  */		}	    }	}	Fclose(rejfp);	rejfp = Nullfp;	if (failed) {	    failtotal += failed;	    if (!*rejname) {		if (strlcpy(rejname, outname, sizeof(rejname)) >= sizeof(rejname))		    fatal2("filename %s is too long\n", outname);		    #ifndef FLEXFILENAMES		{		    char *s = strrchr(rejname,'/');		    if (!s)			s = rejname;		    if (strlen(s) > 13)			if (s[12] == '.')	/* try to preserve difference */			    s[12] = s[13];	/* between .h, .c, .y, etc. */			s[13] = '\0';		}#endif		if (strlcat(rejname, REJEXT, sizeof(rejname)) >= sizeof(rejname))		    fatal2("filename %s is too long\n", outname);	    }	    if (skip_rest_of_patch) {		say4("%d out of %d hunks ignored--saving rejects to %s\n",		    failed, hunk, rejname);	    }	    else {		say4("%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);    }    if (!patch_seen)    	failtotal++;    my_exit(failtotal);    /* NOTREACHED */}