[BACK]Return to patch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / patch

Annotation of src/usr.bin/patch/patch.c, Revision 1.68

1.68    ! deraadt     1: /*     $OpenBSD: patch.c,v 1.67 2019/06/28 05:35:35 deraadt Exp $      */
1.2       niklas      2:
1.20      deraadt     3: /*
                      4:  * patch - a program to apply diffs to original files
                      5:  *
1.1       deraadt     6:  * Copyright 1986, Larry Wall
1.20      deraadt     7:  *
1.14      niklas      8:  * Redistribution and use in source and binary forms, with or without
1.20      deraadt     9:  * modification, are permitted provided that the following condition is met:
                     10:  * 1. Redistributions of source code must retain the above copyright notice,
                     11:  * this condition and the following disclaimer.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
                     14:  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     15:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     16:  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
                     17:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     18:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     19:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
                     20:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.14      niklas     21:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     22:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     23:  * SUCH DAMAGE.
1.20      deraadt    24:  *
                     25:  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
                     26:  * behaviour
1.1       deraadt    27:  */
                     28:
1.23      otto       29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31: #include <unistd.h>
                     32:
                     33: #include <ctype.h>
1.24      millert    34: #include <getopt.h>
1.25      millert    35: #include <limits.h>
1.65      anton      36: #include <paths.h>
1.29      otto       37: #include <stdio.h>
1.23      otto       38: #include <string.h>
                     39: #include <stdlib.h>
                     40:
1.1       deraadt    41: #include "common.h"
                     42: #include "util.h"
                     43: #include "pch.h"
                     44: #include "inp.h"
                     45: #include "backupfile.h"
1.60      tobias     46: #include "ed.h"
1.1       deraadt    47:
1.44      otto       48: mode_t         filemode = 0644;
1.29      otto       49:
                     50: char           buf[MAXLINELEN];        /* general purpose buffer */
                     51:
1.35      otto       52: bool           using_plan_a = true;    /* try to keep everything in memory */
                     53: bool           out_of_mem = false;     /* ran out of memory in plan a */
1.29      otto       54:
                     55: #define MAXFILEC 2
                     56:
                     57: char           *filearg[MAXFILEC];
1.35      otto       58: bool           ok_to_create_file = false;
1.29      otto       59: char           *outname = NULL;
                     60: char           *origprae = NULL;
                     61: char           *TMPOUTNAME;
                     62: char           *TMPINNAME;
                     63: char           *TMPREJNAME;
                     64: char           *TMPPATNAME;
1.35      otto       65: bool           toutkeep = false;
                     66: bool           trejkeep = false;
1.33      otto       67: bool           warn_on_invalid_line;
1.37      otto       68: bool           last_line_missing_eol;
1.29      otto       69:
                     70: #ifdef DEBUGGING
1.30      deraadt    71: int            debug = 0;
1.29      otto       72: #endif
                     73:
1.35      otto       74: bool           force = false;
                     75: bool           batch = false;
                     76: bool           verbose = true;
                     77: bool           reverse = false;
                     78: bool           noreverse = false;
                     79: bool           skip_rest_of_patch = false;
1.29      otto       80: int            strippath = 957;
1.35      otto       81: bool           canonicalize = false;
                     82: bool           check_only = false;
1.29      otto       83: int            diff_type = 0;
                     84: char           *revision = NULL;       /* prerequisite revision, if any */
                     85: LINENUM                input_lines = 0;        /* how long is input file in lines */
1.38      millert    86: int            posix = 0;              /* strict POSIX mode? */
1.23      otto       87:
                     88: static void    reinitialize_almost_everything(void);
                     89: static void    get_some_switches(void);
                     90: static LINENUM locate_hunk(LINENUM);
1.43      otto       91: static void    abort_context_hunk(void);
                     92: static void    rej_line(int, LINENUM);
1.23      otto       93: static void    abort_hunk(void);
                     94: static void    apply_hunk(LINENUM);
1.29      otto       95: static void    init_output(const char *);
                     96: static void    init_reject(const char *);
1.37      otto       97: static void    copy_till(LINENUM, bool);
1.23      otto       98: static void    spew_output(void);
1.37      otto       99: static void    dump_line(LINENUM, bool);
1.23      otto      100: static bool    patch_match(LINENUM, LINENUM, LINENUM);
1.29      otto      101: static bool    similar(const char *, const char *, int);
1.24      millert   102: static __dead void usage(void);
1.1       deraadt   103:
1.35      otto      104: /* true if -E was specified on command line.  */
                    105: static bool    remove_empty_files = false;
1.1       deraadt   106:
1.35      otto      107: /* true if -R was specified on command line.  */
                    108: static bool    reverse_flag_specified = false;
1.1       deraadt   109:
1.25      millert   110: /* buffer holding the name of the rejected patch file. */
1.64      deraadt   111: static char    rejname[PATH_MAX];
1.25      millert   112:
1.29      otto      113: /* how many input lines have been irretractibly output */
                    114: static LINENUM last_frozen_line = 0;
                    115:
                    116: static int     Argc;           /* guess */
                    117: static char    **Argv;
                    118: static int     Argc_last;      /* for restarting plan_b */
                    119: static char    **Argv_last;
                    120:
                    121: static FILE    *ofp = NULL;    /* output file pointer */
                    122: static FILE    *rejfp = NULL;  /* reject file pointer */
                    123:
                    124: static int     filec = 0;      /* how many file arguments? */
                    125: static LINENUM last_offset = 0;
                    126: static LINENUM maxfuzz = 2;
                    127:
                    128: /* patch using ifdef, ifndef, etc. */
1.35      otto      129: static bool            do_defines = false;
1.29      otto      130: /* #ifdef xyzzy */
                    131: static char            if_defined[128];
                    132: /* #ifndef xyzzy */
                    133: static char            not_defined[128];
                    134: /* #else */
                    135: static const char      else_defined[] = "#else\n";
                    136: /* #endif xyzzy */
                    137: static char            end_defined[128];
                    138:
1.11      espie     139:
1.1       deraadt   140: /* Apply a set of diffs as appropriate. */
                    141:
                    142: int
1.19      deraadt   143: main(int argc, char *argv[])
1.1       deraadt   144: {
1.42      deraadt   145:        int     error = 0, hunk, failed, i, fd;
1.47      stsp      146:        bool    patch_seen;
1.35      otto      147:        LINENUM where = 0, newwhere, fuzz, mymaxfuzz;
1.31      millert   148:        const   char *tmpdir;
                    149:        char    *v;
1.55      deraadt   150:
1.61      deraadt   151:        if (pledge("stdio rpath wpath cpath tmppath fattr", NULL) == -1) {
1.59      deraadt   152:                perror("pledge");
1.62      gsoares   153:                my_exit(2);
1.61      deraadt   154:        }
1.20      deraadt   155:
1.52      millert   156:        setvbuf(stdout, NULL, _IOLBF, 0);
                    157:        setvbuf(stderr, NULL, _IOLBF, 0);
1.20      deraadt   158:        for (i = 0; i < MAXFILEC; i++)
1.23      otto      159:                filearg[i] = NULL;
1.20      deraadt   160:
                    161:        /* Cons up the names of the temporary files.  */
1.31      millert   162:        if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
                    163:                tmpdir = _PATH_TMP;
                    164:        for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
                    165:                ;
                    166:        i++;
                    167:        if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
1.20      deraadt   168:                fatal("cannot allocate memory");
1.67      deraadt   169:        if ((fd = mkstemp(TMPOUTNAME)) == -1)
1.20      deraadt   170:                pfatal("can't create %s", TMPOUTNAME);
1.31      millert   171:        close(fd);
1.20      deraadt   172:
1.31      millert   173:        if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
1.20      deraadt   174:                fatal("cannot allocate memory");
1.67      deraadt   175:        if ((fd = mkstemp(TMPINNAME)) == -1)
1.20      deraadt   176:                pfatal("can't create %s", TMPINNAME);
1.31      millert   177:        close(fd);
1.20      deraadt   178:
1.31      millert   179:        if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
1.20      deraadt   180:                fatal("cannot allocate memory");
1.67      deraadt   181:        if ((fd = mkstemp(TMPREJNAME)) == -1)
1.20      deraadt   182:                pfatal("can't create %s", TMPREJNAME);
1.31      millert   183:        close(fd);
1.20      deraadt   184:
1.31      millert   185:        if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
1.20      deraadt   186:                fatal("cannot allocate memory");
1.67      deraadt   187:        if ((fd = mkstemp(TMPPATNAME)) == -1)
1.20      deraadt   188:                pfatal("can't create %s", TMPPATNAME);
1.31      millert   189:        close(fd);
1.20      deraadt   190:
                    191:        v = getenv("SIMPLE_BACKUP_SUFFIX");
                    192:        if (v)
                    193:                simple_backup_suffix = v;
                    194:        else
                    195:                simple_backup_suffix = ORIGEXT;
                    196:
                    197:        /* parse switches */
                    198:        Argc = argc;
                    199:        Argv = argv;
                    200:        get_some_switches();
                    201:
1.27      millert   202:        if (backup_type == none) {
1.38      millert   203:                if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
                    204:                        v = getenv("VERSION_CONTROL");
                    205:                if (v != NULL || !posix)
1.27      millert   206:                        backup_type = get_version(v);   /* OK to pass NULL. */
                    207:        }
                    208:
1.20      deraadt   209:        /* make sure we clean up /tmp in case of disaster */
                    210:        set_signals(0);
                    211:
1.47      stsp      212:        patch_seen = false;
1.20      deraadt   213:        for (open_patch_file(filearg[1]); there_is_another_patch();
                    214:            reinitialize_almost_everything()) {
                    215:                /* for each patch in patch file */
1.53      deraadt   216:
1.47      stsp      217:                patch_seen = true;
1.20      deraadt   218:
1.35      otto      219:                warn_on_invalid_line = true;
1.20      deraadt   220:
1.23      otto      221:                if (outname == NULL)
1.54      tobias    222:                        outname = xstrdup(filearg[0]);
1.1       deraadt   223:
1.20      deraadt   224:                /* initialize the patched file */
                    225:                if (!skip_rest_of_patch)
                    226:                        init_output(TMPOUTNAME);
                    227:
                    228:                /* initialize reject file */
                    229:                init_reject(TMPREJNAME);
                    230:
                    231:                /* find out where all the lines are */
                    232:                if (!skip_rest_of_patch)
                    233:                        scan_input(filearg[0]);
1.60      tobias    234:
                    235:                /* for ed script just up and do it and exit */
                    236:                if (diff_type == ED_DIFF) {
                    237:                        do_ed_script();
                    238:                        continue;
                    239:                }
1.20      deraadt   240:
                    241:                /* from here on, open no standard i/o files, because malloc */
                    242:                /* might misfire and we can't catch it easily */
                    243:
                    244:                /* apply each hunk of patch */
                    245:                hunk = 0;
                    246:                failed = 0;
1.35      otto      247:                out_of_mem = false;
1.20      deraadt   248:                while (another_hunk()) {
                    249:                        hunk++;
1.29      otto      250:                        fuzz = 0;
1.20      deraadt   251:                        mymaxfuzz = pch_context();
                    252:                        if (maxfuzz < mymaxfuzz)
                    253:                                mymaxfuzz = maxfuzz;
                    254:                        if (!skip_rest_of_patch) {
                    255:                                do {
                    256:                                        where = locate_hunk(fuzz);
1.29      otto      257:                                        if (hunk == 1 && where == 0 && !force) {
1.1       deraadt   258:                                                /* dwim for reversed patch? */
1.20      deraadt   259:                                                if (!pch_swap()) {
1.29      otto      260:                                                        if (fuzz == 0)
1.20      deraadt   261:                                                                say("Not enough memory to try swapped hunk!  Assuming unswapped.\n");
                    262:                                                        continue;
                    263:                                                }
                    264:                                                reverse = !reverse;
                    265:                                                /* try again */
                    266:                                                where = locate_hunk(fuzz);
1.29      otto      267:                                                if (where == 0) {
1.20      deraadt   268:                                                        /* didn't find it swapped */
                    269:                                                        if (!pch_swap())
                    270:                                                                /* put it back to normal */
                    271:                                                                fatal("lost hunk on alloc error!\n");
                    272:                                                        reverse = !reverse;
                    273:                                                } else if (noreverse) {
                    274:                                                        if (!pch_swap())
                    275:                                                                /* put it back to normal */
                    276:                                                                fatal("lost hunk on alloc error!\n");
                    277:                                                        reverse = !reverse;
                    278:                                                        say("Ignoring previously applied (or reversed) patch.\n");
1.35      otto      279:                                                        skip_rest_of_patch = true;
1.20      deraadt   280:                                                } else if (batch) {
                    281:                                                        if (verbose)
                    282:                                                                say("%seversed (or previously applied) patch detected!  %s -R.",
                    283:                                                                    reverse ? "R" : "Unr",
                    284:                                                                    reverse ? "Assuming" : "Ignoring");
                    285:                                                } else {
                    286:                                                        ask("%seversed (or previously applied) patch detected!  %s -R? [y] ",
                    287:                                                            reverse ? "R" : "Unr",
                    288:                                                            reverse ? "Assume" : "Ignore");
                    289:                                                        if (*buf == 'n') {
                    290:                                                                ask("Apply anyway? [n] ");
                    291:                                                                if (*buf != 'y')
1.35      otto      292:                                                                        skip_rest_of_patch = true;
1.29      otto      293:                                                                where = 0;
1.20      deraadt   294:                                                                reverse = !reverse;
                    295:                                                                if (!pch_swap())
                    296:                                                                        /* put it back to normal */
                    297:                                                                        fatal("lost hunk on alloc error!\n");
                    298:                                                        }
                    299:                                                }
                    300:                                        }
1.29      otto      301:                                } while (!skip_rest_of_patch && where == 0 &&
1.30      deraadt   302:                                    ++fuzz <= mymaxfuzz);
1.20      deraadt   303:
                    304:                                if (skip_rest_of_patch) {       /* just got decided */
                    305:                                        fclose(ofp);
1.23      otto      306:                                        ofp = NULL;
1.20      deraadt   307:                                }
                    308:                        }
                    309:                        newwhere = pch_newfirst() + last_offset;
                    310:                        if (skip_rest_of_patch) {
                    311:                                abort_hunk();
                    312:                                failed++;
                    313:                                if (verbose)
                    314:                                        say("Hunk #%d ignored at %ld.\n",
                    315:                                            hunk, newwhere);
1.29      otto      316:                        } else if (where == 0) {
1.20      deraadt   317:                                abort_hunk();
                    318:                                failed++;
                    319:                                if (verbose)
                    320:                                        say("Hunk #%d failed at %ld.\n",
                    321:                                            hunk, newwhere);
                    322:                        } else {
                    323:                                apply_hunk(where);
                    324:                                if (verbose) {
                    325:                                        say("Hunk #%d succeeded at %ld",
                    326:                                            hunk, newwhere);
1.29      otto      327:                                        if (fuzz != 0)
1.20      deraadt   328:                                                say(" with fuzz %ld", fuzz);
                    329:                                        if (last_offset)
                    330:                                                say(" (offset %ld line%s)",
                    331:                                                    last_offset,
                    332:                                                    last_offset == 1L ? "" : "s");
                    333:                                        say(".\n");
                    334:                                }
                    335:                        }
                    336:                }
                    337:
                    338:                if (out_of_mem && using_plan_a) {
                    339:                        Argc = Argc_last;
                    340:                        Argv = Argv_last;
                    341:                        say("\n\nRan out of memory using Plan A--trying again...\n\n");
                    342:                        if (ofp)
                    343:                                fclose(ofp);
1.23      otto      344:                        ofp = NULL;
1.20      deraadt   345:                        if (rejfp)
                    346:                                fclose(rejfp);
1.23      otto      347:                        rejfp = NULL;
1.20      deraadt   348:                        continue;
1.11      espie     349:                }
1.35      otto      350:                if (hunk == 0)
                    351:                        fatal("Internal error: hunk should not be 0\n");
1.1       deraadt   352:
1.20      deraadt   353:                /* finish spewing out the new file */
                    354:                if (!skip_rest_of_patch)
                    355:                        spew_output();
                    356:
                    357:                /* and put the output where desired */
                    358:                ignore_signals();
                    359:                if (!skip_rest_of_patch) {
                    360:                        struct stat     statbuf;
                    361:                        char    *realout = outname;
                    362:
                    363:                        if (!check_only) {
                    364:                                if (move_file(TMPOUTNAME, outname) < 0) {
1.35      otto      365:                                        toutkeep = true;
1.20      deraadt   366:                                        realout = TMPOUTNAME;
                    367:                                        chmod(TMPOUTNAME, filemode);
                    368:                                } else
                    369:                                        chmod(outname, filemode);
                    370:
                    371:                                if (remove_empty_files &&
                    372:                                    stat(realout, &statbuf) == 0 &&
                    373:                                    statbuf.st_size == 0) {
                    374:                                        if (verbose)
                    375:                                                say("Removing %s (empty after patching).\n",
                    376:                                                    realout);
                    377:                                        unlink(realout);
                    378:                                }
                    379:                        }
                    380:                }
                    381:                fclose(rejfp);
1.23      otto      382:                rejfp = NULL;
1.20      deraadt   383:                if (failed) {
1.28      millert   384:                        error = 1;
1.29      otto      385:                        if (*rejname == '\0') {
1.20      deraadt   386:                                if (strlcpy(rejname, outname,
                    387:                                    sizeof(rejname)) >= sizeof(rejname))
                    388:                                        fatal("filename %s is too long\n", outname);
                    389:                                if (strlcat(rejname, REJEXT,
                    390:                                    sizeof(rejname)) >= sizeof(rejname))
                    391:                                        fatal("filename %s is too long\n", outname);
                    392:                        }
1.50      millert   393:                        if (!check_only)
                    394:                                say("%d out of %d hunks %s--saving rejects to %s\n",
                    395:                                    failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname);
                    396:                        else
                    397:                                say("%d out of %d hunks %s\n",
                    398:                                    failed, hunk, skip_rest_of_patch ? "ignored" : "failed");
1.20      deraadt   399:                        if (!check_only && move_file(TMPREJNAME, rejname) < 0)
1.35      otto      400:                                trejkeep = true;
1.20      deraadt   401:                }
                    402:                set_signals(1);
                    403:        }
1.53      deraadt   404:
1.47      stsp      405:        if (!patch_seen)
                    406:                error = 2;
                    407:
1.28      millert   408:        my_exit(error);
1.20      deraadt   409:        /* NOTREACHED */
1.1       deraadt   410: }
                    411:
                    412: /* Prepare to find the next patch to do in the patch file. */
                    413:
1.23      otto      414: static void
1.20      deraadt   415: reinitialize_almost_everything(void)
1.1       deraadt   416: {
1.20      deraadt   417:        re_patch();
                    418:        re_input();
1.1       deraadt   419:
1.20      deraadt   420:        input_lines = 0;
                    421:        last_frozen_line = 0;
1.1       deraadt   422:
1.20      deraadt   423:        filec = 0;
1.35      otto      424:        if (!out_of_mem) {
1.20      deraadt   425:                free(filearg[0]);
1.23      otto      426:                filearg[0] = NULL;
1.20      deraadt   427:        }
1.35      otto      428:
                    429:        free(outname);
                    430:        outname = NULL;
                    431:
1.20      deraadt   432:        last_offset = 0;
1.35      otto      433:        diff_type = 0;
1.1       deraadt   434:
1.35      otto      435:        free(revision);
                    436:        revision = NULL;
1.1       deraadt   437:
1.20      deraadt   438:        reverse = reverse_flag_specified;
1.35      otto      439:        skip_rest_of_patch = false;
1.1       deraadt   440:
1.20      deraadt   441:        get_some_switches();
1.1       deraadt   442: }
                    443:
1.24      millert   444: /* Process switches and filenames. */
1.1       deraadt   445:
1.23      otto      446: static void
1.20      deraadt   447: get_some_switches(void)
1.1       deraadt   448: {
1.34      millert   449:        const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
1.24      millert   450:        static struct option longopts[] = {
1.27      millert   451:                {"backup",              no_argument,            0,      'b'},
1.24      millert   452:                {"batch",               no_argument,            0,      't'},
                    453:                {"check",               no_argument,            0,      'C'},
                    454:                {"context",             no_argument,            0,      'c'},
                    455:                {"debug",               required_argument,      0,      'x'},
                    456:                {"directory",           required_argument,      0,      'd'},
1.66      zhuk      457:                {"dry-run",             no_argument,            0,      'C'},
1.24      millert   458:                {"ed",                  no_argument,            0,      'e'},
                    459:                {"force",               no_argument,            0,      'f'},
                    460:                {"forward",             no_argument,            0,      'N'},
                    461:                {"fuzz",                required_argument,      0,      'F'},
                    462:                {"ifdef",               required_argument,      0,      'D'},
1.32      millert   463:                {"input",               required_argument,      0,      'i'},
1.24      millert   464:                {"ignore-whitespace",   no_argument,            0,      'l'},
                    465:                {"normal",              no_argument,            0,      'n'},
                    466:                {"output",              required_argument,      0,      'o'},
                    467:                {"prefix",              required_argument,      0,      'B'},
                    468:                {"quiet",               no_argument,            0,      's'},
                    469:                {"reject-file",         required_argument,      0,      'r'},
                    470:                {"remove-empty-files",  no_argument,            0,      'E'},
                    471:                {"reverse",             no_argument,            0,      'R'},
                    472:                {"silent",              no_argument,            0,      's'},
1.34      millert   473:                {"strip",               required_argument,      0,      'p'},
1.27      millert   474:                {"suffix",              required_argument,      0,      'z'},
1.24      millert   475:                {"unified",             no_argument,            0,      'u'},
                    476:                {"version",             no_argument,            0,      'v'},
                    477:                {"version-control",     required_argument,      0,      'V'},
1.38      millert   478:                {"posix",               no_argument,            &posix, 1},
1.24      millert   479:                {NULL,                  0,                      0,      0}
                    480:        };
                    481:        int ch;
1.1       deraadt   482:
1.20      deraadt   483:        rejname[0] = '\0';
                    484:        Argc_last = Argc;
                    485:        Argv_last = Argv;
                    486:        if (!Argc)
                    487:                return;
1.24      millert   488:        optreset = optind = 1;
                    489:        while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
                    490:                switch (ch) {
                    491:                case 'b':
1.27      millert   492:                        if (backup_type == none)
                    493:                                backup_type = numbered_existing;
                    494:                        if (optarg == NULL)
                    495:                                break;
                    496:                        if (verbose)
                    497:                                say("Warning, the ``-b suffix'' option has been"
                    498:                                    " obsoleted by the -z option.\n");
                    499:                        /* FALLTHROUGH */
                    500:                case 'z':
                    501:                        /* must directly follow 'b' case for backwards compat */
1.54      tobias    502:                        simple_backup_suffix = xstrdup(optarg);
1.24      millert   503:                        break;
                    504:                case 'B':
1.54      tobias    505:                        origprae = xstrdup(optarg);
1.24      millert   506:                        break;
                    507:                case 'c':
                    508:                        diff_type = CONTEXT_DIFF;
                    509:                        break;
                    510:                case 'C':
1.35      otto      511:                        check_only = true;
1.24      millert   512:                        break;
                    513:                case 'd':
1.68    ! deraadt   514:                        if (chdir(optarg) == -1)
1.24      millert   515:                                pfatal("can't cd to %s", optarg);
                    516:                        break;
                    517:                case 'D':
1.35      otto      518:                        do_defines = true;
1.51      deraadt   519:                        if (!isalpha((unsigned char)*optarg) && *optarg != '_')
1.24      millert   520:                                fatal("argument to -D is not an identifier\n");
                    521:                        snprintf(if_defined, sizeof if_defined,
                    522:                            "#ifdef %s\n", optarg);
                    523:                        snprintf(not_defined, sizeof not_defined,
                    524:                            "#ifndef %s\n", optarg);
                    525:                        snprintf(end_defined, sizeof end_defined,
                    526:                            "#endif /* %s */\n", optarg);
                    527:                        break;
                    528:                case 'e':
                    529:                        diff_type = ED_DIFF;
                    530:                        break;
                    531:                case 'E':
1.35      otto      532:                        remove_empty_files = true;
1.24      millert   533:                        break;
                    534:                case 'f':
1.35      otto      535:                        force = true;
1.24      millert   536:                        break;
                    537:                case 'F':
                    538:                        maxfuzz = atoi(optarg);
                    539:                        break;
1.32      millert   540:                case 'i':
                    541:                        if (++filec == MAXFILEC)
                    542:                                fatal("too many file arguments\n");
1.54      tobias    543:                        filearg[filec] = xstrdup(optarg);
1.32      millert   544:                        break;
1.24      millert   545:                case 'l':
1.35      otto      546:                        canonicalize = true;
1.24      millert   547:                        break;
                    548:                case 'n':
                    549:                        diff_type = NORMAL_DIFF;
                    550:                        break;
                    551:                case 'N':
1.35      otto      552:                        noreverse = true;
1.24      millert   553:                        break;
                    554:                case 'o':
1.54      tobias    555:                        outname = xstrdup(optarg);
1.24      millert   556:                        break;
                    557:                case 'p':
1.34      millert   558:                        strippath = atoi(optarg);
1.24      millert   559:                        break;
                    560:                case 'r':
                    561:                        if (strlcpy(rejname, optarg,
                    562:                            sizeof(rejname)) >= sizeof(rejname))
                    563:                                fatal("argument for -r is too long\n");
                    564:                        break;
                    565:                case 'R':
1.35      otto      566:                        reverse = true;
                    567:                        reverse_flag_specified = true;
1.24      millert   568:                        break;
                    569:                case 's':
1.35      otto      570:                        verbose = false;
1.24      millert   571:                        break;
                    572:                case 't':
1.35      otto      573:                        batch = true;
1.24      millert   574:                        break;
                    575:                case 'u':
                    576:                        diff_type = UNI_DIFF;
                    577:                        break;
                    578:                case 'v':
                    579:                        version();
                    580:                        break;
                    581:                case 'V':
                    582:                        backup_type = get_version(optarg);
                    583:                        break;
1.1       deraadt   584: #ifdef DEBUGGING
1.24      millert   585:                case 'x':
                    586:                        debug = atoi(optarg);
                    587:                        break;
1.1       deraadt   588: #endif
1.24      millert   589:                default:
1.38      millert   590:                        if (ch != '\0')
                    591:                                usage();
1.24      millert   592:                        break;
1.20      deraadt   593:                }
1.1       deraadt   594:        }
1.24      millert   595:        Argc -= optind;
                    596:        Argv += optind;
                    597:
1.32      millert   598:        if (Argc > 0) {
1.54      tobias    599:                filearg[0] = xstrdup(*Argv++);
1.24      millert   600:                Argc--;
1.32      millert   601:                while (Argc > 0) {
                    602:                        if (++filec == MAXFILEC)
                    603:                                fatal("too many file arguments\n");
1.54      tobias    604:                        filearg[filec] = xstrdup(*Argv++);
1.32      millert   605:                        Argc--;
                    606:                }
1.24      millert   607:        }
1.38      millert   608:
                    609:        if (getenv("POSIXLY_CORRECT") != NULL)
                    610:                posix = 1;
1.24      millert   611: }
                    612:
                    613: static __dead void
                    614: usage(void)
                    615: {
                    616:        fprintf(stderr,
1.45      sobrado   617: "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
1.34      millert   618: "             [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
1.45      sobrado   619: "             [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n"
                    620: "             [--posix] [origfile [patchfile]]\n"
                    621: "       patch <patchfile\n");
1.63      gsoares   622:        my_exit(2);
1.1       deraadt   623: }
                    624:
1.20      deraadt   625: /*
                    626:  * Attempt to find the right place to apply this hunk of patch.
                    627:  */
1.23      otto      628: static LINENUM
1.20      deraadt   629: locate_hunk(LINENUM fuzz)
1.1       deraadt   630: {
1.20      deraadt   631:        LINENUM first_guess = pch_first() + last_offset;
                    632:        LINENUM offset;
                    633:        LINENUM pat_lines = pch_ptrn_lines();
                    634:        LINENUM max_pos_offset = input_lines - first_guess - pat_lines + 1;
                    635:        LINENUM max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
                    636:
1.36      otto      637:        if (pat_lines == 0) {           /* null range matches always */
1.39      otto      638:                if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
1.36      otto      639:                    || diff_type == NEW_CONTEXT_DIFF
                    640:                    || diff_type == UNI_DIFF)) {
                    641:                        say("Empty context always matches.\n");
                    642:                }
1.46      otto      643:                return (first_guess);
1.36      otto      644:        }
1.20      deraadt   645:        if (max_neg_offset >= first_guess)      /* do not try lines < 0 */
                    646:                max_neg_offset = first_guess - 1;
1.29      otto      647:        if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
1.20      deraadt   648:                return first_guess;
                    649:        for (offset = 1; ; offset++) {
                    650:                bool    check_after = (offset <= max_pos_offset);
                    651:                bool    check_before = (offset <= max_neg_offset);
1.1       deraadt   652:
1.20      deraadt   653:                if (check_after && patch_match(first_guess, offset, fuzz)) {
1.1       deraadt   654: #ifdef DEBUGGING
1.20      deraadt   655:                        if (debug & 1)
                    656:                                say("Offset changing from %ld to %ld\n",
                    657:                                    last_offset, offset);
1.1       deraadt   658: #endif
1.20      deraadt   659:                        last_offset = offset;
                    660:                        return first_guess + offset;
                    661:                } else if (check_before && patch_match(first_guess, -offset, fuzz)) {
1.1       deraadt   662: #ifdef DEBUGGING
1.20      deraadt   663:                        if (debug & 1)
                    664:                                say("Offset changing from %ld to %ld\n",
                    665:                                    last_offset, -offset);
1.1       deraadt   666: #endif
1.20      deraadt   667:                        last_offset = -offset;
                    668:                        return first_guess - offset;
                    669:                } else if (!check_before && !check_after)
1.29      otto      670:                        return 0;
1.1       deraadt   671:        }
                    672: }
                    673:
                    674: /* We did not find the pattern, dump out the hunk so they can handle it. */
                    675:
1.23      otto      676: static void
1.43      otto      677: abort_context_hunk(void)
1.1       deraadt   678: {
1.20      deraadt   679:        LINENUM i;
1.29      otto      680:        const LINENUM   pat_end = pch_end();
1.20      deraadt   681:        /*
                    682:         * add in last_offset to guess the same as the previous successful
                    683:         * hunk
                    684:         */
1.29      otto      685:        const LINENUM   oldfirst = pch_first() + last_offset;
                    686:        const LINENUM   newfirst = pch_newfirst() + last_offset;
                    687:        const LINENUM   oldlast = oldfirst + pch_ptrn_lines() - 1;
                    688:        const LINENUM   newlast = newfirst + pch_repl_lines() - 1;
                    689:        const char      *stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
                    690:        const char      *minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
1.20      deraadt   691:
                    692:        fprintf(rejfp, "***************\n");
                    693:        for (i = 0; i <= pat_end; i++) {
                    694:                switch (pch_char(i)) {
                    695:                case '*':
                    696:                        if (oldlast < oldfirst)
                    697:                                fprintf(rejfp, "*** 0%s\n", stars);
                    698:                        else if (oldlast == oldfirst)
                    699:                                fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
                    700:                        else
                    701:                                fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
                    702:                                    oldlast, stars);
                    703:                        break;
                    704:                case '=':
                    705:                        if (newlast < newfirst)
                    706:                                fprintf(rejfp, "--- 0%s\n", minuses);
                    707:                        else if (newlast == newfirst)
                    708:                                fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
                    709:                        else
                    710:                                fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
                    711:                                    newlast, minuses);
                    712:                        break;
                    713:                case '\n':
                    714:                        fprintf(rejfp, "%s", pfetch(i));
                    715:                        break;
                    716:                case ' ':
                    717:                case '-':
                    718:                case '+':
                    719:                case '!':
                    720:                        fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
                    721:                        break;
                    722:                default:
1.43      otto      723:                        fatal("fatal internal error in abort_context_hunk\n");
                    724:                }
                    725:        }
                    726: }
                    727:
                    728: static void
                    729: rej_line(int ch, LINENUM i)
                    730: {
                    731:        size_t len;
                    732:        const char *line = pfetch(i);
                    733:
                    734:        len = strlen(line);
                    735:
                    736:        fprintf(rejfp, "%c%s", ch, line);
                    737:        if (len == 0 || line[len-1] != '\n')
                    738:                fprintf(rejfp, "\n\\ No newline at end of file\n");
                    739: }
                    740:
                    741: static void
                    742: abort_hunk(void)
                    743: {
                    744:        LINENUM         i, j, split;
                    745:        int             ch1, ch2;
                    746:        const LINENUM   pat_end = pch_end();
                    747:        const LINENUM   oldfirst = pch_first() + last_offset;
                    748:        const LINENUM   newfirst = pch_newfirst() + last_offset;
                    749:
                    750:        if (diff_type != UNI_DIFF) {
                    751:                abort_context_hunk();
                    752:                return;
                    753:        }
                    754:        split = -1;
                    755:        for (i = 0; i <= pat_end; i++) {
                    756:                if (pch_char(i) == '=') {
                    757:                        split = i;
                    758:                        break;
                    759:                }
                    760:        }
                    761:        if (split == -1) {
                    762:                fprintf(rejfp, "malformed hunk: no split found\n");
                    763:                return;
                    764:        }
                    765:        i = 0;
                    766:        j = split + 1;
                    767:        fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
                    768:            pch_ptrn_lines() ? oldfirst : 0,
                    769:            pch_ptrn_lines(), newfirst, pch_repl_lines());
                    770:        while (i < split || j <= pat_end) {
                    771:                ch1 = i < split ? pch_char(i) : -1;
                    772:                ch2 = j <= pat_end ? pch_char(j) : -1;
                    773:                if (ch1 == '-') {
                    774:                        rej_line('-', i);
                    775:                        i++;
                    776:                } else if (ch1 == ' ' && ch2 == ' ') {
                    777:                        rej_line(' ', i);
                    778:                        i++;
                    779:                        j++;
                    780:                } else if (ch1 == '!' && ch2 == '!') {
                    781:                        while (i < split && ch1 == '!') {
                    782:                                rej_line('-', i);
                    783:                                i++;
                    784:                                ch1 = i < split ? pch_char(i) : -1;
                    785:                        }
                    786:                        while (j <= pat_end && ch2 == '!') {
                    787:                                rej_line('+', j);
                    788:                                j++;
                    789:                                ch2 = j <= pat_end ? pch_char(j) : -1;
                    790:                        }
                    791:                } else if (ch1 == '*') {
                    792:                        i++;
                    793:                } else if (ch2 == '+' || ch2 == ' ') {
                    794:                        rej_line(ch2, j);
                    795:                        j++;
                    796:                } else {
                    797:                        fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
                    798:                            i, split, j);
                    799:                        rej_line(ch1, i);
                    800:                        rej_line(ch2, j);
                    801:                        return;
1.20      deraadt   802:                }
1.1       deraadt   803:        }
                    804: }
                    805:
                    806: /* We found where to apply it (we hope), so do it. */
                    807:
1.23      otto      808: static void
1.20      deraadt   809: apply_hunk(LINENUM where)
1.1       deraadt   810: {
1.29      otto      811:        LINENUM         old = 1;
                    812:        const LINENUM   lastline = pch_ptrn_lines();
                    813:        LINENUM         new = lastline + 1;
1.1       deraadt   814: #define OUTSIDE 0
                    815: #define IN_IFNDEF 1
                    816: #define IN_IFDEF 2
                    817: #define IN_ELSE 3
1.29      otto      818:        int             def_state = OUTSIDE;
                    819:        const LINENUM   pat_end = pch_end();
1.20      deraadt   820:
                    821:        where--;
                    822:        while (pch_char(new) == '=' || pch_char(new) == '\n')
                    823:                new++;
                    824:
                    825:        while (old <= lastline) {
                    826:                if (pch_char(old) == '-') {
1.37      otto      827:                        copy_till(where + old - 1, false);
1.29      otto      828:                        if (do_defines) {
1.20      deraadt   829:                                if (def_state == OUTSIDE) {
                    830:                                        fputs(not_defined, ofp);
                    831:                                        def_state = IN_IFNDEF;
                    832:                                } else if (def_state == IN_IFDEF) {
                    833:                                        fputs(else_defined, ofp);
                    834:                                        def_state = IN_ELSE;
                    835:                                }
                    836:                                fputs(pfetch(old), ofp);
                    837:                        }
                    838:                        last_frozen_line++;
                    839:                        old++;
                    840:                } else if (new > pat_end) {
                    841:                        break;
                    842:                } else if (pch_char(new) == '+') {
1.37      otto      843:                        copy_till(where + old - 1, false);
1.29      otto      844:                        if (do_defines) {
1.20      deraadt   845:                                if (def_state == IN_IFNDEF) {
                    846:                                        fputs(else_defined, ofp);
                    847:                                        def_state = IN_ELSE;
                    848:                                } else if (def_state == OUTSIDE) {
                    849:                                        fputs(if_defined, ofp);
                    850:                                        def_state = IN_IFDEF;
                    851:                                }
                    852:                        }
                    853:                        fputs(pfetch(new), ofp);
                    854:                        new++;
                    855:                } else if (pch_char(new) != pch_char(old)) {
                    856:                        say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
                    857:                            pch_hunk_beg() + old,
                    858:                            pch_hunk_beg() + new);
1.1       deraadt   859: #ifdef DEBUGGING
1.20      deraadt   860:                        say("oldchar = '%c', newchar = '%c'\n",
                    861:                            pch_char(old), pch_char(new));
1.1       deraadt   862: #endif
1.28      millert   863:                        my_exit(2);
1.20      deraadt   864:                } else if (pch_char(new) == '!') {
1.37      otto      865:                        copy_till(where + old - 1, false);
1.29      otto      866:                        if (do_defines) {
1.20      deraadt   867:                                fputs(not_defined, ofp);
                    868:                                def_state = IN_IFNDEF;
                    869:                        }
                    870:                        while (pch_char(old) == '!') {
1.29      otto      871:                                if (do_defines) {
1.20      deraadt   872:                                        fputs(pfetch(old), ofp);
                    873:                                }
                    874:                                last_frozen_line++;
                    875:                                old++;
                    876:                        }
1.29      otto      877:                        if (do_defines) {
1.20      deraadt   878:                                fputs(else_defined, ofp);
                    879:                                def_state = IN_ELSE;
                    880:                        }
                    881:                        while (pch_char(new) == '!') {
                    882:                                fputs(pfetch(new), ofp);
                    883:                                new++;
                    884:                        }
                    885:                } else {
1.35      otto      886:                        if (pch_char(new) != ' ')
                    887:                                fatal("Internal error: expected ' '\n");
1.20      deraadt   888:                        old++;
                    889:                        new++;
1.29      otto      890:                        if (do_defines && def_state != OUTSIDE) {
1.20      deraadt   891:                                fputs(end_defined, ofp);
                    892:                                def_state = OUTSIDE;
                    893:                        }
                    894:                }
1.1       deraadt   895:        }
1.20      deraadt   896:        if (new <= pat_end && pch_char(new) == '+') {
1.37      otto      897:                copy_till(where + old - 1, false);
1.29      otto      898:                if (do_defines) {
1.20      deraadt   899:                        if (def_state == OUTSIDE) {
                    900:                                fputs(if_defined, ofp);
                    901:                                def_state = IN_IFDEF;
                    902:                        } else if (def_state == IN_IFNDEF) {
                    903:                                fputs(else_defined, ofp);
                    904:                                def_state = IN_ELSE;
                    905:                        }
                    906:                }
                    907:                while (new <= pat_end && pch_char(new) == '+') {
                    908:                        fputs(pfetch(new), ofp);
                    909:                        new++;
1.1       deraadt   910:                }
                    911:        }
1.29      otto      912:        if (do_defines && def_state != OUTSIDE) {
1.1       deraadt   913:                fputs(end_defined, ofp);
                    914:        }
                    915: }
                    916:
1.20      deraadt   917: /*
                    918:  * Open the new file.
                    919:  */
1.23      otto      920: static void
1.29      otto      921: init_output(const char *name)
1.1       deraadt   922: {
1.20      deraadt   923:        ofp = fopen(name, "w");
1.23      otto      924:        if (ofp == NULL)
1.20      deraadt   925:                pfatal("can't create %s", name);
1.1       deraadt   926: }
                    927:
1.20      deraadt   928: /*
                    929:  * Open a file to put hunks we can't locate.
                    930:  */
1.23      otto      931: static void
1.29      otto      932: init_reject(const char *name)
1.1       deraadt   933: {
1.20      deraadt   934:        rejfp = fopen(name, "w");
1.23      otto      935:        if (rejfp == NULL)
1.20      deraadt   936:                pfatal("can't create %s", name);
1.1       deraadt   937: }
                    938:
1.20      deraadt   939: /*
                    940:  * Copy input file to output, up to wherever hunk is to be applied.
1.37      otto      941:  * If endoffile is true, treat the last line specially since it may
                    942:  * lack a newline.
1.20      deraadt   943:  */
1.23      otto      944: static void
1.37      otto      945: copy_till(LINENUM lastline, bool endoffile)
1.1       deraadt   946: {
1.29      otto      947:        if (last_frozen_line > lastline)
1.20      deraadt   948:                fatal("misordered hunks! output would be garbled\n");
1.37      otto      949:        while (last_frozen_line < lastline) {
                    950:                if (++last_frozen_line == lastline && endoffile)
                    951:                        dump_line(last_frozen_line, !last_line_missing_eol);
                    952:                else
                    953:                        dump_line(last_frozen_line, true);
                    954:        }
1.1       deraadt   955: }
                    956:
1.20      deraadt   957: /*
                    958:  * Finish copying the input file to the output file.
                    959:  */
1.23      otto      960: static void
1.20      deraadt   961: spew_output(void)
1.1       deraadt   962: {
                    963: #ifdef DEBUGGING
1.20      deraadt   964:        if (debug & 256)
                    965:                say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
1.1       deraadt   966: #endif
1.20      deraadt   967:        if (input_lines)
1.37      otto      968:                copy_till(input_lines, true);   /* dump remainder of file */
1.20      deraadt   969:        fclose(ofp);
1.23      otto      970:        ofp = NULL;
1.1       deraadt   971: }
                    972:
1.20      deraadt   973: /*
                    974:  * Copy one line from input to output.
                    975:  */
1.23      otto      976: static void
1.37      otto      977: dump_line(LINENUM line, bool write_newline)
1.1       deraadt   978: {
1.29      otto      979:        char    *s;
1.1       deraadt   980:
1.26      otto      981:        s = ifetch(line, 0);
                    982:        if (s == NULL)
                    983:                return;
1.30      deraadt   984:        /* Note: string is not NUL terminated. */
1.37      otto      985:        for (; *s != '\n'; s++)
                    986:                putc(*s, ofp);
                    987:        if (write_newline)
                    988:                putc('\n', ofp);
1.1       deraadt   989: }
                    990:
1.20      deraadt   991: /*
                    992:  * Does the patch pattern match at line base+offset?
                    993:  */
1.23      otto      994: static bool
1.20      deraadt   995: patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
                    996: {
1.29      otto      997:        LINENUM         pline = 1 + fuzz;
                    998:        LINENUM         iline;
                    999:        LINENUM         pat_lines = pch_ptrn_lines() - fuzz;
                   1000:        const char      *ilineptr;
1.30      deraadt  1001:        const char      *plineptr;
                   1002:        short           plinelen;
1.20      deraadt  1003:
                   1004:        for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
1.26      otto     1005:                ilineptr = ifetch(iline, offset >= 0);
                   1006:                if (ilineptr == NULL)
1.35      otto     1007:                        return false;
1.26      otto     1008:                plineptr = pfetch(pline);
                   1009:                plinelen = pch_line_len(pline);
1.20      deraadt  1010:                if (canonicalize) {
1.26      otto     1011:                        if (!similar(ilineptr, plineptr, plinelen))
1.35      otto     1012:                                return false;
1.26      otto     1013:                } else if (strnNE(ilineptr, plineptr, plinelen))
1.35      otto     1014:                        return false;
1.41      otto     1015:                if (iline == input_lines) {
                   1016:                        /*
                   1017:                         * We are looking at the last line of the file.
                   1018:                         * If the file has no eol, the patch line should
                   1019:                         * not have one either and vice-versa. Note that
                   1020:                         * plinelen > 0.
                   1021:                         */
                   1022:                        if (last_line_missing_eol) {
                   1023:                                if (plineptr[plinelen - 1] == '\n')
                   1024:                                        return false;
                   1025:                        } else {
                   1026:                                if (plineptr[plinelen - 1] != '\n')
                   1027:                                        return false;
                   1028:                        }
                   1029:                }
1.1       deraadt  1030:        }
1.35      otto     1031:        return true;
1.1       deraadt  1032: }
                   1033:
1.20      deraadt  1034: /*
                   1035:  * Do two lines match with canonicalized white space?
                   1036:  */
1.23      otto     1037: static bool
1.29      otto     1038: similar(const char *a, const char *b, int len)
1.20      deraadt  1039: {
                   1040:        while (len) {
1.51      deraadt  1041:                if (isspace((unsigned char)*b)) { /* whitespace (or \n) to match? */
                   1042:                        if (!isspace((unsigned char)*a))
                   1043:                                return false;   /* no corresponding whitespace */
                   1044:                        while (len && isspace((unsigned char)*b) && *b != '\n')
1.20      deraadt  1045:                                b++, len--;     /* skip pattern whitespace */
1.51      deraadt  1046:                        while (isspace((unsigned char)*a) && *a != '\n')
1.20      deraadt  1047:                                a++;    /* skip target whitespace */
                   1048:                        if (*a == '\n' || *b == '\n')
                   1049:                                return (*a == *b);      /* should end in sync */
                   1050:                } else if (*a++ != *b++)        /* match non-whitespace chars */
1.35      otto     1051:                        return false;
1.20      deraadt  1052:                else
                   1053:                        len--;  /* probably not necessary */
1.1       deraadt  1054:        }
1.35      otto     1055:        return true;            /* actually, this is not reached */
1.20      deraadt  1056:        /* since there is always a \n */
1.1       deraadt  1057: }