Home | History | Annotate | Line # | Download | only in patch
patch.c revision 1.24.12.1
      1  1.24.12.1      matt /*	$NetBSD: patch.c,v 1.24.12.1 2007/11/06 23:36:10 matt Exp $	*/
      2        1.4   thorpej 
      3        1.1       cgd /* patch - a program to apply diffs to original files
      4        1.1       cgd  *
      5        1.1       cgd  * Copyright 1986, Larry Wall
      6        1.1       cgd  *
      7        1.9     ragge  * Redistribution and use in source and binary forms, with or without
      8        1.9     ragge  * modification, are permitted provided that the following condition
      9        1.9     ragge  * is met:
     10        1.9     ragge  *  1. Redistributions of source code must retain the above copyright
     11        1.9     ragge  *     notice, this condition and the following disclaimer.
     12        1.9     ragge  *
     13        1.9     ragge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     14        1.9     ragge  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15        1.9     ragge  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     16        1.9     ragge  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     17        1.9     ragge  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     18        1.9     ragge  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     19        1.9     ragge  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     20        1.9     ragge  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     21        1.9     ragge  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     22        1.9     ragge  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     23        1.9     ragge  * SUCH DAMAGE.
     24        1.1       cgd  */
     25        1.2   mycroft 
     26        1.6  christos #include <sys/cdefs.h>
     27        1.2   mycroft #ifndef lint
     28  1.24.12.1      matt __RCSID("$NetBSD: patch.c,v 1.24.12.1 2007/11/06 23:36:10 matt Exp $");
     29        1.2   mycroft #endif /* not lint */
     30        1.1       cgd 
     31        1.1       cgd #include "INTERN.h"
     32        1.1       cgd #include "common.h"
     33        1.1       cgd #include "EXTERN.h"
     34        1.1       cgd #include "version.h"
     35        1.1       cgd #include "util.h"
     36        1.1       cgd #include "pch.h"
     37        1.1       cgd #include "inp.h"
     38        1.1       cgd #include "backupfile.h"
     39        1.1       cgd 
     40        1.3   thorpej #include <stdlib.h>
     41        1.6  christos #include <unistd.h>
     42  1.24.12.1      matt #include <util.h>
     43        1.6  christos 
     44        1.1       cgd /* procedures */
     45       1.10  kristerw static void reinitialize_almost_everything(void);
     46       1.10  kristerw static char *nextarg(void);
     47       1.10  kristerw static int optcmp(const void *, const void *);
     48       1.10  kristerw static char decode_long_option(char *);
     49       1.10  kristerw static void get_some_switches(void);
     50       1.10  kristerw static LINENUM locate_hunk(LINENUM);
     51       1.10  kristerw static void abort_hunk(void);
     52       1.10  kristerw static void apply_hunk(LINENUM);
     53       1.10  kristerw static void init_output(char *);
     54       1.10  kristerw static void init_reject(char *);
     55       1.10  kristerw static void copy_till(LINENUM);
     56       1.10  kristerw static void spew_output(void);
     57       1.10  kristerw static void dump_line(LINENUM);
     58       1.10  kristerw static bool patch_match(LINENUM, LINENUM, LINENUM);
     59       1.18  kristerw static bool similar(const char *, const char *, size_t);
     60       1.10  kristerw int main(int, char *[]);
     61        1.1       cgd 
     62        1.1       cgd /* TRUE if -E was specified on command line.  */
     63        1.1       cgd static int remove_empty_files = FALSE;
     64        1.1       cgd 
     65        1.1       cgd /* TRUE if -R was specified on command line.  */
     66        1.1       cgd static int reverse_flag_specified = FALSE;
     67        1.1       cgd 
     68        1.1       cgd /* Apply a set of diffs as appropriate. */
     69        1.1       cgd 
     70        1.1       cgd int
     71       1.10  kristerw main(int argc, char *argv[])
     72        1.1       cgd {
     73        1.6  christos     LINENUM where = 0;
     74        1.1       cgd     LINENUM newwhere;
     75        1.1       cgd     LINENUM fuzz;
     76        1.1       cgd     LINENUM mymaxfuzz;
     77        1.1       cgd     int hunk = 0;
     78        1.1       cgd     int failed = 0;
     79        1.1       cgd     int failtotal = 0;
     80        1.1       cgd     int i;
     81        1.1       cgd 
     82        1.1       cgd     for (i = 0; i<MAXFILEC; i++)
     83       1.11  kristerw 	filearg[i] = NULL;
     84        1.1       cgd 
     85        1.1       cgd     myuid = getuid();
     86        1.1       cgd 
     87       1.23       skd     check_only = FALSE;
     88       1.23       skd 
     89        1.1       cgd     /* Cons up the names of the temporary files.  */
     90        1.1       cgd     {
     91        1.1       cgd       /* Directory for temporary files.  */
     92       1.18  kristerw       const char *tmpdir;
     93       1.11  kristerw       size_t tmpname_len;
     94        1.1       cgd 
     95        1.1       cgd       tmpdir = getenv ("TMPDIR");
     96        1.1       cgd       if (tmpdir == NULL) {
     97        1.1       cgd 	tmpdir = "/tmp";
     98        1.1       cgd       }
     99        1.1       cgd       tmpname_len = strlen (tmpdir) + 20;
    100        1.1       cgd 
    101       1.12  kristerw       TMPOUTNAME = xmalloc(tmpname_len);
    102       1.19    itojun       strlcpy(TMPOUTNAME, tmpdir, tmpname_len);
    103       1.19    itojun       strlcat(TMPOUTNAME, "/patchoXXXXXX", tmpname_len);
    104        1.5     lukem       if ((i = mkstemp(TMPOUTNAME)) < 0)
    105       1.11  kristerw         pfatal("can't create %s", TMPOUTNAME);
    106        1.5     lukem       Close(i);
    107        1.1       cgd 
    108       1.12  kristerw       TMPINNAME = xmalloc(tmpname_len);
    109       1.19    itojun       strlcpy(TMPINNAME, tmpdir, tmpname_len);
    110       1.19    itojun       strlcat(TMPINNAME, "/patchiXXXXXX", tmpname_len);
    111        1.5     lukem       if ((i = mkstemp(TMPINNAME)) < 0)
    112       1.11  kristerw         pfatal("can't create %s", TMPINNAME);
    113        1.5     lukem       Close(i);
    114        1.1       cgd 
    115       1.12  kristerw       TMPREJNAME = xmalloc(tmpname_len);
    116       1.19    itojun       strlcpy(TMPREJNAME, tmpdir, tmpname_len);
    117       1.19    itojun       strlcat(TMPREJNAME, "/patchrXXXXXX", tmpname_len);
    118        1.5     lukem       if ((i = mkstemp(TMPREJNAME)) < 0)
    119       1.11  kristerw         pfatal("can't create %s", TMPREJNAME);
    120        1.5     lukem       Close(i);
    121        1.1       cgd 
    122       1.12  kristerw       TMPPATNAME = xmalloc(tmpname_len);
    123       1.19    itojun       strlcpy(TMPPATNAME, tmpdir, tmpname_len);
    124       1.19    itojun       strlcat(TMPPATNAME, "/patchpXXXXXX", tmpname_len);
    125        1.5     lukem       if ((i = mkstemp(TMPPATNAME)) < 0)
    126       1.11  kristerw         pfatal("can't create %s", TMPPATNAME);
    127        1.5     lukem       Close(i);
    128        1.1       cgd     }
    129        1.1       cgd 
    130        1.1       cgd     {
    131        1.1       cgd       char *v;
    132        1.1       cgd 
    133        1.1       cgd       v = getenv ("SIMPLE_BACKUP_SUFFIX");
    134        1.1       cgd       if (v)
    135        1.1       cgd 	simple_backup_suffix = v;
    136        1.1       cgd       else
    137        1.1       cgd 	simple_backup_suffix = ORIGEXT;
    138        1.1       cgd #ifndef NODIR
    139        1.1       cgd       v = getenv ("VERSION_CONTROL");
    140        1.1       cgd       backup_type = get_version (v); /* OK to pass NULL. */
    141        1.1       cgd #endif
    142        1.1       cgd     }
    143        1.1       cgd 
    144        1.1       cgd     /* parse switches */
    145        1.1       cgd     Argc = argc;
    146        1.1       cgd     Argv = argv;
    147        1.1       cgd     get_some_switches();
    148        1.1       cgd 
    149        1.1       cgd     /* make sure we clean up /tmp in case of disaster */
    150        1.1       cgd     set_signals(0);
    151        1.1       cgd 
    152        1.1       cgd     for (
    153        1.1       cgd 	open_patch_file(filearg[1]);
    154        1.1       cgd 	there_is_another_patch();
    155        1.1       cgd 	reinitialize_almost_everything()
    156        1.1       cgd     ) {					/* for each patch in patch file */
    157        1.1       cgd 
    158       1.13  kristerw 	if (!skip_rest_of_patch && outname == NULL)
    159       1.12  kristerw 	    outname = xstrdup(filearg[0]);
    160        1.1       cgd 
    161        1.1       cgd 	/* for ed script just up and do it and exit */
    162        1.1       cgd 	if (diff_type == ED_DIFF) {
    163        1.1       cgd 	    do_ed_script();
    164        1.1       cgd 	    continue;
    165        1.1       cgd 	}
    166        1.1       cgd 
    167        1.1       cgd 	/* initialize the patched file */
    168        1.1       cgd 	if (!skip_rest_of_patch)
    169        1.1       cgd 	    init_output(TMPOUTNAME);
    170        1.1       cgd 
    171        1.1       cgd 	/* initialize reject file */
    172        1.1       cgd 	init_reject(TMPREJNAME);
    173        1.1       cgd 
    174        1.1       cgd 	/* find out where all the lines are */
    175        1.1       cgd 	if (!skip_rest_of_patch)
    176        1.1       cgd 	    scan_input(filearg[0]);
    177        1.1       cgd 
    178        1.1       cgd 	/* from here on, open no standard i/o files, because malloc */
    179        1.1       cgd 	/* might misfire and we can't catch it easily */
    180        1.1       cgd 
    181        1.1       cgd 	/* apply each hunk of patch */
    182        1.1       cgd 	hunk = 0;
    183        1.1       cgd 	failed = 0;
    184        1.1       cgd 	while (another_hunk()) {
    185        1.1       cgd 	    hunk++;
    186        1.1       cgd 	    fuzz = Nulline;
    187        1.1       cgd 	    mymaxfuzz = pch_context();
    188        1.1       cgd 	    if (maxfuzz < mymaxfuzz)
    189        1.1       cgd 		mymaxfuzz = maxfuzz;
    190        1.1       cgd 	    if (!skip_rest_of_patch) {
    191        1.1       cgd 		do {
    192        1.1       cgd 		    where = locate_hunk(fuzz);
    193        1.1       cgd 		    if (hunk == 1 && where == Nulline && !force) {
    194        1.1       cgd 						/* dwim for reversed patch? */
    195        1.1       cgd 			if (!pch_swap()) {
    196        1.1       cgd 			    if (fuzz == Nulline)
    197       1.11  kristerw 				say(
    198        1.1       cgd "Not enough memory to try swapped hunk!  Assuming unswapped.\n");
    199        1.1       cgd 			    continue;
    200        1.1       cgd 			}
    201        1.1       cgd 			reverse = !reverse;
    202        1.1       cgd 			where = locate_hunk(fuzz);  /* try again */
    203        1.1       cgd 			if (where == Nulline) {	    /* didn't find it swapped */
    204        1.1       cgd 			    if (!pch_swap())         /* put it back to normal */
    205       1.11  kristerw 				fatal("lost hunk on alloc error!\n");
    206        1.1       cgd 			    reverse = !reverse;
    207        1.1       cgd 			}
    208        1.1       cgd 			else if (noreverse) {
    209        1.1       cgd 			    if (!pch_swap())         /* put it back to normal */
    210       1.11  kristerw 				fatal("lost hunk on alloc error!\n");
    211        1.1       cgd 			    reverse = !reverse;
    212       1.11  kristerw 			    say(
    213        1.1       cgd "Ignoring previously applied (or reversed) patch.\n");
    214        1.1       cgd 			    skip_rest_of_patch = TRUE;
    215        1.1       cgd 			}
    216        1.1       cgd 			else if (batch) {
    217        1.1       cgd 			    if (verbose)
    218       1.11  kristerw 				say(
    219        1.1       cgd "%seversed (or previously applied) patch detected!  %s -R.",
    220        1.1       cgd 				reverse ? "R" : "Unr",
    221        1.1       cgd 				reverse ? "Assuming" : "Ignoring");
    222        1.1       cgd 			}
    223        1.1       cgd 			else {
    224       1.11  kristerw 			    ask(
    225        1.1       cgd "%seversed (or previously applied) patch detected!  %s -R? [y] ",
    226        1.1       cgd 				reverse ? "R" : "Unr",
    227        1.1       cgd 				reverse ? "Assume" : "Ignore");
    228        1.1       cgd 			    if (*buf == 'n') {
    229       1.11  kristerw 				ask("Apply anyway? [n] ");
    230        1.1       cgd 				if (*buf != 'y')
    231        1.1       cgd 				    skip_rest_of_patch = TRUE;
    232        1.1       cgd 				where = Nulline;
    233        1.1       cgd 				reverse = !reverse;
    234        1.1       cgd 				if (!pch_swap())  /* put it back to normal */
    235       1.11  kristerw 				    fatal("lost hunk on alloc error!\n");
    236        1.1       cgd 			    }
    237        1.1       cgd 			}
    238        1.1       cgd 		    }
    239        1.1       cgd 		} while (!skip_rest_of_patch && where == Nulline &&
    240        1.1       cgd 		    ++fuzz <= mymaxfuzz);
    241        1.1       cgd 
    242        1.1       cgd 		if (skip_rest_of_patch) {		/* just got decided */
    243        1.1       cgd 		    Fclose(ofp);
    244       1.11  kristerw 		    ofp = NULL;
    245        1.1       cgd 		}
    246        1.1       cgd 	    }
    247        1.1       cgd 
    248        1.1       cgd 	    newwhere = pch_newfirst() + last_offset;
    249        1.1       cgd 	    if (skip_rest_of_patch) {
    250        1.1       cgd 		abort_hunk();
    251        1.1       cgd 		failed++;
    252        1.1       cgd 		if (verbose)
    253       1.14  kristerw 		    say("Hunk #%d ignored at %d.\n", hunk, newwhere);
    254        1.1       cgd 	    }
    255        1.1       cgd 	    else if (where == Nulline) {
    256        1.1       cgd 		abort_hunk();
    257        1.1       cgd 		failed++;
    258        1.1       cgd 		if (verbose)
    259       1.14  kristerw 		    say("Hunk #%d failed at %d.\n", hunk, newwhere);
    260        1.1       cgd 	    }
    261        1.1       cgd 	    else {
    262        1.1       cgd 		apply_hunk(where);
    263        1.1       cgd 		if (verbose) {
    264       1.14  kristerw 		    say("Hunk #%d succeeded at %d", hunk, newwhere);
    265        1.1       cgd 		    if (fuzz)
    266       1.14  kristerw 			say(" with fuzz %d", fuzz);
    267        1.1       cgd 		    if (last_offset)
    268       1.14  kristerw 			say(" (offset %d line%s)",
    269       1.14  kristerw 			    last_offset, last_offset==1?"":"s");
    270       1.11  kristerw 		    say(".\n");
    271        1.1       cgd 		}
    272        1.1       cgd 	    }
    273        1.1       cgd 	}
    274        1.1       cgd 
    275        1.1       cgd 	assert(hunk);
    276        1.1       cgd 
    277        1.1       cgd 	/* finish spewing out the new file */
    278        1.1       cgd 	if (!skip_rest_of_patch)
    279        1.1       cgd 	    spew_output();
    280        1.1       cgd 
    281        1.1       cgd 	/* and put the output where desired */
    282        1.1       cgd 	ignore_signals();
    283        1.1       cgd 	if (!skip_rest_of_patch) {
    284        1.1       cgd 	    struct stat statbuf;
    285        1.1       cgd 	    char *realout = outname;
    286        1.1       cgd 
    287        1.1       cgd 	    if (move_file(TMPOUTNAME, outname) < 0) {
    288        1.1       cgd 		toutkeep = TRUE;
    289        1.1       cgd 		realout = TMPOUTNAME;
    290        1.1       cgd 		chmod(TMPOUTNAME, filemode);
    291        1.1       cgd 	    }
    292        1.1       cgd 	    else
    293        1.1       cgd 		chmod(outname, filemode);
    294        1.1       cgd 
    295        1.1       cgd 	    if (remove_empty_files && stat(realout, &statbuf) == 0
    296        1.1       cgd 		&& statbuf.st_size == 0) {
    297        1.1       cgd 		if (verbose)
    298       1.11  kristerw 		    say("Removing %s (empty after patching).\n", realout);
    299        1.1       cgd 	        while (unlink(realout) >= 0) ; /* while is for Eunice.  */
    300        1.1       cgd 	    }
    301        1.1       cgd 	}
    302        1.1       cgd 	Fclose(rejfp);
    303       1.11  kristerw 	rejfp = NULL;
    304        1.1       cgd 	if (failed) {
    305        1.1       cgd 	    failtotal += failed;
    306       1.13  kristerw 	    if (outname != NULL) {
    307       1.13  kristerw 		    if (!*rejname) {
    308       1.19    itojun 			    strlcpy(rejname, outname, sizeof(rejname));
    309       1.19    itojun 			    strlcat(rejname, REJEXT, sizeof(rejname));
    310       1.13  kristerw 		    }
    311       1.13  kristerw 		    if (skip_rest_of_patch)
    312       1.13  kristerw 			    say("%d out of %d hunks ignored"
    313       1.13  kristerw 				"--saving rejects to %s\n",
    314       1.13  kristerw 				failed, hunk, rejname);
    315       1.13  kristerw 		    else
    316       1.13  kristerw 			    say("%d out of %d hunks failed"
    317       1.13  kristerw 				"--saving rejects to %s\n",
    318       1.13  kristerw 				failed, hunk, rejname);
    319       1.13  kristerw 		    if (move_file(TMPREJNAME, rejname) < 0)
    320       1.13  kristerw 			    trejkeep = TRUE;
    321       1.13  kristerw 	    } else
    322       1.13  kristerw 		    say("%d out of %d hunks ignored\n", failed, hunk);
    323        1.1       cgd 	}
    324        1.1       cgd 	set_signals(1);
    325        1.1       cgd     }
    326        1.1       cgd     my_exit(failtotal);
    327       1.17  kristerw     /* NOTREACHED */
    328        1.1       cgd }
    329        1.1       cgd 
    330        1.1       cgd /* Prepare to find the next patch to do in the patch file. */
    331        1.1       cgd 
    332        1.6  christos static void
    333       1.10  kristerw reinitialize_almost_everything(void)
    334        1.1       cgd {
    335        1.1       cgd     re_patch();
    336        1.1       cgd     re_input();
    337        1.1       cgd 
    338        1.1       cgd     input_lines = 0;
    339        1.1       cgd     last_frozen_line = 0;
    340        1.1       cgd 
    341        1.1       cgd     filec = 0;
    342       1.15  kristerw     if (filearg[0] != NULL) {
    343        1.1       cgd 	free(filearg[0]);
    344       1.11  kristerw 	filearg[0] = NULL;
    345        1.1       cgd     }
    346        1.1       cgd 
    347       1.11  kristerw     if (outname != NULL) {
    348        1.1       cgd 	free(outname);
    349       1.11  kristerw 	outname = NULL;
    350        1.1       cgd     }
    351        1.1       cgd 
    352        1.1       cgd     last_offset = 0;
    353        1.1       cgd 
    354        1.1       cgd     diff_type = 0;
    355        1.1       cgd 
    356       1.11  kristerw     if (revision != NULL) {
    357        1.1       cgd 	free(revision);
    358       1.11  kristerw 	revision = NULL;
    359        1.1       cgd     }
    360        1.1       cgd 
    361        1.1       cgd     reverse = reverse_flag_specified;
    362        1.1       cgd     skip_rest_of_patch = FALSE;
    363        1.1       cgd 
    364        1.1       cgd     get_some_switches();
    365        1.1       cgd 
    366        1.1       cgd     if (filec >= 2)
    367       1.11  kristerw 	fatal("you may not change to a different patch file\n");
    368        1.1       cgd }
    369        1.1       cgd 
    370        1.1       cgd static char *
    371       1.10  kristerw nextarg(void)
    372        1.1       cgd {
    373        1.1       cgd     if (!--Argc)
    374       1.11  kristerw 	fatal("missing argument after `%s'\n", *Argv);
    375        1.1       cgd     return *++Argv;
    376        1.1       cgd }
    377        1.1       cgd 
    378        1.3   thorpej /* Module for handling of long options. */
    379        1.3   thorpej 
    380        1.3   thorpej struct option {
    381       1.18  kristerw     const char *long_opt;
    382        1.3   thorpej     char short_opt;
    383        1.3   thorpej };
    384        1.3   thorpej 
    385        1.6  christos static int
    386       1.10  kristerw optcmp(const void *va, const void *vb)
    387        1.3   thorpej {
    388        1.6  christos     const struct option *a = va, *b = vb;
    389        1.3   thorpej     return strcmp (a->long_opt, b->long_opt);
    390        1.3   thorpej }
    391        1.3   thorpej 
    392        1.3   thorpej /* Decode Long options beginning with "--" to their short equivalents. */
    393        1.3   thorpej 
    394        1.6  christos static char
    395       1.10  kristerw decode_long_option(char *opt)
    396        1.3   thorpej {
    397        1.3   thorpej     /*
    398        1.3   thorpej      * This table must be sorted on the first field.  We also decode
    399        1.3   thorpej      * unimplemented options as those will probably be dealt with
    400        1.3   thorpej      * later, anyhow.
    401        1.3   thorpej      */
    402        1.3   thorpej     static struct option options[] = {
    403        1.3   thorpej       { "batch",		't' },
    404        1.3   thorpej       { "context",		'c' },
    405        1.3   thorpej       { "debug",		'x' },
    406        1.3   thorpej       { "directory",		'd' },
    407       1.23       skd       { "dry-run",		'C' },
    408        1.3   thorpej       { "ed",			'e' },
    409        1.3   thorpej       { "force",		'f' },
    410        1.3   thorpej       { "forward",		'N' },
    411        1.3   thorpej       { "fuzz",			'F' },
    412        1.3   thorpej       { "ifdef",		'D' },
    413        1.3   thorpej       { "ignore-whitespace",	'l' },
    414        1.3   thorpej       { "normal",		'n' },
    415        1.3   thorpej       { "output",		'o' },
    416        1.8    kleink       { "patchfile",		'i' },
    417        1.3   thorpej       { "prefix",		'B' },
    418        1.3   thorpej       { "quiet",		's' },
    419        1.3   thorpej       { "reject-file",		'r' },
    420        1.3   thorpej       { "remove-empty-files",	'E' },
    421        1.3   thorpej       { "reverse",		'R' },
    422        1.3   thorpej       { "silent",		's' },
    423        1.3   thorpej       { "skip",			'S' },
    424        1.3   thorpej       { "strip",		'p' },
    425        1.3   thorpej       { "suffix",		'b' },
    426        1.3   thorpej       { "unified",		'u' },
    427        1.3   thorpej       { "version",		'v' },
    428        1.3   thorpej       { "version-control",	'V' },
    429        1.3   thorpej     };
    430        1.3   thorpej     struct option key, *found;
    431        1.3   thorpej 
    432        1.3   thorpej     key.long_opt = opt;
    433       1.11  kristerw     found = bsearch(&key, options,
    434        1.3   thorpej 	sizeof(options) / sizeof(options[0]), sizeof(options[0]), optcmp);
    435        1.3   thorpej 
    436        1.3   thorpej     return found ? found->short_opt : '\0';
    437        1.3   thorpej }
    438        1.3   thorpej 
    439        1.1       cgd /* Process switches and filenames up to next '+' or end of list. */
    440        1.1       cgd 
    441        1.6  christos static void
    442       1.10  kristerw get_some_switches(void)
    443        1.1       cgd {
    444       1.10  kristerw     char *s;
    445        1.1       cgd 
    446        1.1       cgd     rejname[0] = '\0';
    447        1.1       cgd     if (!Argc)
    448        1.1       cgd 	return;
    449        1.1       cgd     for (Argc--,Argv++; Argc; Argc--,Argv++) {
    450        1.1       cgd 	s = Argv[0];
    451        1.1       cgd 	if (strEQ(s, "+")) {
    452        1.1       cgd 	    return;			/* + will be skipped by for loop */
    453        1.1       cgd 	}
    454        1.1       cgd 	if (*s != '-' || !s[1]) {
    455        1.1       cgd 	    if (filec == MAXFILEC)
    456       1.11  kristerw 		fatal("too many file arguments\n");
    457       1.11  kristerw 	    if (filec == 1 && filearg[filec] != NULL)
    458       1.11  kristerw 		fatal("-i option and patchfile argument are mutually\
    459        1.8    kleink exclusive\n");
    460       1.12  kristerw 	    filearg[filec++] = xstrdup(s);
    461        1.1       cgd 	}
    462        1.1       cgd 	else {
    463        1.3   thorpej 	    char opt;
    464        1.3   thorpej 
    465        1.3   thorpej 	    if (*(s + 1) == '-') {
    466        1.3   thorpej 		opt = decode_long_option(s + 2);
    467       1.20   mycroft 		s = "";
    468        1.3   thorpej 	    }
    469        1.3   thorpej 	    else
    470        1.3   thorpej 		opt = *++s;
    471        1.3   thorpej 
    472       1.20   mycroft 	    do {
    473       1.20   mycroft 		switch (opt) {
    474       1.20   mycroft 		case 'b':
    475       1.20   mycroft 		    simple_backup_suffix = xstrdup(nextarg());
    476       1.20   mycroft 		    break;
    477       1.20   mycroft 		case 'B':
    478       1.20   mycroft 		    origprae = xstrdup(nextarg());
    479       1.20   mycroft 		    break;
    480       1.20   mycroft 		case 'c':
    481       1.20   mycroft 		    diff_type = CONTEXT_DIFF;
    482       1.20   mycroft 		    break;
    483       1.23       skd 		case 'C':
    484       1.23       skd 		    check_only = TRUE;
    485       1.23       skd 		    break;
    486       1.20   mycroft 		case 'd':
    487       1.20   mycroft 		    if (!*++s)
    488       1.20   mycroft 			s = nextarg();
    489       1.20   mycroft 		    if (chdir(s) < 0)
    490       1.20   mycroft 			pfatal("can't cd to %s", s);
    491       1.20   mycroft 		    s = "";
    492       1.20   mycroft 		    break;
    493       1.20   mycroft 		case 'D':
    494       1.20   mycroft 	    	    do_defines = TRUE;
    495       1.20   mycroft 		    if (!*++s)
    496       1.20   mycroft 			s = nextarg();
    497       1.20   mycroft 		    if (!isalpha((unsigned char)*s) && '_' != *s)
    498       1.20   mycroft 			fatal("argument to -D is not an identifier\n");
    499       1.20   mycroft 		    snprintf(if_defined, sizeof(if_defined), "#ifdef %s\n", s);
    500       1.20   mycroft 		    snprintf(not_defined, sizeof(not_defined), "#ifndef %s\n", s);
    501       1.20   mycroft 		    snprintf(end_defined, sizeof(end_defined),
    502       1.20   mycroft 			"#endif /* %s */\n", s);
    503       1.20   mycroft 		    s = "";
    504       1.20   mycroft 		    break;
    505       1.20   mycroft 		case 'e':
    506       1.20   mycroft 		    diff_type = ED_DIFF;
    507       1.20   mycroft 		    break;
    508       1.20   mycroft 		case 'E':
    509       1.20   mycroft 		    remove_empty_files = TRUE;
    510       1.20   mycroft 		    break;
    511       1.20   mycroft 		case 'f':
    512       1.20   mycroft 		    force = TRUE;
    513       1.20   mycroft 		    break;
    514       1.20   mycroft 		case 'F':
    515       1.20   mycroft 		    if (*++s == '=')
    516       1.20   mycroft 			s++;
    517       1.20   mycroft 		    maxfuzz = atoi(s);
    518       1.20   mycroft 		    s = "";
    519       1.20   mycroft 		    break;
    520       1.20   mycroft 		case 'i':
    521       1.20   mycroft 		    if (filearg[1] != NULL)
    522       1.20   mycroft 			free(filearg[1]);
    523       1.20   mycroft 		    filearg[1] = xstrdup(nextarg());
    524       1.20   mycroft 		    break;
    525       1.20   mycroft 		case 'l':
    526       1.20   mycroft 		    canonicalize = TRUE;
    527       1.20   mycroft 		    break;
    528       1.20   mycroft 		case 'n':
    529       1.20   mycroft 		    diff_type = NORMAL_DIFF;
    530       1.20   mycroft 		    break;
    531       1.20   mycroft 		case 'N':
    532       1.20   mycroft 		    noreverse = TRUE;
    533       1.20   mycroft 		    break;
    534       1.20   mycroft 		case 'o':
    535       1.20   mycroft 		    outname = xstrdup(nextarg());
    536       1.20   mycroft 		    break;
    537       1.20   mycroft 		case 'p':
    538       1.20   mycroft 		    if (*++s == '=')
    539       1.20   mycroft 			s++;
    540       1.20   mycroft 		    strippath = atoi(s);
    541       1.20   mycroft 		    s = "";
    542       1.20   mycroft 		    break;
    543       1.20   mycroft 		case 'r':
    544       1.20   mycroft 		    strlcpy(rejname, nextarg(), sizeof(rejname));
    545       1.20   mycroft 		    break;
    546       1.20   mycroft 		case 'R':
    547       1.20   mycroft 		    reverse = TRUE;
    548       1.20   mycroft 		    reverse_flag_specified = TRUE;
    549       1.20   mycroft 		    break;
    550       1.20   mycroft 		case 's':
    551       1.20   mycroft 		    verbose = FALSE;
    552       1.20   mycroft 		    break;
    553       1.20   mycroft 		case 'S':
    554       1.20   mycroft 		    skip_rest_of_patch = TRUE;
    555       1.20   mycroft 		    break;
    556       1.20   mycroft 		case 't':
    557       1.20   mycroft 		    batch = TRUE;
    558       1.20   mycroft 		    break;
    559       1.20   mycroft 		case 'u':
    560       1.20   mycroft 		    diff_type = UNI_DIFF;
    561       1.20   mycroft 		    break;
    562       1.20   mycroft 		case 'v':
    563       1.20   mycroft 		    version();
    564       1.20   mycroft 		    break;
    565       1.20   mycroft 		case 'V':
    566        1.1       cgd #ifndef NODIR
    567       1.20   mycroft 		    backup_type = get_version (nextarg ());
    568        1.1       cgd #endif
    569       1.20   mycroft 		    break;
    570        1.1       cgd #ifdef DEBUGGING
    571       1.20   mycroft 		case 'x':
    572       1.20   mycroft 		    debug = atoi(s+1);
    573       1.20   mycroft 		    s = "";
    574       1.20   mycroft 		    break;
    575        1.1       cgd #endif
    576       1.20   mycroft 		default:
    577       1.20   mycroft 		    fprintf(stderr, "patch: unrecognized option `%s'\n",
    578       1.20   mycroft 			Argv[0]);
    579       1.20   mycroft 		    fprintf(stderr, "\
    580        1.1       cgd Usage: patch [options] [origfile [patchfile]] [+ [options] [origfile]]...\n\
    581        1.1       cgd Options:\n\
    582       1.24       wiz        [-CcEeflNnRSstuv] [-B backup-prefix] [-b backup-ext] [-d directory]\n\
    583        1.1       cgd        [-D symbol] [-Fmax-fuzz] [-o out-file] [-p[strip-count]]\n\
    584        1.1       cgd        [-r rej-name] [-V {numbered,existing,simple}]\n");
    585       1.20   mycroft 		    my_exit(1);
    586       1.20   mycroft 		}
    587       1.22   mycroft 		opt = *s ? *++s : '\0';
    588       1.20   mycroft 	    } while (opt != '\0');
    589        1.1       cgd 	}
    590        1.1       cgd     }
    591        1.1       cgd }
    592        1.1       cgd 
    593        1.1       cgd /* Attempt to find the right place to apply this hunk of patch. */
    594        1.1       cgd 
    595        1.6  christos static LINENUM
    596       1.10  kristerw locate_hunk(LINENUM fuzz)
    597        1.1       cgd {
    598       1.10  kristerw     LINENUM first_guess = pch_first() + last_offset;
    599       1.10  kristerw     LINENUM offset;
    600        1.1       cgd     LINENUM pat_lines = pch_ptrn_lines();
    601       1.10  kristerw     LINENUM max_pos_offset = input_lines - first_guess
    602        1.1       cgd 				- pat_lines + 1;
    603       1.10  kristerw     LINENUM max_neg_offset = first_guess - last_frozen_line - 1
    604        1.1       cgd 				+ pch_context();
    605        1.1       cgd 
    606        1.1       cgd     if (!pat_lines)			/* null range matches always */
    607        1.1       cgd 	return first_guess;
    608        1.1       cgd     if (max_neg_offset >= first_guess)	/* do not try lines < 0 */
    609        1.1       cgd 	max_neg_offset = first_guess - 1;
    610        1.1       cgd     if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz))
    611        1.1       cgd 	return first_guess;
    612        1.1       cgd     for (offset = 1; ; offset++) {
    613       1.10  kristerw 	bool check_after = (offset <= max_pos_offset);
    614       1.10  kristerw 	bool check_before = (offset <= max_neg_offset);
    615        1.1       cgd 
    616        1.1       cgd 	if (check_after && patch_match(first_guess, offset, fuzz)) {
    617        1.1       cgd #ifdef DEBUGGING
    618        1.1       cgd 	    if (debug & 1)
    619       1.14  kristerw 		say("Offset changing from %d to %d\n", last_offset, offset);
    620        1.1       cgd #endif
    621        1.1       cgd 	    last_offset = offset;
    622        1.1       cgd 	    return first_guess+offset;
    623        1.1       cgd 	}
    624        1.1       cgd 	else if (check_before && patch_match(first_guess, -offset, fuzz)) {
    625        1.1       cgd #ifdef DEBUGGING
    626        1.1       cgd 	    if (debug & 1)
    627       1.14  kristerw 		say("Offset changing from %d to %d\n", last_offset, -offset);
    628        1.1       cgd #endif
    629        1.1       cgd 	    last_offset = -offset;
    630        1.1       cgd 	    return first_guess-offset;
    631        1.1       cgd 	}
    632        1.1       cgd 	else if (!check_before && !check_after)
    633        1.1       cgd 	    return Nulline;
    634        1.1       cgd     }
    635        1.1       cgd }
    636        1.1       cgd 
    637        1.1       cgd /* We did not find the pattern, dump out the hunk so they can handle it. */
    638        1.1       cgd 
    639        1.6  christos static void
    640       1.10  kristerw abort_hunk(void)
    641        1.1       cgd {
    642       1.10  kristerw     LINENUM i;
    643       1.10  kristerw     LINENUM pat_end = pch_end();
    644        1.1       cgd     /* add in last_offset to guess the same as the previous successful hunk */
    645        1.1       cgd     LINENUM oldfirst = pch_first() + last_offset;
    646        1.1       cgd     LINENUM newfirst = pch_newfirst() + last_offset;
    647        1.1       cgd     LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
    648        1.1       cgd     LINENUM newlast = newfirst + pch_repl_lines() - 1;
    649       1.18  kristerw     const char *stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
    650       1.18  kristerw     const char *minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
    651        1.1       cgd 
    652        1.1       cgd     fprintf(rejfp, "***************\n");
    653        1.1       cgd     for (i=0; i<=pat_end; i++) {
    654        1.1       cgd 	switch (pch_char(i)) {
    655        1.1       cgd 	case '*':
    656        1.1       cgd 	    if (oldlast < oldfirst)
    657        1.1       cgd 		fprintf(rejfp, "*** 0%s\n", stars);
    658        1.1       cgd 	    else if (oldlast == oldfirst)
    659       1.14  kristerw 		fprintf(rejfp, "*** %d%s\n", oldfirst, stars);
    660        1.1       cgd 	    else
    661       1.14  kristerw 		fprintf(rejfp, "*** %d,%d%s\n", oldfirst, oldlast, stars);
    662        1.1       cgd 	    break;
    663        1.1       cgd 	case '=':
    664        1.1       cgd 	    if (newlast < newfirst)
    665        1.1       cgd 		fprintf(rejfp, "--- 0%s\n", minuses);
    666        1.1       cgd 	    else if (newlast == newfirst)
    667       1.14  kristerw 		fprintf(rejfp, "--- %d%s\n", newfirst, minuses);
    668        1.1       cgd 	    else
    669       1.14  kristerw 		fprintf(rejfp, "--- %d,%d%s\n", newfirst, newlast, minuses);
    670        1.1       cgd 	    break;
    671        1.1       cgd 	case '\n':
    672        1.1       cgd 	    fprintf(rejfp, "%s", pfetch(i));
    673        1.1       cgd 	    break;
    674        1.1       cgd 	case ' ': case '-': case '+': case '!':
    675        1.1       cgd 	    fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
    676        1.1       cgd 	    break;
    677        1.1       cgd 	default:
    678       1.11  kristerw 	    fatal("fatal internal error in abort_hunk\n");
    679        1.1       cgd 	}
    680        1.1       cgd     }
    681        1.1       cgd }
    682        1.1       cgd 
    683        1.1       cgd /* We found where to apply it (we hope), so do it. */
    684        1.1       cgd 
    685        1.6  christos static void
    686       1.10  kristerw apply_hunk(LINENUM where)
    687        1.1       cgd {
    688       1.10  kristerw     LINENUM old = 1;
    689       1.10  kristerw     LINENUM lastline = pch_ptrn_lines();
    690       1.10  kristerw     LINENUM new = lastline+1;
    691        1.1       cgd #define OUTSIDE 0
    692        1.1       cgd #define IN_IFNDEF 1
    693        1.1       cgd #define IN_IFDEF 2
    694        1.1       cgd #define IN_ELSE 3
    695       1.10  kristerw     int def_state = OUTSIDE;
    696       1.10  kristerw     bool R_do_defines = do_defines;
    697       1.10  kristerw     LINENUM pat_end = pch_end();
    698        1.1       cgd 
    699        1.1       cgd     where--;
    700        1.1       cgd     while (pch_char(new) == '=' || pch_char(new) == '\n')
    701        1.1       cgd 	new++;
    702        1.1       cgd 
    703        1.1       cgd     while (old <= lastline) {
    704        1.1       cgd 	if (pch_char(old) == '-') {
    705        1.1       cgd 	    copy_till(where + old - 1);
    706        1.1       cgd 	    if (R_do_defines) {
    707        1.1       cgd 		if (def_state == OUTSIDE) {
    708        1.1       cgd 		    fputs(not_defined, ofp);
    709        1.1       cgd 		    def_state = IN_IFNDEF;
    710        1.1       cgd 		}
    711        1.1       cgd 		else if (def_state == IN_IFDEF) {
    712        1.1       cgd 		    fputs(else_defined, ofp);
    713        1.1       cgd 		    def_state = IN_ELSE;
    714        1.1       cgd 		}
    715        1.1       cgd 		fputs(pfetch(old), ofp);
    716        1.1       cgd 	    }
    717        1.1       cgd 	    last_frozen_line++;
    718        1.1       cgd 	    old++;
    719        1.1       cgd 	}
    720        1.1       cgd 	else if (new > pat_end) {
    721        1.1       cgd 	    break;
    722        1.1       cgd 	}
    723        1.1       cgd 	else if (pch_char(new) == '+') {
    724        1.1       cgd 	    copy_till(where + old - 1);
    725        1.1       cgd 	    if (R_do_defines) {
    726        1.1       cgd 		if (def_state == IN_IFNDEF) {
    727        1.1       cgd 		    fputs(else_defined, ofp);
    728        1.1       cgd 		    def_state = IN_ELSE;
    729        1.1       cgd 		}
    730        1.1       cgd 		else if (def_state == OUTSIDE) {
    731        1.1       cgd 		    fputs(if_defined, ofp);
    732        1.1       cgd 		    def_state = IN_IFDEF;
    733        1.1       cgd 		}
    734        1.1       cgd 	    }
    735        1.1       cgd 	    fputs(pfetch(new), ofp);
    736        1.1       cgd 	    new++;
    737        1.1       cgd 	}
    738        1.1       cgd 	else if (pch_char(new) != pch_char(old)) {
    739       1.14  kristerw 	    say("Out-of-sync patch, lines %d,%d--mangled text or line numbers, maybe?\n",
    740        1.1       cgd 		pch_hunk_beg() + old,
    741        1.1       cgd 		pch_hunk_beg() + new);
    742        1.1       cgd #ifdef DEBUGGING
    743       1.11  kristerw 	    say("oldchar = '%c', newchar = '%c'\n",
    744        1.1       cgd 		pch_char(old), pch_char(new));
    745        1.1       cgd #endif
    746        1.1       cgd 	    my_exit(1);
    747        1.1       cgd 	}
    748        1.1       cgd 	else if (pch_char(new) == '!') {
    749        1.1       cgd 	    copy_till(where + old - 1);
    750        1.1       cgd 	    if (R_do_defines) {
    751        1.1       cgd 	       fputs(not_defined, ofp);
    752        1.1       cgd 	       def_state = IN_IFNDEF;
    753        1.1       cgd 	    }
    754        1.1       cgd 	    while (pch_char(old) == '!') {
    755        1.1       cgd 		if (R_do_defines) {
    756        1.1       cgd 		    fputs(pfetch(old), ofp);
    757        1.1       cgd 		}
    758        1.1       cgd 		last_frozen_line++;
    759        1.1       cgd 		old++;
    760        1.1       cgd 	    }
    761        1.1       cgd 	    if (R_do_defines) {
    762        1.1       cgd 		fputs(else_defined, ofp);
    763        1.1       cgd 		def_state = IN_ELSE;
    764        1.1       cgd 	    }
    765        1.1       cgd 	    while (pch_char(new) == '!') {
    766        1.1       cgd 		fputs(pfetch(new), ofp);
    767        1.1       cgd 		new++;
    768        1.1       cgd 	    }
    769        1.1       cgd 	}
    770        1.1       cgd 	else {
    771        1.1       cgd 	    assert(pch_char(new) == ' ');
    772        1.1       cgd 	    old++;
    773        1.1       cgd 	    new++;
    774        1.1       cgd 	    if (R_do_defines && def_state != OUTSIDE) {
    775        1.1       cgd 		fputs(end_defined, ofp);
    776        1.1       cgd 		def_state = OUTSIDE;
    777        1.1       cgd 	    }
    778        1.1       cgd 	}
    779        1.1       cgd     }
    780        1.1       cgd     if (new <= pat_end && pch_char(new) == '+') {
    781        1.1       cgd 	copy_till(where + old - 1);
    782        1.1       cgd 	if (R_do_defines) {
    783        1.1       cgd 	    if (def_state == OUTSIDE) {
    784        1.1       cgd 	    	fputs(if_defined, ofp);
    785        1.1       cgd 		def_state = IN_IFDEF;
    786        1.1       cgd 	    }
    787        1.1       cgd 	    else if (def_state == IN_IFNDEF) {
    788        1.1       cgd 		fputs(else_defined, ofp);
    789        1.1       cgd 		def_state = IN_ELSE;
    790        1.1       cgd 	    }
    791        1.1       cgd 	}
    792        1.1       cgd 	while (new <= pat_end && pch_char(new) == '+') {
    793        1.1       cgd 	    fputs(pfetch(new), ofp);
    794        1.1       cgd 	    new++;
    795        1.1       cgd 	}
    796        1.1       cgd     }
    797        1.1       cgd     if (R_do_defines && def_state != OUTSIDE) {
    798        1.1       cgd 	fputs(end_defined, ofp);
    799        1.1       cgd     }
    800        1.1       cgd }
    801        1.1       cgd 
    802        1.1       cgd /* Open the new file. */
    803        1.1       cgd 
    804        1.6  christos static void
    805       1.10  kristerw init_output(char *name)
    806        1.1       cgd {
    807        1.1       cgd     ofp = fopen(name, "w");
    808       1.11  kristerw     if (ofp == NULL)
    809       1.11  kristerw 	pfatal("can't create %s", name);
    810        1.1       cgd }
    811        1.1       cgd 
    812        1.1       cgd /* Open a file to put hunks we can't locate. */
    813        1.1       cgd 
    814        1.6  christos static void
    815       1.10  kristerw init_reject(char *name)
    816        1.1       cgd {
    817        1.1       cgd     rejfp = fopen(name, "w");
    818       1.11  kristerw     if (rejfp == NULL)
    819       1.11  kristerw 	pfatal("can't create %s", name);
    820        1.1       cgd }
    821        1.1       cgd 
    822        1.1       cgd /* Copy input file to output, up to wherever hunk is to be applied. */
    823        1.1       cgd 
    824        1.6  christos static void
    825       1.10  kristerw copy_till(LINENUM lastline)
    826        1.1       cgd {
    827       1.10  kristerw     LINENUM R_last_frozen_line = last_frozen_line;
    828        1.1       cgd 
    829        1.1       cgd     if (R_last_frozen_line > lastline)
    830       1.11  kristerw 	fatal("misordered hunks! output would be garbled\n");
    831        1.1       cgd     while (R_last_frozen_line < lastline) {
    832        1.1       cgd 	dump_line(++R_last_frozen_line);
    833        1.1       cgd     }
    834        1.1       cgd     last_frozen_line = R_last_frozen_line;
    835        1.1       cgd }
    836        1.1       cgd 
    837        1.1       cgd /* Finish copying the input file to the output file. */
    838        1.1       cgd 
    839        1.6  christos static void
    840       1.10  kristerw spew_output(void)
    841        1.1       cgd {
    842        1.1       cgd #ifdef DEBUGGING
    843        1.1       cgd     if (debug & 256)
    844       1.14  kristerw 	say("il=%d lfl=%d\n",input_lines,last_frozen_line);
    845        1.1       cgd #endif
    846        1.1       cgd     if (input_lines)
    847        1.1       cgd 	copy_till(input_lines);		/* dump remainder of file */
    848        1.1       cgd     Fclose(ofp);
    849       1.11  kristerw     ofp = NULL;
    850        1.1       cgd }
    851        1.1       cgd 
    852        1.1       cgd /* Copy one line from input to output. */
    853        1.1       cgd 
    854        1.6  christos static void
    855       1.10  kristerw dump_line(LINENUM line)
    856        1.1       cgd {
    857       1.18  kristerw     const char *s;
    858       1.10  kristerw     char R_newline = '\n';
    859        1.1       cgd 
    860        1.1       cgd     /* Note: string is not null terminated. */
    861       1.16  kristerw     for (s=ifetch(line); putc(*s, ofp) != R_newline; s++) ;
    862        1.1       cgd }
    863        1.1       cgd 
    864        1.1       cgd /* Does the patch pattern match at line base+offset? */
    865        1.1       cgd 
    866        1.6  christos static bool
    867       1.10  kristerw patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
    868       1.10  kristerw {
    869       1.10  kristerw     LINENUM pline = 1 + fuzz;
    870       1.10  kristerw     LINENUM iline;
    871       1.10  kristerw     LINENUM pat_lines = pch_ptrn_lines() - fuzz;
    872        1.1       cgd 
    873        1.1       cgd     for (iline=base+offset+fuzz; pline <= pat_lines; pline++,iline++) {
    874        1.1       cgd 	if (canonicalize) {
    875       1.16  kristerw 	    if (!similar(ifetch(iline),
    876        1.1       cgd 			 pfetch(pline),
    877        1.1       cgd 			 pch_line_len(pline) ))
    878        1.1       cgd 		return FALSE;
    879        1.1       cgd 	}
    880       1.16  kristerw 	else if (strnNE(ifetch(iline),
    881        1.1       cgd 		   pfetch(pline),
    882        1.1       cgd 		   pch_line_len(pline) ))
    883        1.1       cgd 	    return FALSE;
    884        1.1       cgd     }
    885        1.1       cgd     return TRUE;
    886        1.1       cgd }
    887        1.1       cgd 
    888        1.1       cgd /* Do two lines match with canonicalized white space? */
    889        1.1       cgd 
    890        1.6  christos static bool
    891       1.18  kristerw similar(const char *a, const char *b, size_t len)
    892        1.1       cgd {
    893        1.1       cgd     while (len) {
    894        1.7  christos 	if (isspace((unsigned char)*b)) {/* whitespace (or \n) to match? */
    895        1.7  christos 	    if (!isspace((unsigned char)*a))/* no corresponding whitespace? */
    896        1.1       cgd 		return FALSE;
    897        1.7  christos 	    while (len && isspace((unsigned char)*b) && *b != '\n')
    898        1.1       cgd 		b++,len--;		/* skip pattern whitespace */
    899        1.7  christos 	    while (isspace((unsigned char)*a) && *a != '\n')
    900        1.1       cgd 		a++;			/* skip target whitespace */
    901        1.1       cgd 	    if (*a == '\n' || *b == '\n')
    902        1.1       cgd 		return (*a == *b);	/* should end in sync */
    903        1.1       cgd 	}
    904        1.1       cgd 	else if (*a++ != *b++)		/* match non-whitespace chars */
    905        1.1       cgd 	    return FALSE;
    906        1.1       cgd 	else
    907        1.1       cgd 	    len--;			/* probably not necessary */
    908        1.1       cgd     }
    909        1.1       cgd     return TRUE;			/* actually, this is not reached */
    910        1.1       cgd 					/* since there is always a \n */
    911        1.1       cgd }
    912        1.1       cgd 
    913        1.1       cgd /* Exit with cleanup. */
    914        1.1       cgd 
    915  1.24.12.1      matt static void
    916  1.24.12.1      matt my_cleanup(void)
    917        1.1       cgd {
    918        1.1       cgd     Unlink(TMPINNAME);
    919        1.1       cgd     if (!toutkeep) {
    920        1.1       cgd 	Unlink(TMPOUTNAME);
    921        1.1       cgd     }
    922        1.1       cgd     if (!trejkeep) {
    923        1.1       cgd 	Unlink(TMPREJNAME);
    924        1.1       cgd     }
    925        1.1       cgd     Unlink(TMPPATNAME);
    926  1.24.12.1      matt }
    927  1.24.12.1      matt 
    928  1.24.12.1      matt 
    929  1.24.12.1      matt void
    930  1.24.12.1      matt my_exit(int status)
    931  1.24.12.1      matt {
    932  1.24.12.1      matt     my_cleanup();
    933        1.1       cgd     exit(status);
    934        1.1       cgd }
    935  1.24.12.1      matt 
    936  1.24.12.1      matt void
    937  1.24.12.1      matt my_sig_exit(int signo)
    938  1.24.12.1      matt {
    939  1.24.12.1      matt     my_cleanup();
    940  1.24.12.1      matt     (void)raise_default_signal(signo);
    941  1.24.12.1      matt     _exit(1);
    942  1.24.12.1      matt }
    943