Home | History | Annotate | Line # | Download | only in patch
pch.c revision 1.8
      1 /*	$NetBSD: pch.c,v 1.8 2002/03/08 21:57:33 kristerw Exp $	*/
      2 #include <sys/cdefs.h>
      3 #ifndef lint
      4 __RCSID("$NetBSD: pch.c,v 1.8 2002/03/08 21:57:33 kristerw Exp $");
      5 #endif /* not lint */
      6 
      7 #include "EXTERN.h"
      8 #include "common.h"
      9 #include "util.h"
     10 #include "INTERN.h"
     11 #include "pch.h"
     12 
     13 #include <stdlib.h>
     14 #include <unistd.h>
     15 
     16 /* Patch (diff listing) abstract type. */
     17 
     18 static long p_filesize;			/* size of the patch file */
     19 static LINENUM p_first;			/* 1st line number */
     20 static LINENUM p_newfirst;		/* 1st line number of replacement */
     21 static LINENUM p_ptrn_lines;		/* # lines in pattern */
     22 static LINENUM p_repl_lines;		/* # lines in replacement text */
     23 static LINENUM p_end = -1;		/* last line in hunk */
     24 static LINENUM p_max;			/* max allowed value of p_end */
     25 static LINENUM p_context = 3;		/* # of context lines */
     26 static LINENUM p_input_line = 0;	/* current line # from patch file */
     27 static char **p_line = Null(char**);	/* the text of the hunk */
     28 static short *p_len = Null(short*);	/* length of each line */
     29 static char *p_char = Nullch;		/* +, -, and ! */
     30 static int hunkmax = INITHUNKMAX;	/* size of above arrays to begin with */
     31 static int p_indent;			/* indent to patch */
     32 static LINENUM p_base;			/* where to intuit this time */
     33 static LINENUM p_bline;			/* line # of p_base */
     34 static LINENUM p_start;			/* where intuit found a patch */
     35 static LINENUM p_sline;			/* and the line number for it */
     36 static LINENUM p_hunk_beg;		/* line number of current hunk */
     37 static LINENUM p_efake = -1;		/* end of faked up lines--don't free */
     38 static LINENUM p_bfake = -1;		/* beg of faked up lines */
     39 
     40 /* Prepare to look for the next patch in the patch file. */
     41 static void malformed(void);
     42 
     43 void
     44 re_patch(void)
     45 {
     46     p_first = Nulline;
     47     p_newfirst = Nulline;
     48     p_ptrn_lines = Nulline;
     49     p_repl_lines = Nulline;
     50     p_end = (LINENUM)-1;
     51     p_max = Nulline;
     52     p_indent = 0;
     53 }
     54 
     55 /* Open the patch file at the beginning of time. */
     56 
     57 void
     58 open_patch_file(char *filename)
     59 {
     60     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
     61 	pfp = fopen(TMPPATNAME, "w");
     62 	if (pfp == Nullfp)
     63 	    pfatal2("can't create %s", TMPPATNAME);
     64 	while (fgets(buf, sizeof buf, stdin) != Nullch)
     65 	    fputs(buf, pfp);
     66 	Fclose(pfp);
     67 	filename = TMPPATNAME;
     68     }
     69     pfp = fopen(filename, "r");
     70     if (pfp == Nullfp)
     71 	pfatal2("patch file %s not found", filename);
     72     Fstat(fileno(pfp), &filestat);
     73     p_filesize = filestat.st_size;
     74     next_intuit_at(0L,1L);			/* start at the beginning */
     75     set_hunkmax();
     76 }
     77 
     78 /* Make sure our dynamically realloced tables are malloced to begin with. */
     79 
     80 void
     81 set_hunkmax(void)
     82 {
     83     if (p_line == NULL)
     84 	p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
     85     if (p_len == NULL)
     86 	p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
     87     if (p_char == NULL)
     88 	p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
     89 }
     90 
     91 /* Enlarge the arrays containing the current hunk of patch. */
     92 
     93 void
     94 grow_hunkmax(void)
     95 {
     96     hunkmax *= 2;
     97     /*
     98      * Note that on most systems, only the p_line array ever gets fresh memory
     99      * since p_len can move into p_line's old space, and p_char can move into
    100      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
    101      */
    102     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
    103     p_line = (char**) realloc(p_line, (MEM)hunkmax * sizeof(char *));
    104     p_len  = (short*) realloc(p_len,  (MEM)hunkmax * sizeof(short));
    105     p_char = (char*)  realloc(p_char, (MEM)hunkmax * sizeof(char));
    106     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
    107 	return;
    108     if (!using_plan_a)
    109 	fatal1("out of memory\n");
    110     out_of_mem = TRUE;		/* whatever is null will be allocated again */
    111 				/* from within plan_a(), of all places */
    112 }
    113 
    114 /* True if the remainder of the patch file contains a diff of some sort. */
    115 
    116 bool
    117 there_is_another_patch(void)
    118 {
    119     if (p_base != 0L && p_base >= p_filesize) {
    120 	if (verbose)
    121 	    say1("done\n");
    122 	return FALSE;
    123     }
    124     if (verbose)
    125 	say1("Hmm...");
    126     diff_type = intuit_diff_type();
    127     if (!diff_type) {
    128 	if (p_base != 0L) {
    129 	    if (verbose)
    130 		say1("  Ignoring the trailing garbage.\ndone\n");
    131 	}
    132 	else
    133 	    say1("  I can't seem to find a patch in there anywhere.\n");
    134 	return FALSE;
    135     }
    136     if (verbose)
    137 	say3("  %sooks like %s to me...\n",
    138 	    (p_base == 0L ? "L" : "The next patch l"),
    139 	    diff_type == UNI_DIFF ? "a unified diff" :
    140 	    diff_type == CONTEXT_DIFF ? "a context diff" :
    141 	    diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
    142 	    diff_type == NORMAL_DIFF ? "a normal diff" :
    143 	    "an ed script" );
    144     if (p_indent && verbose)
    145 	say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
    146     skip_to(p_start,p_sline);
    147     while (filearg[0] == Nullch) {
    148 	if (force || batch) {
    149 	    say1("No file to patch.  Skipping...\n");
    150 	    filearg[0] = savestr(bestguess);
    151 	    skip_rest_of_patch = TRUE;
    152 	    return TRUE;
    153 	}
    154 	ask1("File to patch: ");
    155 	if (*buf != '\n') {
    156 	    if (bestguess)
    157 		free(bestguess);
    158 	    bestguess = savestr(buf);
    159 	    filearg[0] = fetchname(buf, 0, FALSE);
    160 	}
    161 	if (filearg[0] == Nullch) {
    162 	    ask1("No file found--skip this patch? [n] ");
    163 	    if (*buf != 'y') {
    164 		continue;
    165 	    }
    166 	    if (verbose)
    167 		say1("Skipping patch...\n");
    168 	    filearg[0] = fetchname(bestguess, 0, TRUE);
    169 	    skip_rest_of_patch = TRUE;
    170 	    return TRUE;
    171 	}
    172     }
    173     return TRUE;
    174 }
    175 
    176 /* Determine what kind of diff is in the remaining part of the patch file. */
    177 
    178 int
    179 intuit_diff_type(void)
    180 {
    181     long this_line = 0;
    182     long previous_line;
    183     long first_command_line = -1;
    184     long fcl_line = -1;
    185     bool last_line_was_command = FALSE;
    186     bool this_is_a_command = FALSE;
    187     bool stars_last_line = FALSE;
    188     bool stars_this_line = FALSE;
    189     int indent;
    190     char *s;
    191     char *t;
    192     char *indtmp = Nullch;
    193     char *oldtmp = Nullch;
    194     char *newtmp = Nullch;
    195     char *indname = Nullch;
    196     char *oldname = Nullch;
    197     char *newname = Nullch;
    198     int retval;
    199     bool no_filearg = (filearg[0] == Nullch);
    200 
    201     ok_to_create_file = FALSE;
    202     old_file_is_dev_null = FALSE;
    203     Fseek(pfp, p_base, 0);
    204     p_input_line = p_bline - 1;
    205     for (;;) {
    206 	previous_line = this_line;
    207 	last_line_was_command = this_is_a_command;
    208 	stars_last_line = stars_this_line;
    209 	this_line = ftell(pfp);
    210 	indent = 0;
    211 	p_input_line++;
    212 	if (fgets(buf, sizeof buf, pfp) == Nullch) {
    213 	    if (first_command_line >= 0L) {
    214 					/* nothing but deletes!? */
    215 		p_start = first_command_line;
    216 		p_sline = fcl_line;
    217 		retval = ED_DIFF;
    218 		goto scan_exit;
    219 	    }
    220 	    else {
    221 		p_start = this_line;
    222 		p_sline = p_input_line;
    223 		retval = 0;
    224 		goto scan_exit;
    225 	    }
    226 	}
    227 	for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
    228 	    if (*s == '\t')
    229 		indent += 8 - (indent % 8);
    230 	    else
    231 		indent++;
    232 	}
    233 	for (t=s; isdigit((unsigned char)*t) || *t == ','; t++) ;
    234 	this_is_a_command = (isdigit((unsigned char)*s) &&
    235 	  (*t == 'd' || *t == 'c' || *t == 'a') );
    236 	if (first_command_line < 0L && this_is_a_command) {
    237 	    first_command_line = this_line;
    238 	    fcl_line = p_input_line;
    239 	    p_indent = indent;		/* assume this for now */
    240 	}
    241 	if (!stars_last_line && strnEQ(s, "*** ", 4))
    242 	    oldtmp = savestr(s+4);
    243 	else if (strnEQ(s, "--- ", 4))
    244 	    newtmp = savestr(s+4);
    245 	else if (strnEQ(s, "+++ ", 4))
    246 	    oldtmp = savestr(s+4);	/* pretend it is the old name */
    247 	else if (strnEQ(s, "Index:", 6))
    248 	    indtmp = savestr(s+6);
    249 	else if (strnEQ(s, "Prereq:", 7)) {
    250 	    for (t=s+7; isspace((unsigned char)*t); t++) ;
    251 	    revision = savestr(t);
    252 	    for (t=revision; *t && !isspace((unsigned char)*t); t++) ;
    253 	    *t = '\0';
    254 	    if (!*revision) {
    255 		free(revision);
    256 		revision = Nullch;
    257 	    }
    258 	}
    259 	if ((!diff_type || diff_type == ED_DIFF) &&
    260 	  first_command_line >= 0L &&
    261 	  strEQ(s, ".\n") ) {
    262 	    p_indent = indent;
    263 	    p_start = first_command_line;
    264 	    p_sline = fcl_line;
    265 	    retval = ED_DIFF;
    266 	    goto scan_exit;
    267 	}
    268 	if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
    269 	    if (!atol(s+3))
    270 		ok_to_create_file = TRUE;
    271 	    p_indent = indent;
    272 	    p_start = this_line;
    273 	    p_sline = p_input_line;
    274 	    retval = UNI_DIFF;
    275 	    goto scan_exit;
    276 	}
    277 	stars_this_line = strnEQ(s, "********", 8);
    278 	if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
    279 		 strnEQ(s, "*** ", 4)) {
    280 	    if (!atol(s+4))
    281 		ok_to_create_file = TRUE;
    282 	    /* if this is a new context diff the character just before */
    283 	    /* the newline is a '*'. */
    284 	    while (*s != '\n')
    285 		s++;
    286 	    p_indent = indent;
    287 	    p_start = previous_line;
    288 	    p_sline = p_input_line - 1;
    289 	    retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
    290 	    goto scan_exit;
    291 	}
    292 	if ((!diff_type || diff_type == NORMAL_DIFF) &&
    293 	  last_line_was_command &&
    294 	  (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
    295 	    p_start = previous_line;
    296 	    p_sline = p_input_line - 1;
    297 	    p_indent = indent;
    298 	    retval = NORMAL_DIFF;
    299 	    goto scan_exit;
    300 	}
    301     }
    302   scan_exit:
    303     if (no_filearg) {
    304 	if (indtmp != Nullch)
    305 	    indname = fetchname(indtmp, strippath, ok_to_create_file);
    306 	if (oldtmp != Nullch) {
    307 	    oldname = fetchname(oldtmp, strippath, ok_to_create_file);
    308 	    old_file_is_dev_null = filename_is_dev_null;
    309 	}
    310 	if (newtmp != Nullch)
    311 	    newname = fetchname(newtmp, strippath, ok_to_create_file);
    312 	if (oldname && newname) {
    313 	    if (strlen(oldname) < strlen(newname))
    314 		filearg[0] = savestr(oldname);
    315 	    else
    316 		filearg[0] = savestr(newname);
    317 	}
    318 	else if (oldname)
    319 	    filearg[0] = savestr(oldname);
    320 	else if (newname)
    321 	    filearg[0] = savestr(newname);
    322 	else if (indname)
    323 	    filearg[0] = savestr(indname);
    324     }
    325     if (bestguess) {
    326 	free(bestguess);
    327 	bestguess = Nullch;
    328     }
    329     if (filearg[0] != Nullch)
    330 	bestguess = savestr(filearg[0]);
    331     else if (indtmp != Nullch)
    332 	bestguess = fetchname(indtmp, strippath, TRUE);
    333     else {
    334         if (oldtmp != Nullch) {
    335 	    oldname = fetchname(oldtmp, strippath, TRUE);
    336 	    old_file_is_dev_null = filename_is_dev_null;
    337 	}
    338 	if (newtmp != Nullch)
    339 	    newname = fetchname(newtmp, strippath, TRUE);
    340 	if (oldname && newname) {
    341 	    if (strlen(oldname) < strlen(newname))
    342 		bestguess = savestr(oldname);
    343 	    else
    344 		bestguess = savestr(newname);
    345 	}
    346 	else if (oldname)
    347 	    bestguess = savestr(oldname);
    348 	else if (newname)
    349 	    bestguess = savestr(newname);
    350     }
    351     if (indtmp != Nullch)
    352 	free(indtmp);
    353     if (oldtmp != Nullch)
    354 	free(oldtmp);
    355     if (newtmp != Nullch)
    356 	free(newtmp);
    357     if (indname != Nullch)
    358 	free(indname);
    359     if (oldname != Nullch)
    360 	free(oldname);
    361     if (newname != Nullch)
    362 	free(newname);
    363     return retval;
    364 }
    365 
    366 /* Remember where this patch ends so we know where to start up again. */
    367 
    368 void
    369 next_intuit_at(long file_pos, long file_line)
    370 {
    371     p_base = file_pos;
    372     p_bline = file_line;
    373 }
    374 
    375 /* Basically a verbose fseek() to the actual diff listing. */
    376 
    377 void
    378 skip_to(long file_pos, long file_line)
    379 {
    380     char *ret;
    381 
    382     assert(p_base <= file_pos);
    383     if (verbose && p_base < file_pos) {
    384 	Fseek(pfp, p_base, 0);
    385 	say1("The text leading up to this was:\n--------------------------\n");
    386 	while (ftell(pfp) < file_pos) {
    387 	    ret = fgets(buf, sizeof buf, pfp);
    388 	    assert(ret != Nullch);
    389 	    say2("|%s", buf);
    390 	}
    391 	say1("--------------------------\n");
    392     }
    393     else
    394 	Fseek(pfp, file_pos, 0);
    395     p_input_line = file_line - 1;
    396 }
    397 
    398 /* Make this a function for better debugging.  */
    399 static void
    400 malformed(void)
    401 {
    402     fatal3("malformed patch at line %ld: %s", p_input_line, buf);
    403 		/* about as informative as "Syntax error" in C */
    404 }
    405 
    406 /* True if there is more of the current diff listing to process. */
    407 
    408 bool
    409 another_hunk(void)
    410 {
    411     char *s;
    412     char *ret;
    413     int context = 0;
    414 
    415     while (p_end >= 0) {
    416 	if (p_end == p_efake)
    417 	    p_end = p_bfake;		/* don't free twice */
    418 	else
    419 	    free(p_line[p_end]);
    420 	p_end--;
    421     }
    422     assert(p_end == -1);
    423     p_efake = -1;
    424 
    425     p_max = hunkmax;			/* gets reduced when --- found */
    426     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
    427 	long line_beginning = ftell(pfp);
    428 					/* file pos of the current line */
    429 	LINENUM repl_beginning = 0;	/* index of --- line */
    430 	LINENUM fillcnt = 0;		/* #lines of missing ptrn or repl */
    431 	LINENUM fillsrc = 0;		/* index of first line to copy */
    432 	LINENUM filldst = 0;		/* index of first missing line */
    433 	bool ptrn_spaces_eaten = FALSE;	/* ptrn was slightly misformed */
    434 	bool repl_could_be_missing = TRUE;
    435 					/* no + or ! lines in this hunk */
    436 	bool repl_missing = FALSE;	/* we are now backtracking */
    437 	long repl_backtrack_position = 0;
    438 					/* file pos of first repl line */
    439 	LINENUM repl_patch_line = 0;	/* input line number for same */
    440 	LINENUM ptrn_copiable = 0;	/* # of copiable lines in ptrn */
    441 
    442 	ret = pgets(buf, sizeof buf, pfp);
    443 	p_input_line++;
    444 	if (ret == Nullch || strnNE(buf, "********", 8)) {
    445 	    next_intuit_at(line_beginning,p_input_line);
    446 	    return FALSE;
    447 	}
    448 	p_context = 100;
    449 	p_hunk_beg = p_input_line + 1;
    450 	while (p_end < p_max) {
    451 	    line_beginning = ftell(pfp);
    452 	    ret = pgets(buf, sizeof buf, pfp);
    453 	    p_input_line++;
    454 	    if (ret == Nullch) {
    455 		if (p_max - p_end < 4)
    456 		    Strcpy(buf, "  \n");  /* assume blank lines got chopped */
    457 		else {
    458 		    if (repl_beginning && repl_could_be_missing) {
    459 			repl_missing = TRUE;
    460 			goto hunk_done;
    461 		    }
    462 		    fatal1("unexpected end of file in patch\n");
    463 		}
    464 	    }
    465 	    p_end++;
    466 	    assert(p_end < hunkmax);
    467 	    p_char[p_end] = *buf;
    468 #ifdef zilog
    469 	    p_line[(short)p_end] = Nullch;
    470 #else
    471 	    p_line[p_end] = Nullch;
    472 #endif
    473 	    switch (*buf) {
    474 	    case '*':
    475 		if (strnEQ(buf, "********", 8)) {
    476 		    if (repl_beginning && repl_could_be_missing) {
    477 			repl_missing = TRUE;
    478 			goto hunk_done;
    479 		    }
    480 		    else
    481 			fatal2("unexpected end of hunk at line %ld\n",
    482 			    p_input_line);
    483 		}
    484 		if (p_end != 0) {
    485 		    if (repl_beginning && repl_could_be_missing) {
    486 			repl_missing = TRUE;
    487 			goto hunk_done;
    488 		    }
    489 		    fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
    490 		}
    491 		context = 0;
    492 		p_line[p_end] = savestr(buf);
    493 		if (out_of_mem) {
    494 		    p_end--;
    495 		    return FALSE;
    496 		}
    497 		for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
    498 		if (!*s)
    499 		    malformed ();
    500 		if (strnEQ(s,"0,0",3))
    501 		    strcpy(s,s+2);
    502 		p_first = (LINENUM) atol(s);
    503 		while (isdigit((unsigned char)*s)) s++;
    504 		if (*s == ',') {
    505 		    for (; *s && !isdigit((unsigned char)*s); s++) ;
    506 		    if (!*s)
    507 			malformed ();
    508 		    p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
    509 		}
    510 		else if (p_first)
    511 		    p_ptrn_lines = 1;
    512 		else {
    513 		    p_ptrn_lines = 0;
    514 		    p_first = 1;
    515 		}
    516 		p_max = p_ptrn_lines + 6;	/* we need this much at least */
    517 		while (p_max >= hunkmax)
    518 		    grow_hunkmax();
    519 		p_max = hunkmax;
    520 		break;
    521 	    case '-':
    522 		if (buf[1] == '-') {
    523 		    if (repl_beginning ||
    524 			(p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
    525 		    {
    526 			if (p_end == 1) {
    527 			    /* `old' lines were omitted - set up to fill */
    528 			    /* them in from 'new' context lines. */
    529 			    p_end = p_ptrn_lines + 1;
    530 			    fillsrc = p_end + 1;
    531 			    filldst = 1;
    532 			    fillcnt = p_ptrn_lines;
    533 			}
    534 			else {
    535 			    if (repl_beginning) {
    536 				if (repl_could_be_missing){
    537 				    repl_missing = TRUE;
    538 				    goto hunk_done;
    539 				}
    540 				fatal3(
    541 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
    542 				    p_input_line, p_hunk_beg + repl_beginning);
    543 			    }
    544 			    else {
    545 				fatal4(
    546 "%s \"---\" at line %ld--check line numbers at line %ld\n",
    547 				    (p_end <= p_ptrn_lines
    548 					? "Premature"
    549 					: "Overdue" ),
    550 				    p_input_line, p_hunk_beg);
    551 			    }
    552 			}
    553 		    }
    554 		    repl_beginning = p_end;
    555 		    repl_backtrack_position = ftell(pfp);
    556 		    repl_patch_line = p_input_line;
    557 		    p_line[p_end] = savestr(buf);
    558 		    if (out_of_mem) {
    559 			p_end--;
    560 			return FALSE;
    561 		    }
    562 		    p_char[p_end] = '=';
    563 		    for (s=buf; *s && !isdigit((unsigned char)*s); s++) ;
    564 		    if (!*s)
    565 			malformed ();
    566 		    p_newfirst = (LINENUM) atol(s);
    567 		    while (isdigit((unsigned char)*s)) s++;
    568 		    if (*s == ',') {
    569 			for (; *s && !isdigit((unsigned char)*s); s++) ;
    570 			if (!*s)
    571 			    malformed ();
    572 			p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
    573 		    }
    574 		    else if (p_newfirst)
    575 			p_repl_lines = 1;
    576 		    else {
    577 			p_repl_lines = 0;
    578 			p_newfirst = 1;
    579 		    }
    580 		    p_max = p_repl_lines + p_end;
    581 		    if (p_max > MAXHUNKSIZE)
    582 			fatal4("hunk too large (%ld lines) at line %ld: %s",
    583 			      p_max, p_input_line, buf);
    584 		    while (p_max >= hunkmax)
    585 			grow_hunkmax();
    586 		    if (p_repl_lines != ptrn_copiable
    587 		     && (p_context != 0 || p_repl_lines != 1))
    588 			repl_could_be_missing = FALSE;
    589 		    break;
    590 		}
    591 		goto change_line;
    592 	    case '+':  case '!':
    593 		repl_could_be_missing = FALSE;
    594 	      change_line:
    595 		if (buf[1] == '\n' && canonicalize)
    596 		    strcpy(buf+1," \n");
    597 		if (!isspace((unsigned char)buf[1]) && buf[1] != '>' && buf[1] != '<' &&
    598 		  repl_beginning && repl_could_be_missing) {
    599 		    repl_missing = TRUE;
    600 		    goto hunk_done;
    601 		}
    602 		if (context >= 0) {
    603 		    if (context < p_context)
    604 			p_context = context;
    605 		    context = -1000;
    606 		}
    607 		p_line[p_end] = savestr(buf+2);
    608 		if (out_of_mem) {
    609 		    p_end--;
    610 		    return FALSE;
    611 		}
    612 		break;
    613 	    case '\t': case '\n':	/* assume the 2 spaces got eaten */
    614 		if (repl_beginning && repl_could_be_missing &&
    615 		  (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
    616 		    repl_missing = TRUE;
    617 		    goto hunk_done;
    618 		}
    619 		p_line[p_end] = savestr(buf);
    620 		if (out_of_mem) {
    621 		    p_end--;
    622 		    return FALSE;
    623 		}
    624 		if (p_end != p_ptrn_lines + 1) {
    625 		    ptrn_spaces_eaten |= (repl_beginning != 0);
    626 		    context++;
    627 		    if (!repl_beginning)
    628 			ptrn_copiable++;
    629 		    p_char[p_end] = ' ';
    630 		}
    631 		break;
    632 	    case ' ':
    633 		if (!isspace((unsigned char)buf[1]) &&
    634 		  repl_beginning && repl_could_be_missing) {
    635 		    repl_missing = TRUE;
    636 		    goto hunk_done;
    637 		}
    638 		context++;
    639 		if (!repl_beginning)
    640 		    ptrn_copiable++;
    641 		p_line[p_end] = savestr(buf+2);
    642 		if (out_of_mem) {
    643 		    p_end--;
    644 		    return FALSE;
    645 		}
    646 		break;
    647 	    default:
    648 		if (repl_beginning && repl_could_be_missing) {
    649 		    repl_missing = TRUE;
    650 		    goto hunk_done;
    651 		}
    652 		malformed ();
    653 	    }
    654 	    /* set up p_len for strncmp() so we don't have to */
    655 	    /* assume null termination */
    656 	    if (p_line[p_end])
    657 		p_len[p_end] = strlen(p_line[p_end]);
    658 	    else
    659 		p_len[p_end] = 0;
    660 	}
    661 
    662     hunk_done:
    663 	if (p_end >=0 && !repl_beginning)
    664 	    fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
    665 
    666 	if (repl_missing) {
    667 
    668 	    /* reset state back to just after --- */
    669 	    p_input_line = repl_patch_line;
    670 	    for (p_end--; p_end > repl_beginning; p_end--)
    671 		free(p_line[p_end]);
    672 	    Fseek(pfp, repl_backtrack_position, 0);
    673 
    674 	    /* redundant 'new' context lines were omitted - set */
    675 	    /* up to fill them in from the old file context */
    676 	    if (!p_context && p_repl_lines == 1) {
    677 		p_repl_lines = 0;
    678 		p_max--;
    679 	    }
    680 	    fillsrc = 1;
    681 	    filldst = repl_beginning+1;
    682 	    fillcnt = p_repl_lines;
    683 	    p_end = p_max;
    684 	}
    685 	else if (!p_context && fillcnt == 1) {
    686 	    /* the first hunk was a null hunk with no context */
    687 	    /* and we were expecting one line -- fix it up. */
    688 	    while (filldst < p_end) {
    689 		p_line[filldst] = p_line[filldst+1];
    690 		p_char[filldst] = p_char[filldst+1];
    691 		p_len[filldst] = p_len[filldst+1];
    692 		filldst++;
    693 	    }
    694 #if 0
    695 	    repl_beginning--;		/* this doesn't need to be fixed */
    696 #endif
    697 	    p_end--;
    698 	    p_first++;			/* do append rather than insert */
    699 	    fillcnt = 0;
    700 	    p_ptrn_lines = 0;
    701 	}
    702 
    703 	if (diff_type == CONTEXT_DIFF &&
    704 	  (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
    705 	    if (verbose)
    706 		say4("%s\n%s\n%s\n",
    707 "(Fascinating--this is really a new-style context diff but without",
    708 "the telltale extra asterisks on the *** line that usually indicate",
    709 "the new style...)");
    710 	    diff_type = NEW_CONTEXT_DIFF;
    711 	}
    712 
    713 	/* if there were omitted context lines, fill them in now */
    714 	if (fillcnt) {
    715 	    p_bfake = filldst;		/* remember where not to free() */
    716 	    p_efake = filldst + fillcnt - 1;
    717 	    while (fillcnt-- > 0) {
    718 		while (fillsrc <= p_end && p_char[fillsrc] != ' ')
    719 		    fillsrc++;
    720 		if (fillsrc > p_end)
    721 		    fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
    722 			p_hunk_beg);
    723 		p_line[filldst] = p_line[fillsrc];
    724 		p_char[filldst] = p_char[fillsrc];
    725 		p_len[filldst] = p_len[fillsrc];
    726 		fillsrc++; filldst++;
    727 	    }
    728 	    while (fillsrc <= p_end && fillsrc != repl_beginning &&
    729 	      p_char[fillsrc] != ' ')
    730 		fillsrc++;
    731 #ifdef DEBUGGING
    732 	    if (debug & 64)
    733 		printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
    734 		    fillsrc,filldst,repl_beginning,p_end+1);
    735 #endif
    736 	    assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
    737 	    assert(filldst==p_end+1 || filldst==repl_beginning);
    738 	}
    739     }
    740     else if (diff_type == UNI_DIFF) {
    741 	long line_beginning = ftell(pfp);
    742 					/* file pos of the current line */
    743 	LINENUM fillsrc;		/* index of old lines */
    744 	LINENUM filldst;		/* index of new lines */
    745 	char ch;
    746 
    747 	ret = pgets(buf, sizeof buf, pfp);
    748 	p_input_line++;
    749 	if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
    750 	    next_intuit_at(line_beginning,p_input_line);
    751 	    return FALSE;
    752 	}
    753 	s = buf+4;
    754 	if (!*s)
    755 	    malformed ();
    756 	p_first = (LINENUM) atol(s);
    757 	while (isdigit((unsigned char)*s)) s++;
    758 	if (*s == ',') {
    759 	    p_ptrn_lines = (LINENUM) atol(++s);
    760 	    while (isdigit((unsigned char)*s)) s++;
    761 	} else
    762 	    p_ptrn_lines = 1;
    763 	if (*s == ' ') s++;
    764 	if (*s != '+' || !*++s)
    765 	    malformed ();
    766 	p_newfirst = (LINENUM) atol(s);
    767 	while (isdigit((unsigned char)*s)) s++;
    768 	if (*s == ',') {
    769 	    p_repl_lines = (LINENUM) atol(++s);
    770 	    while (isdigit((unsigned char)*s)) s++;
    771 	} else
    772 	    p_repl_lines = 1;
    773 	if (*s == ' ') s++;
    774 	if (*s != '@')
    775 	    malformed ();
    776 	if (!p_ptrn_lines)
    777 	    p_first++;			/* do append rather than insert */
    778 	p_max = p_ptrn_lines + p_repl_lines + 1;
    779 	while (p_max >= hunkmax)
    780 	    grow_hunkmax();
    781 	fillsrc = 1;
    782 	filldst = fillsrc + p_ptrn_lines;
    783 	p_end = filldst + p_repl_lines;
    784 	Sprintf(buf,"*** %ld,%ld ****\n",p_first,p_first + p_ptrn_lines - 1);
    785 	p_line[0] = savestr(buf);
    786 	if (out_of_mem) {
    787 	    p_end = -1;
    788 	    return FALSE;
    789 	}
    790 	p_char[0] = '*';
    791         Sprintf(buf,"--- %ld,%ld ----\n",p_newfirst,p_newfirst+p_repl_lines-1);
    792 	p_line[filldst] = savestr(buf);
    793 	if (out_of_mem) {
    794 	    p_end = 0;
    795 	    return FALSE;
    796 	}
    797 	p_char[filldst++] = '=';
    798 	p_context = 100;
    799 	context = 0;
    800 	p_hunk_beg = p_input_line + 1;
    801 	while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
    802 	    line_beginning = ftell(pfp);
    803 	    ret = pgets(buf, sizeof buf, pfp);
    804 	    p_input_line++;
    805 	    if (ret == Nullch) {
    806 		if (p_max - filldst < 3)
    807 		    Strcpy(buf, " \n");  /* assume blank lines got chopped */
    808 		else {
    809 		    fatal1("unexpected end of file in patch\n");
    810 		}
    811 	    }
    812 	    if (*buf == '\t' || *buf == '\n') {
    813 		ch = ' ';		/* assume the space got eaten */
    814 		s = savestr(buf);
    815 	    }
    816 	    else {
    817 		ch = *buf;
    818 		s = savestr(buf+1);
    819 	    }
    820 	    if (out_of_mem) {
    821 		while (--filldst > p_ptrn_lines)
    822 		    free(p_line[filldst]);
    823 		p_end = fillsrc-1;
    824 		return FALSE;
    825 	    }
    826 	    switch (ch) {
    827 	    case '-':
    828 		if (fillsrc > p_ptrn_lines) {
    829 		    free(s);
    830 		    p_end = filldst-1;
    831 		    malformed ();
    832 		}
    833 		p_char[fillsrc] = ch;
    834 		p_line[fillsrc] = s;
    835 		p_len[fillsrc++] = strlen(s);
    836 		break;
    837 	    case '=':
    838 		ch = ' ';
    839 		/* FALL THROUGH */
    840 	    case ' ':
    841 		if (fillsrc > p_ptrn_lines) {
    842 		    free(s);
    843 		    while (--filldst > p_ptrn_lines)
    844 			free(p_line[filldst]);
    845 		    p_end = fillsrc-1;
    846 		    malformed ();
    847 		}
    848 		context++;
    849 		p_char[fillsrc] = ch;
    850 		p_line[fillsrc] = s;
    851 		p_len[fillsrc++] = strlen(s);
    852 		s = savestr(s);
    853 		if (out_of_mem) {
    854 		    while (--filldst > p_ptrn_lines)
    855 			free(p_line[filldst]);
    856 		    p_end = fillsrc-1;
    857 		    return FALSE;
    858 		}
    859 		/* FALL THROUGH */
    860 	    case '+':
    861 		if (filldst > p_end) {
    862 		    free(s);
    863 		    while (--filldst > p_ptrn_lines)
    864 			free(p_line[filldst]);
    865 		    p_end = fillsrc-1;
    866 		    malformed ();
    867 		}
    868 		p_char[filldst] = ch;
    869 		p_line[filldst] = s;
    870 		p_len[filldst++] = strlen(s);
    871 		break;
    872 	    default:
    873 		p_end = filldst;
    874 		malformed ();
    875 	    }
    876 	    if (ch != ' ' && context > 0) {
    877 		if (context < p_context)
    878 		    p_context = context;
    879 		context = -1000;
    880 	    }
    881 	}/* while */
    882     }
    883     else {				/* normal diff--fake it up */
    884 	char hunk_type;
    885 	int i;
    886 	LINENUM min, max;
    887 	long line_beginning = ftell(pfp);
    888 
    889 	p_context = 0;
    890 	ret = pgets(buf, sizeof buf, pfp);
    891 	p_input_line++;
    892 	if (ret == Nullch || !isdigit((unsigned char)*buf)) {
    893 	    next_intuit_at(line_beginning,p_input_line);
    894 	    return FALSE;
    895 	}
    896 	p_first = (LINENUM)atol(buf);
    897 	for (s=buf; isdigit((unsigned char)*s); s++) ;
    898 	if (*s == ',') {
    899 	    p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
    900 	    while (isdigit((unsigned char)*s)) s++;
    901 	}
    902 	else
    903 	    p_ptrn_lines = (*s != 'a');
    904 	hunk_type = *s;
    905 	if (hunk_type == 'a')
    906 	    p_first++;			/* do append rather than insert */
    907 	min = (LINENUM)atol(++s);
    908 	for (; isdigit((unsigned char)*s); s++) ;
    909 	if (*s == ',')
    910 	    max = (LINENUM)atol(++s);
    911 	else
    912 	    max = min;
    913 	if (hunk_type == 'd')
    914 	    min++;
    915 	p_end = p_ptrn_lines + 1 + max - min + 1;
    916 	if (p_end > MAXHUNKSIZE)
    917 	    fatal4("hunk too large (%ld lines) at line %ld: %s",
    918 		  p_end, p_input_line, buf);
    919 	while (p_end >= hunkmax)
    920 	    grow_hunkmax();
    921 	p_newfirst = min;
    922 	p_repl_lines = max - min + 1;
    923 	Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
    924 	p_line[0] = savestr(buf);
    925 	if (out_of_mem) {
    926 	    p_end = -1;
    927 	    return FALSE;
    928 	}
    929 	p_char[0] = '*';
    930 	for (i=1; i<=p_ptrn_lines; i++) {
    931 	    ret = pgets(buf, sizeof buf, pfp);
    932 	    p_input_line++;
    933 	    if (ret == Nullch)
    934 		fatal2("unexpected end of file in patch at line %ld\n",
    935 		  p_input_line);
    936 	    if (*buf != '<')
    937 		fatal2("< expected at line %ld of patch\n", p_input_line);
    938 	    p_line[i] = savestr(buf+2);
    939 	    if (out_of_mem) {
    940 		p_end = i-1;
    941 		return FALSE;
    942 	    }
    943 	    p_len[i] = strlen(p_line[i]);
    944 	    p_char[i] = '-';
    945 	}
    946 	if (hunk_type == 'c') {
    947 	    ret = pgets(buf, sizeof buf, pfp);
    948 	    p_input_line++;
    949 	    if (ret == Nullch)
    950 		fatal2("unexpected end of file in patch at line %ld\n",
    951 		    p_input_line);
    952 	    if (*buf != '-')
    953 		fatal2("--- expected at line %ld of patch\n", p_input_line);
    954 	}
    955 	Sprintf(buf, "--- %ld,%ld\n", min, max);
    956 	p_line[i] = savestr(buf);
    957 	if (out_of_mem) {
    958 	    p_end = i-1;
    959 	    return FALSE;
    960 	}
    961 	p_char[i] = '=';
    962 	for (i++; i<=p_end; i++) {
    963 	    ret = pgets(buf, sizeof buf, pfp);
    964 	    p_input_line++;
    965 	    if (ret == Nullch)
    966 		fatal2("unexpected end of file in patch at line %ld\n",
    967 		    p_input_line);
    968 	    if (*buf != '>')
    969 		fatal2("> expected at line %ld of patch\n", p_input_line);
    970 	    p_line[i] = savestr(buf+2);
    971 	    if (out_of_mem) {
    972 		p_end = i-1;
    973 		return FALSE;
    974 	    }
    975 	    p_len[i] = strlen(p_line[i]);
    976 	    p_char[i] = '+';
    977 	}
    978     }
    979     if (reverse)			/* backwards patch? */
    980 	if (!pch_swap())
    981 	    say1("Not enough memory to swap next hunk!\n");
    982 #ifdef DEBUGGING
    983     if (debug & 2) {
    984 	int i;
    985 	char special;
    986 
    987 	for (i=0; i <= p_end; i++) {
    988 	    if (i == p_ptrn_lines)
    989 		special = '^';
    990 	    else
    991 		special = ' ';
    992 	    fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
    993 	    Fflush(stderr);
    994 	}
    995     }
    996 #endif
    997     if (p_end+1 < hunkmax)	/* paranoia reigns supreme... */
    998 	p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
    999     return TRUE;
   1000 }
   1001 
   1002 /* Input a line from the patch file, worrying about indentation. */
   1003 
   1004 char *
   1005 pgets(char *bf, int sz, FILE *fp)
   1006 {
   1007     char *ret = fgets(bf, sz, fp);
   1008     char *s;
   1009     int indent = 0;
   1010 
   1011     if (p_indent && ret != Nullch) {
   1012 	for (s=buf;
   1013 	  indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
   1014 	    if (*s == '\t')
   1015 		indent += 8 - (indent % 7);
   1016 	    else
   1017 		indent++;
   1018 	}
   1019 	if (buf != s)
   1020 	    Strcpy(buf, s);
   1021     }
   1022     return ret;
   1023 }
   1024 
   1025 /* Reverse the old and new portions of the current hunk. */
   1026 
   1027 bool
   1028 pch_swap(void)
   1029 {
   1030     char **tp_line;		/* the text of the hunk */
   1031     short *tp_len;		/* length of each line */
   1032     char *tp_char;		/* +, -, and ! */
   1033     LINENUM i;
   1034     LINENUM n;
   1035     bool blankline = FALSE;
   1036     char *s;
   1037 
   1038     i = p_first;
   1039     p_first = p_newfirst;
   1040     p_newfirst = i;
   1041 
   1042     /* make a scratch copy */
   1043 
   1044     tp_line = p_line;
   1045     tp_len = p_len;
   1046     tp_char = p_char;
   1047     p_line = Null(char**);	/* force set_hunkmax to allocate again */
   1048     p_len = Null(short*);
   1049     p_char = Nullch;
   1050     set_hunkmax();
   1051     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
   1052 	if (p_line == Null(char**))
   1053 	    free(p_line);
   1054 	p_line = tp_line;
   1055 	if (p_len == Null(short*))
   1056 	    free(p_len);
   1057 	p_len = tp_len;
   1058 	if (p_char == Nullch)
   1059 	    free(p_char);
   1060 	p_char = tp_char;
   1061 	return FALSE;		/* not enough memory to swap hunk! */
   1062     }
   1063 
   1064     /* now turn the new into the old */
   1065 
   1066     i = p_ptrn_lines + 1;
   1067     if (tp_char[i] == '\n') {		/* account for possible blank line */
   1068 	blankline = TRUE;
   1069 	i++;
   1070     }
   1071     if (p_efake >= 0) {			/* fix non-freeable ptr range */
   1072 	if (p_efake <= i)
   1073 	    n = p_end - i + 1;
   1074 	else
   1075 	    n = -i;
   1076 	p_efake += n;
   1077 	p_bfake += n;
   1078     }
   1079     for (n=0; i <= p_end; i++,n++) {
   1080 	p_line[n] = tp_line[i];
   1081 	p_char[n] = tp_char[i];
   1082 	if (p_char[n] == '+')
   1083 	    p_char[n] = '-';
   1084 	p_len[n] = tp_len[i];
   1085     }
   1086     if (blankline) {
   1087 	i = p_ptrn_lines + 1;
   1088 	p_line[n] = tp_line[i];
   1089 	p_char[n] = tp_char[i];
   1090 	p_len[n] = tp_len[i];
   1091 	n++;
   1092     }
   1093     assert(p_char[0] == '=');
   1094     p_char[0] = '*';
   1095     for (s=p_line[0]; *s; s++)
   1096 	if (*s == '-')
   1097 	    *s = '*';
   1098 
   1099     /* now turn the old into the new */
   1100 
   1101     assert(tp_char[0] == '*');
   1102     tp_char[0] = '=';
   1103     for (s=tp_line[0]; *s; s++)
   1104 	if (*s == '*')
   1105 	    *s = '-';
   1106     for (i=0; n <= p_end; i++,n++) {
   1107 	p_line[n] = tp_line[i];
   1108 	p_char[n] = tp_char[i];
   1109 	if (p_char[n] == '-')
   1110 	    p_char[n] = '+';
   1111 	p_len[n] = tp_len[i];
   1112     }
   1113     assert(i == p_ptrn_lines + 1);
   1114     i = p_ptrn_lines;
   1115     p_ptrn_lines = p_repl_lines;
   1116     p_repl_lines = i;
   1117     if (tp_line == Null(char**))
   1118 	free(tp_line);
   1119     if (tp_len == Null(short*))
   1120 	free(tp_len);
   1121     if (tp_char == Nullch)
   1122 	free(tp_char);
   1123     return TRUE;
   1124 }
   1125 
   1126 /* Return the specified line position in the old file of the old context. */
   1127 
   1128 LINENUM
   1129 pch_first(void)
   1130 {
   1131     return p_first;
   1132 }
   1133 
   1134 /* Return the number of lines of old context. */
   1135 
   1136 LINENUM
   1137 pch_ptrn_lines(void)
   1138 {
   1139     return p_ptrn_lines;
   1140 }
   1141 
   1142 /* Return the probable line position in the new file of the first line. */
   1143 
   1144 LINENUM
   1145 pch_newfirst(void)
   1146 {
   1147     return p_newfirst;
   1148 }
   1149 
   1150 /* Return the number of lines in the replacement text including context. */
   1151 
   1152 LINENUM
   1153 pch_repl_lines(void)
   1154 {
   1155     return p_repl_lines;
   1156 }
   1157 
   1158 /* Return the number of lines in the whole hunk. */
   1159 
   1160 LINENUM
   1161 pch_end(void)
   1162 {
   1163     return p_end;
   1164 }
   1165 
   1166 /* Return the number of context lines before the first changed line. */
   1167 
   1168 LINENUM
   1169 pch_context(void)
   1170 {
   1171     return p_context;
   1172 }
   1173 
   1174 /* Return the length of a particular patch line. */
   1175 
   1176 short
   1177 pch_line_len(LINENUM line)
   1178 {
   1179     return p_len[line];
   1180 }
   1181 
   1182 /* Return the control character (+, -, *, !, etc) for a patch line. */
   1183 
   1184 char
   1185 pch_char(LINENUM line)
   1186 {
   1187     return p_char[line];
   1188 }
   1189 
   1190 /* Return a pointer to a particular patch line. */
   1191 
   1192 char *
   1193 pfetch(LINENUM line)
   1194 {
   1195     return p_line[line];
   1196 }
   1197 
   1198 /* Return where in the patch file this hunk began, for error messages. */
   1199 
   1200 LINENUM
   1201 pch_hunk_beg(void)
   1202 {
   1203     return p_hunk_beg;
   1204 }
   1205 
   1206 /* Apply an ed script by feeding ed itself. */
   1207 
   1208 void
   1209 do_ed_script(void)
   1210 {
   1211     char *t;
   1212     long beginning_of_this_line;
   1213     bool this_line_is_command = FALSE;
   1214     FILE *pipefp = NULL;
   1215 
   1216     if (!skip_rest_of_patch) {
   1217 	Unlink(TMPOUTNAME);
   1218 	copy_file(filearg[0], TMPOUTNAME);
   1219 	if (verbose)
   1220 	    Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
   1221 	else
   1222 	    Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
   1223 	pipefp = popen(buf, "w");
   1224     }
   1225     for (;;) {
   1226 	beginning_of_this_line = ftell(pfp);
   1227 	if (pgets(buf, sizeof buf, pfp) == Nullch) {
   1228 	    next_intuit_at(beginning_of_this_line,p_input_line);
   1229 	    break;
   1230 	}
   1231 	p_input_line++;
   1232 	for (t=buf; isdigit((unsigned char)*t) || *t == ','; t++) ;
   1233 	this_line_is_command = (isdigit((unsigned char)*buf) &&
   1234 	  (*t == 'd' || *t == 'c' || *t == 'a') );
   1235 	if (this_line_is_command) {
   1236 	    if (!skip_rest_of_patch)
   1237 		fputs(buf, pipefp);
   1238 	    if (*t != 'd') {
   1239 		while (pgets(buf, sizeof buf, pfp) != Nullch) {
   1240 		    p_input_line++;
   1241 		    if (!skip_rest_of_patch)
   1242 			fputs(buf, pipefp);
   1243 		    if (strEQ(buf, ".\n"))
   1244 			break;
   1245 		}
   1246 	    }
   1247 	}
   1248 	else {
   1249 	    next_intuit_at(beginning_of_this_line,p_input_line);
   1250 	    break;
   1251 	}
   1252     }
   1253     if (skip_rest_of_patch)
   1254 	return;
   1255     fprintf(pipefp, "w\n");
   1256     fprintf(pipefp, "q\n");
   1257     Fflush(pipefp);
   1258     Pclose(pipefp);
   1259     ignore_signals();
   1260     if (move_file(TMPOUTNAME, outname) < 0) {
   1261 	toutkeep = TRUE;
   1262 	chmod(TMPOUTNAME, filemode);
   1263     }
   1264     else
   1265 	chmod(outname, filemode);
   1266     set_signals(1);
   1267 }
   1268