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

Annotation of src/usr.bin/patch/pch.c, Revision 1.47

1.47    ! deraadt     1: /*     $OpenBSD: pch.c,v 1.46 2014/11/26 10:11:21 tobias Exp $ */
1.30      otto        2:
                      3: /*
                      4:  * patch - a program to apply diffs to original files
                      5:  *
                      6:  * Copyright 1986, Larry Wall
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      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
                     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.
                     24:  *
                     25:  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
                     26:  * behaviour
                     27:  */
1.1       deraadt    28:
1.21      otto       29: #include <sys/types.h>
                     30: #include <sys/stat.h>
                     31:
                     32: #include <ctype.h>
1.32      millert    33: #include <libgen.h>
                     34: #include <limits.h>
1.25      otto       35: #include <stdio.h>
1.21      otto       36: #include <stdlib.h>
                     37: #include <string.h>
                     38: #include <unistd.h>
                     39:
1.1       deraadt    40: #include "common.h"
                     41: #include "util.h"
                     42: #include "pch.h"
1.27      millert    43: #include "pathnames.h"
1.1       deraadt    44:
                     45: /* Patch (diff listing) abstract type. */
                     46:
1.42      tobias     47: static off_t   p_filesize;     /* size of the patch file */
1.17      deraadt    48: static LINENUM p_first;        /* 1st line number */
                     49: static LINENUM p_newfirst;     /* 1st line number of replacement */
                     50: static LINENUM p_ptrn_lines;   /* # lines in pattern */
                     51: static LINENUM p_repl_lines;   /* # lines in replacement text */
                     52: static LINENUM p_end = -1;     /* last line in hunk */
                     53: static LINENUM p_max;          /* max allowed value of p_end */
                     54: static LINENUM p_context = 3;  /* # of context lines */
                     55: static LINENUM p_input_line = 0;       /* current line # from patch file */
1.25      otto       56: static char    **p_line = NULL;/* the text of the hunk */
1.21      otto       57: static short   *p_len = NULL;  /* length of each line */
1.25      otto       58: static char    *p_char = NULL; /* +, -, and ! */
1.17      deraadt    59: static int     hunkmax = INITHUNKMAX;  /* size of above arrays to begin with */
                     60: static int     p_indent;       /* indent to patch */
1.42      tobias     61: static off_t   p_base;         /* where to intuit this time */
1.17      deraadt    62: static LINENUM p_bline;        /* line # of p_base */
1.42      tobias     63: static off_t   p_start;        /* where intuit found a patch */
1.17      deraadt    64: static LINENUM p_sline;        /* and the line number for it */
                     65: static LINENUM p_hunk_beg;     /* line number of current hunk */
                     66: static LINENUM p_efake = -1;   /* end of faked up lines--don't free */
                     67: static LINENUM p_bfake = -1;   /* beg of faked up lines */
1.25      otto       68: static FILE    *pfp = NULL;    /* patch file pointer */
                     69: static char    *bestguess = NULL;      /* guess at correct filename */
1.1       deraadt    70:
1.21      otto       71: static void    grow_hunkmax(void);
                     72: static int     intuit_diff_type(void);
1.42      tobias     73: static void    next_intuit_at(off_t, LINENUM);
                     74: static void    skip_to(off_t, LINENUM);
1.23      deraadt    75: static char    *pgets(char *, int, FILE *);
1.32      millert    76: static char    *best_name(const struct file_name *, bool);
                     77: static char    *posix_name(const struct file_name *, bool);
                     78: static size_t  num_components(const char *);
1.45      tobias     79: static LINENUM strtolinenum(char *, char **);
1.25      otto       80:
1.17      deraadt    81: /*
                     82:  * Prepare to look for the next patch in the patch file.
                     83:  */
1.1       deraadt    84: void
1.17      deraadt    85: re_patch(void)
1.1       deraadt    86: {
1.25      otto       87:        p_first = 0;
                     88:        p_newfirst = 0;
                     89:        p_ptrn_lines = 0;
                     90:        p_repl_lines = 0;
1.17      deraadt    91:        p_end = (LINENUM) - 1;
1.25      otto       92:        p_max = 0;
1.17      deraadt    93:        p_indent = 0;
1.1       deraadt    94: }
                     95:
1.17      deraadt    96: /*
                     97:  * Open the patch file at the beginning of time.
                     98:  */
1.1       deraadt    99: void
1.25      otto      100: open_patch_file(const char *filename)
1.1       deraadt   101: {
1.25      otto      102:        struct stat filestat;
                    103:
                    104:        if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) {
1.17      deraadt   105:                pfp = fopen(TMPPATNAME, "w");
1.21      otto      106:                if (pfp == NULL)
1.17      deraadt   107:                        pfatal("can't create %s", TMPPATNAME);
1.21      otto      108:                while (fgets(buf, sizeof buf, stdin) != NULL)
1.17      deraadt   109:                        fputs(buf, pfp);
                    110:                fclose(pfp);
                    111:                filename = TMPPATNAME;
                    112:        }
                    113:        pfp = fopen(filename, "r");
1.21      otto      114:        if (pfp == NULL)
1.17      deraadt   115:                pfatal("patch file %s not found", filename);
1.42      tobias    116:        if (fstat(fileno(pfp), &filestat))
                    117:                pfatal("can't stat %s", filename);
1.17      deraadt   118:        p_filesize = filestat.st_size;
1.42      tobias    119:        next_intuit_at(0, 1L);  /* start at the beginning */
1.17      deraadt   120:        set_hunkmax();
1.1       deraadt   121: }
                    122:
1.17      deraadt   123: /*
                    124:  * Make sure our dynamically realloced tables are malloced to begin with.
                    125:  */
1.1       deraadt   126: void
1.17      deraadt   127: set_hunkmax(void)
1.1       deraadt   128: {
1.21      otto      129:        if (p_line == NULL)
1.37      deraadt   130:                p_line = calloc((size_t) hunkmax, sizeof(char *));
1.21      otto      131:        if (p_len == NULL)
1.37      deraadt   132:                p_len = calloc((size_t) hunkmax, sizeof(short));
1.21      otto      133:        if (p_char == NULL)
1.37      deraadt   134:                p_char = calloc((size_t) hunkmax, sizeof(char));
1.1       deraadt   135: }
                    136:
1.17      deraadt   137: /*
                    138:  * Enlarge the arrays containing the current hunk of patch.
                    139:  */
1.21      otto      140: static void
1.17      deraadt   141: grow_hunkmax(void)
1.1       deraadt   142: {
1.31      otto      143:        int             new_hunkmax;
1.35      deraadt   144:        char            **new_p_line;
1.31      otto      145:        short           *new_p_len;
                    146:        char            *new_p_char;
                    147:
                    148:        new_hunkmax = hunkmax * 2;
1.17      deraadt   149:
1.28      otto      150:        if (p_line == NULL || p_len == NULL || p_char == NULL)
                    151:                fatal("Internal memory allocation error\n");
1.21      otto      152:
1.47    ! deraadt   153:        new_p_line = reallocarray(p_line, new_hunkmax, sizeof(char *));
1.31      otto      154:        if (new_p_line == NULL)
                    155:                free(p_line);
                    156:
1.47    ! deraadt   157:        new_p_len = reallocarray(p_len, new_hunkmax, sizeof(short));
1.31      otto      158:        if (new_p_len == NULL)
                    159:                free(p_len);
                    160:
1.47    ! deraadt   161:        new_p_char = reallocarray(p_char, new_hunkmax, sizeof(char));
1.31      otto      162:        if (new_p_char == NULL)
                    163:                free(p_char);
                    164:
                    165:        p_char = new_p_char;
                    166:        p_len = new_p_len;
                    167:        p_line = new_p_line;
1.21      otto      168:
1.31      otto      169:        if (p_line != NULL && p_len != NULL && p_char != NULL) {
                    170:                hunkmax = new_hunkmax;
1.17      deraadt   171:                return;
1.31      otto      172:        }
                    173:
1.17      deraadt   174:        if (!using_plan_a)
                    175:                fatal("out of memory\n");
1.28      otto      176:        out_of_mem = true;      /* whatever is null will be allocated again */
                    177:                                /* from within plan_a(), of all places */
1.1       deraadt   178: }
                    179:
                    180: /* True if the remainder of the patch file contains a diff of some sort. */
                    181:
                    182: bool
1.17      deraadt   183: there_is_another_patch(void)
1.1       deraadt   184: {
1.32      millert   185:        bool exists = false;
                    186:
1.42      tobias    187:        if (p_base != 0 && p_base >= p_filesize) {
1.17      deraadt   188:                if (verbose)
                    189:                        say("done\n");
1.28      otto      190:                return false;
1.17      deraadt   191:        }
1.1       deraadt   192:        if (verbose)
1.17      deraadt   193:                say("Hmm...");
                    194:        diff_type = intuit_diff_type();
                    195:        if (!diff_type) {
1.42      tobias    196:                if (p_base != 0) {
1.17      deraadt   197:                        if (verbose)
                    198:                                say("  Ignoring the trailing garbage.\ndone\n");
                    199:                } else
                    200:                        say("  I can't seem to find a patch in there anywhere.\n");
1.28      otto      201:                return false;
1.1       deraadt   202:        }
1.17      deraadt   203:        if (verbose)
                    204:                say("  %sooks like %s to me...\n",
1.42      tobias    205:                    (p_base == 0 ? "L" : "The next patch l"),
1.17      deraadt   206:                    diff_type == UNI_DIFF ? "a unified diff" :
                    207:                    diff_type == CONTEXT_DIFF ? "a context diff" :
                    208:                diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
                    209:                    diff_type == NORMAL_DIFF ? "a normal diff" :
                    210:                    "an ed script");
                    211:        if (p_indent && verbose)
                    212:                say("(Patch is indented %d space%s.)\n", p_indent,
                    213:                    p_indent == 1 ? "" : "s");
                    214:        skip_to(p_start, p_sline);
1.21      otto      215:        while (filearg[0] == NULL) {
1.17      deraadt   216:                if (force || batch) {
                    217:                        say("No file to patch.  Skipping...\n");
                    218:                        filearg[0] = savestr(bestguess);
1.28      otto      219:                        skip_rest_of_patch = true;
                    220:                        return true;
1.17      deraadt   221:                }
                    222:                ask("File to patch: ");
                    223:                if (*buf != '\n') {
1.25      otto      224:                        free(bestguess);
1.17      deraadt   225:                        bestguess = savestr(buf);
1.32      millert   226:                        filearg[0] = fetchname(buf, &exists, 0);
1.17      deraadt   227:                }
1.32      millert   228:                if (!exists) {
1.17      deraadt   229:                        ask("No file found--skip this patch? [n] ");
                    230:                        if (*buf != 'y')
                    231:                                continue;
                    232:                        if (verbose)
                    233:                                say("Skipping patch...\n");
1.33      otto      234:                        free(filearg[0]);
1.32      millert   235:                        filearg[0] = fetchname(bestguess, &exists, 0);
1.28      otto      236:                        skip_rest_of_patch = true;
                    237:                        return true;
1.17      deraadt   238:                }
1.1       deraadt   239:        }
1.28      otto      240:        return true;
1.1       deraadt   241: }
                    242:
                    243: /* Determine what kind of diff is in the remaining part of the patch file. */
                    244:
1.21      otto      245: static int
1.17      deraadt   246: intuit_diff_type(void)
1.1       deraadt   247: {
1.42      tobias    248:        off_t   this_line = 0, previous_line;
                    249:        off_t   first_command_line = -1;
1.28      otto      250:        LINENUM fcl_line = -1;
                    251:        bool    last_line_was_command = false, this_is_a_command = false;
                    252:        bool    stars_last_line = false, stars_this_line = false;
1.17      deraadt   253:        char    *s, *t;
                    254:        int     indent, retval;
1.32      millert   255:        struct file_name names[MAX_FILE];
1.17      deraadt   256:
1.32      millert   257:        memset(names, 0, sizeof(names));
1.28      otto      258:        ok_to_create_file = false;
1.42      tobias    259:        fseeko(pfp, p_base, SEEK_SET);
1.17      deraadt   260:        p_input_line = p_bline - 1;
                    261:        for (;;) {
                    262:                previous_line = this_line;
                    263:                last_line_was_command = this_is_a_command;
                    264:                stars_last_line = stars_this_line;
1.42      tobias    265:                this_line = ftello(pfp);
1.17      deraadt   266:                indent = 0;
                    267:                p_input_line++;
1.21      otto      268:                if (fgets(buf, sizeof buf, pfp) == NULL) {
1.42      tobias    269:                        if (first_command_line >= 0) {
1.17      deraadt   270:                                /* nothing but deletes!? */
                    271:                                p_start = first_command_line;
                    272:                                p_sline = fcl_line;
                    273:                                retval = ED_DIFF;
                    274:                                goto scan_exit;
                    275:                        } else {
                    276:                                p_start = this_line;
                    277:                                p_sline = p_input_line;
                    278:                                retval = 0;
                    279:                                goto scan_exit;
                    280:                        }
                    281:                }
                    282:                for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
                    283:                        if (*s == '\t')
                    284:                                indent += 8 - (indent % 8);
                    285:                        else
                    286:                                indent++;
                    287:                }
1.41      deraadt   288:                for (t = s; isdigit((unsigned char)*t) || *t == ','; t++)
1.17      deraadt   289:                        ;
1.41      deraadt   290:                this_is_a_command = (isdigit((unsigned char)*s) &&
1.17      deraadt   291:                    (*t == 'd' || *t == 'c' || *t == 'a'));
1.42      tobias    292:                if (first_command_line < 0 && this_is_a_command) {
1.17      deraadt   293:                        first_command_line = this_line;
                    294:                        fcl_line = p_input_line;
                    295:                        p_indent = indent;      /* assume this for now */
                    296:                }
                    297:                if (!stars_last_line && strnEQ(s, "*** ", 4))
1.32      millert   298:                        names[OLD_FILE].path = fetchname(s + 4,
                    299:                            &names[OLD_FILE].exists, strippath);
1.17      deraadt   300:                else if (strnEQ(s, "--- ", 4))
1.32      millert   301:                        names[NEW_FILE].path = fetchname(s + 4,
                    302:                            &names[NEW_FILE].exists, strippath);
1.17      deraadt   303:                else if (strnEQ(s, "+++ ", 4))
1.32      millert   304:                        /* pretend it is the old name */
                    305:                        names[OLD_FILE].path = fetchname(s + 4,
                    306:                            &names[OLD_FILE].exists, strippath);
1.17      deraadt   307:                else if (strnEQ(s, "Index:", 6))
1.32      millert   308:                        names[INDEX_FILE].path = fetchname(s + 6,
                    309:                            &names[INDEX_FILE].exists, strippath);
1.17      deraadt   310:                else if (strnEQ(s, "Prereq:", 7)) {
1.41      deraadt   311:                        for (t = s + 7; isspace((unsigned char)*t); t++)
1.18      deraadt   312:                                ;
1.17      deraadt   313:                        revision = savestr(t);
1.41      deraadt   314:                        for (t = revision;
                    315:                            *t && !isspace((unsigned char)*t); t++)
1.18      deraadt   316:                                ;
1.17      deraadt   317:                        *t = '\0';
1.25      otto      318:                        if (*revision == '\0') {
1.17      deraadt   319:                                free(revision);
1.21      otto      320:                                revision = NULL;
1.17      deraadt   321:                        }
                    322:                }
                    323:                if ((!diff_type || diff_type == ED_DIFF) &&
1.42      tobias    324:                    first_command_line >= 0 &&
1.17      deraadt   325:                    strEQ(s, ".\n")) {
                    326:                        p_indent = indent;
                    327:                        p_start = first_command_line;
                    328:                        p_sline = fcl_line;
                    329:                        retval = ED_DIFF;
                    330:                        goto scan_exit;
                    331:                }
                    332:                if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
1.32      millert   333:                        if (strnEQ(s + 4, "0,0", 3))
1.28      otto      334:                                ok_to_create_file = true;
1.17      deraadt   335:                        p_indent = indent;
                    336:                        p_start = this_line;
                    337:                        p_sline = p_input_line;
                    338:                        retval = UNI_DIFF;
                    339:                        goto scan_exit;
                    340:                }
                    341:                stars_this_line = strnEQ(s, "********", 8);
                    342:                if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
                    343:                    strnEQ(s, "*** ", 4)) {
1.45      tobias    344:                        if (strtolinenum(s + 4, &s) == 0)
1.28      otto      345:                                ok_to_create_file = true;
1.17      deraadt   346:                        /*
1.32      millert   347:                         * If this is a new context diff the character just
1.43      tobias    348:                         * at the end of the line is a '*'.
1.17      deraadt   349:                         */
1.43      tobias    350:                        while (*s && *s != '\n')
1.17      deraadt   351:                                s++;
                    352:                        p_indent = indent;
                    353:                        p_start = previous_line;
                    354:                        p_sline = p_input_line - 1;
                    355:                        retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
                    356:                        goto scan_exit;
                    357:                }
                    358:                if ((!diff_type || diff_type == NORMAL_DIFF) &&
                    359:                    last_line_was_command &&
                    360:                    (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) {
                    361:                        p_start = previous_line;
                    362:                        p_sline = p_input_line - 1;
                    363:                        p_indent = indent;
                    364:                        retval = NORMAL_DIFF;
                    365:                        goto scan_exit;
                    366:                }
                    367:        }
                    368: scan_exit:
1.32      millert   369:        if (retval == UNI_DIFF) {
                    370:                /* unswap old and new */
                    371:                struct file_name tmp = names[OLD_FILE];
                    372:                names[OLD_FILE] = names[NEW_FILE];
                    373:                names[NEW_FILE] = tmp;
                    374:        }
                    375:        if (filearg[0] == NULL) {
                    376:                if (posix)
                    377:                        filearg[0] = posix_name(names, ok_to_create_file);
                    378:                else {
                    379:                        /* Ignore the Index: name for context diffs, like GNU */
                    380:                        if (names[OLD_FILE].path != NULL ||
                    381:                            names[NEW_FILE].path != NULL) {
                    382:                                free(names[INDEX_FILE].path);
                    383:                                names[INDEX_FILE].path = NULL;
                    384:                        }
                    385:                        filearg[0] = best_name(names, ok_to_create_file);
                    386:                }
1.17      deraadt   387:        }
1.25      otto      388:
                    389:        free(bestguess);
                    390:        bestguess = NULL;
1.21      otto      391:        if (filearg[0] != NULL)
1.17      deraadt   392:                bestguess = savestr(filearg[0]);
1.32      millert   393:        else if (!ok_to_create_file) {
                    394:                /*
                    395:                 * We don't want to create a new file but we need a
                    396:                 * filename to set bestguess.  Avoid setting filearg[0]
                    397:                 * so the file is not created automatically.
                    398:                 */
                    399:                if (posix)
                    400:                        bestguess = posix_name(names, true);
                    401:                else
                    402:                        bestguess = best_name(names, true);
                    403:        }
                    404:        free(names[OLD_FILE].path);
                    405:        free(names[NEW_FILE].path);
                    406:        free(names[INDEX_FILE].path);
1.17      deraadt   407:        return retval;
1.1       deraadt   408: }
                    409:
1.17      deraadt   410: /*
                    411:  * Remember where this patch ends so we know where to start up again.
                    412:  */
1.21      otto      413: static void
1.42      tobias    414: next_intuit_at(off_t file_pos, LINENUM file_line)
1.1       deraadt   415: {
1.17      deraadt   416:        p_base = file_pos;
                    417:        p_bline = file_line;
1.1       deraadt   418: }
                    419:
1.17      deraadt   420: /*
1.42      tobias    421:  * Basically a verbose fseeko() to the actual diff listing.
1.17      deraadt   422:  */
1.21      otto      423: static void
1.42      tobias    424: skip_to(off_t file_pos, LINENUM file_line)
1.1       deraadt   425: {
1.17      deraadt   426:        char    *ret;
1.1       deraadt   427:
1.28      otto      428:        if (p_base > file_pos)
1.42      tobias    429:                fatal("Internal error: seek %lld>%lld\n",
                    430:                    (long long)p_base, (long long)file_pos);
1.17      deraadt   431:        if (verbose && p_base < file_pos) {
1.42      tobias    432:                fseeko(pfp, p_base, SEEK_SET);
1.17      deraadt   433:                say("The text leading up to this was:\n--------------------------\n");
1.42      tobias    434:                while (ftello(pfp) < file_pos) {
1.17      deraadt   435:                        ret = fgets(buf, sizeof buf, pfp);
1.28      otto      436:                        if (ret == NULL)
                    437:                                fatal("Unexpected end of file\n");
1.17      deraadt   438:                        say("|%s", buf);
                    439:                }
                    440:                say("--------------------------\n");
                    441:        } else
1.42      tobias    442:                fseeko(pfp, file_pos, SEEK_SET);
1.17      deraadt   443:        p_input_line = file_line - 1;
1.1       deraadt   444: }
                    445:
                    446: /* Make this a function for better debugging.  */
                    447: static void
1.16      deraadt   448: malformed(void)
1.1       deraadt   449: {
1.17      deraadt   450:        fatal("malformed patch at line %ld: %s", p_input_line, buf);
                    451:        /* about as informative as "Syntax error" in C */
1.1       deraadt   452: }
                    453:
1.14      otto      454: /*
                    455:  * True if the line has been discarded (i.e. it is a line saying
                    456:  *  "\ No newline at end of file".)
                    457:  */
                    458: static bool
                    459: remove_special_line(void)
                    460: {
1.17      deraadt   461:        int     c;
1.14      otto      462:
                    463:        c = fgetc(pfp);
                    464:        if (c == '\\') {
                    465:                do {
                    466:                        c = fgetc(pfp);
                    467:                } while (c != EOF && c != '\n');
                    468:
1.28      otto      469:                return true;
1.14      otto      470:        }
                    471:        if (c != EOF)
1.42      tobias    472:                fseeko(pfp, -1, SEEK_CUR);
1.14      otto      473:
1.28      otto      474:        return false;
1.14      otto      475: }
                    476:
1.17      deraadt   477: /*
                    478:  * True if there is more of the current diff listing to process.
                    479:  */
1.1       deraadt   480: bool
1.17      deraadt   481: another_hunk(void)
1.1       deraadt   482: {
1.42      tobias    483:        off_t   line_beginning;                 /* file pos of the current line */
1.17      deraadt   484:        LINENUM repl_beginning;                 /* index of --- line */
                    485:        LINENUM fillcnt;                        /* #lines of missing ptrn or repl */
                    486:        LINENUM fillsrc;                        /* index of first line to copy */
                    487:        LINENUM filldst;                        /* index of first missing line */
                    488:        bool    ptrn_spaces_eaten;              /* ptrn was slightly misformed */
                    489:        bool    repl_could_be_missing;          /* no + or ! lines in this hunk */
                    490:        bool    repl_missing;                   /* we are now backtracking */
1.42      tobias    491:        off_t   repl_backtrack_position;        /* file pos of first repl line */
1.17      deraadt   492:        LINENUM repl_patch_line;                /* input line number for same */
                    493:        LINENUM ptrn_copiable;                  /* # of copiable lines in ptrn */
                    494:        char    *s, *ret;
                    495:        int     context = 0;
                    496:
                    497:        while (p_end >= 0) {
                    498:                if (p_end == p_efake)
                    499:                        p_end = p_bfake;        /* don't free twice */
                    500:                else
                    501:                        free(p_line[p_end]);
                    502:                p_end--;
                    503:        }
                    504:        p_efake = -1;
                    505:
                    506:        p_max = hunkmax;        /* gets reduced when --- found */
                    507:        if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
1.42      tobias    508:                line_beginning = ftello(pfp);
1.17      deraadt   509:                repl_beginning = 0;
                    510:                fillcnt = 0;
1.39      ajacouto  511:                fillsrc = 0;
1.28      otto      512:                ptrn_spaces_eaten = false;
                    513:                repl_could_be_missing = true;
                    514:                repl_missing = false;
1.17      deraadt   515:                repl_backtrack_position = 0;
1.39      ajacouto  516:                repl_patch_line = 0;
1.17      deraadt   517:                ptrn_copiable = 0;
                    518:
                    519:                ret = pgets(buf, sizeof buf, pfp);
                    520:                p_input_line++;
1.21      otto      521:                if (ret == NULL || strnNE(buf, "********", 8)) {
1.17      deraadt   522:                        next_intuit_at(line_beginning, p_input_line);
1.28      otto      523:                        return false;
1.17      deraadt   524:                }
                    525:                p_context = 100;
                    526:                p_hunk_beg = p_input_line + 1;
                    527:                while (p_end < p_max) {
1.42      tobias    528:                        line_beginning = ftello(pfp);
1.17      deraadt   529:                        ret = pgets(buf, sizeof buf, pfp);
                    530:                        p_input_line++;
1.21      otto      531:                        if (ret == NULL) {
1.17      deraadt   532:                                if (p_max - p_end < 4) {
                    533:                                        /* assume blank lines got chopped */
                    534:                                        strlcpy(buf, "  \n", sizeof buf);
                    535:                                } else {
                    536:                                        if (repl_beginning && repl_could_be_missing) {
1.28      otto      537:                                                repl_missing = true;
1.17      deraadt   538:                                                goto hunk_done;
                    539:                                        }
                    540:                                        fatal("unexpected end of file in patch\n");
                    541:                                }
                    542:                        }
                    543:                        p_end++;
1.28      otto      544:                        if (p_end >= hunkmax)
                    545:                                fatal("Internal error: hunk larger than hunk "
1.29      deraadt   546:                                    "buffer size");
1.17      deraadt   547:                        p_char[p_end] = *buf;
1.21      otto      548:                        p_line[p_end] = NULL;
1.17      deraadt   549:                        switch (*buf) {
                    550:                        case '*':
                    551:                                if (strnEQ(buf, "********", 8)) {
                    552:                                        if (repl_beginning && repl_could_be_missing) {
1.28      otto      553:                                                repl_missing = true;
1.17      deraadt   554:                                                goto hunk_done;
                    555:                                        } else
                    556:                                                fatal("unexpected end of hunk "
                    557:                                                    "at line %ld\n",
                    558:                                                    p_input_line);
                    559:                                }
                    560:                                if (p_end != 0) {
                    561:                                        if (repl_beginning && repl_could_be_missing) {
1.28      otto      562:                                                repl_missing = true;
1.17      deraadt   563:                                                goto hunk_done;
                    564:                                        }
                    565:                                        fatal("unexpected *** at line %ld: %s",
                    566:                                            p_input_line, buf);
                    567:                                }
                    568:                                context = 0;
                    569:                                p_line[p_end] = savestr(buf);
                    570:                                if (out_of_mem) {
                    571:                                        p_end--;
1.28      otto      572:                                        return false;
1.17      deraadt   573:                                }
1.41      deraadt   574:                                for (s = buf;
                    575:                                    *s && !isdigit((unsigned char)*s); s++)
1.17      deraadt   576:                                        ;
                    577:                                if (!*s)
                    578:                                        malformed();
                    579:                                if (strnEQ(s, "0,0", 3))
                    580:                                        memmove(s, s + 2, strlen(s + 2) + 1);
1.45      tobias    581:                                p_first = strtolinenum(s, &s);
1.17      deraadt   582:                                if (*s == ',') {
1.41      deraadt   583:                                        for (; *s && !isdigit((unsigned char)*s); s++)
1.18      deraadt   584:                                                ;
1.17      deraadt   585:                                        if (!*s)
                    586:                                                malformed();
1.45      tobias    587:                                        p_ptrn_lines = strtolinenum(s, &s) - p_first + 1;
1.46      tobias    588:                                        if (p_ptrn_lines < 0)
                    589:                                                malformed();
1.17      deraadt   590:                                } else if (p_first)
                    591:                                        p_ptrn_lines = 1;
                    592:                                else {
                    593:                                        p_ptrn_lines = 0;
                    594:                                        p_first = 1;
                    595:                                }
1.46      tobias    596:                                if (p_first >= LINENUM_MAX - p_ptrn_lines ||
                    597:                                    p_ptrn_lines >= LINENUM_MAX - 6)
                    598:                                        malformed();
1.26      deraadt   599:
                    600:                                /* we need this much at least */
                    601:                                p_max = p_ptrn_lines + 6;
1.17      deraadt   602:                                while (p_max >= hunkmax)
                    603:                                        grow_hunkmax();
                    604:                                p_max = hunkmax;
                    605:                                break;
                    606:                        case '-':
                    607:                                if (buf[1] == '-') {
                    608:                                        if (repl_beginning ||
                    609:                                            (p_end != p_ptrn_lines + 1 +
                    610:                                            (p_char[p_end - 1] == '\n'))) {
                    611:                                                if (p_end == 1) {
                    612:                                                        /*
                    613:                                                         * `old' lines were omitted;
                    614:                                                         * set up to fill them in
                    615:                                                         * from 'new' context lines.
                    616:                                                         */
                    617:                                                        p_end = p_ptrn_lines + 1;
                    618:                                                        fillsrc = p_end + 1;
                    619:                                                        filldst = 1;
                    620:                                                        fillcnt = p_ptrn_lines;
                    621:                                                } else {
                    622:                                                        if (repl_beginning) {
                    623:                                                                if (repl_could_be_missing) {
1.28      otto      624:                                                                        repl_missing = true;
1.17      deraadt   625:                                                                        goto hunk_done;
                    626:                                                                }
                    627:                                                                fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
                    628:                                                                    p_input_line, p_hunk_beg + repl_beginning);
                    629:                                                        } else {
                    630:                                                                fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
                    631:                                                                    (p_end <= p_ptrn_lines
                    632:                                                                    ? "Premature"
                    633:                                                                    : "Overdue"),
                    634:                                                                    p_input_line, p_hunk_beg);
                    635:                                                        }
                    636:                                                }
                    637:                                        }
                    638:                                        repl_beginning = p_end;
1.42      tobias    639:                                        repl_backtrack_position = ftello(pfp);
1.17      deraadt   640:                                        repl_patch_line = p_input_line;
                    641:                                        p_line[p_end] = savestr(buf);
                    642:                                        if (out_of_mem) {
                    643:                                                p_end--;
1.28      otto      644:                                                return false;
1.17      deraadt   645:                                        }
                    646:                                        p_char[p_end] = '=';
1.41      deraadt   647:                                        for (s = buf;
                    648:                                            *s && !isdigit((unsigned char)*s); s++)
1.17      deraadt   649:                                                ;
                    650:                                        if (!*s)
                    651:                                                malformed();
1.45      tobias    652:                                        p_newfirst = strtolinenum(s, &s);
1.17      deraadt   653:                                        if (*s == ',') {
1.41      deraadt   654:                                                for (; *s && !isdigit((unsigned char)*s); s++)
1.17      deraadt   655:                                                        ;
                    656:                                                if (!*s)
                    657:                                                        malformed();
1.45      tobias    658:                                                p_repl_lines = strtolinenum(s, &s) -
1.17      deraadt   659:                                                    p_newfirst + 1;
1.46      tobias    660:                                                if (p_repl_lines < 0)
                    661:                                                        malformed();
1.17      deraadt   662:                                        } else if (p_newfirst)
                    663:                                                p_repl_lines = 1;
                    664:                                        else {
                    665:                                                p_repl_lines = 0;
                    666:                                                p_newfirst = 1;
                    667:                                        }
1.46      tobias    668:                                        if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
                    669:                                            p_repl_lines >= LINENUM_MAX - p_end)
                    670:                                                malformed();
1.17      deraadt   671:                                        p_max = p_repl_lines + p_end;
                    672:                                        if (p_max > MAXHUNKSIZE)
                    673:                                                fatal("hunk too large (%ld lines) at line %ld: %s",
                    674:                                                    p_max, p_input_line, buf);
                    675:                                        while (p_max >= hunkmax)
                    676:                                                grow_hunkmax();
                    677:                                        if (p_repl_lines != ptrn_copiable &&
                    678:                                            (p_context != 0 || p_repl_lines != 1))
1.28      otto      679:                                                repl_could_be_missing = false;
1.17      deraadt   680:                                        break;
                    681:                                }
                    682:                                goto change_line;
                    683:                        case '+':
                    684:                        case '!':
1.28      otto      685:                                repl_could_be_missing = false;
1.17      deraadt   686:                change_line:
                    687:                                if (buf[1] == '\n' && canonicalize)
                    688:                                        strlcpy(buf + 1, " \n", sizeof buf - 1);
1.41      deraadt   689:                                if (!isspace((unsigned char)buf[1]) &&
                    690:                                    buf[1] != '>' && buf[1] != '<' &&
1.17      deraadt   691:                                    repl_beginning && repl_could_be_missing) {
1.28      otto      692:                                        repl_missing = true;
1.17      deraadt   693:                                        goto hunk_done;
                    694:                                }
                    695:                                if (context >= 0) {
                    696:                                        if (context < p_context)
                    697:                                                p_context = context;
                    698:                                        context = -1000;
                    699:                                }
                    700:                                p_line[p_end] = savestr(buf + 2);
                    701:                                if (out_of_mem) {
                    702:                                        p_end--;
1.28      otto      703:                                        return false;
1.17      deraadt   704:                                }
                    705:                                if (p_end == p_ptrn_lines) {
                    706:                                        if (remove_special_line()) {
                    707:                                                int     len;
                    708:
                    709:                                                len = strlen(p_line[p_end]) - 1;
                    710:                                                (p_line[p_end])[len] = 0;
                    711:                                        }
                    712:                                }
                    713:                                break;
                    714:                        case '\t':
                    715:                        case '\n':      /* assume the 2 spaces got eaten */
                    716:                                if (repl_beginning && repl_could_be_missing &&
                    717:                                    (!ptrn_spaces_eaten ||
                    718:                                    diff_type == NEW_CONTEXT_DIFF)) {
1.28      otto      719:                                        repl_missing = true;
1.17      deraadt   720:                                        goto hunk_done;
                    721:                                }
                    722:                                p_line[p_end] = savestr(buf);
                    723:                                if (out_of_mem) {
                    724:                                        p_end--;
1.28      otto      725:                                        return false;
1.17      deraadt   726:                                }
                    727:                                if (p_end != p_ptrn_lines + 1) {
                    728:                                        ptrn_spaces_eaten |= (repl_beginning != 0);
                    729:                                        context++;
                    730:                                        if (!repl_beginning)
                    731:                                                ptrn_copiable++;
                    732:                                        p_char[p_end] = ' ';
                    733:                                }
                    734:                                break;
                    735:                        case ' ':
1.41      deraadt   736:                                if (!isspace((unsigned char)buf[1]) &&
1.23      deraadt   737:                                    repl_beginning && repl_could_be_missing) {
1.28      otto      738:                                        repl_missing = true;
1.17      deraadt   739:                                        goto hunk_done;
                    740:                                }
                    741:                                context++;
                    742:                                if (!repl_beginning)
                    743:                                        ptrn_copiable++;
                    744:                                p_line[p_end] = savestr(buf + 2);
                    745:                                if (out_of_mem) {
                    746:                                        p_end--;
1.28      otto      747:                                        return false;
1.17      deraadt   748:                                }
                    749:                                break;
                    750:                        default:
                    751:                                if (repl_beginning && repl_could_be_missing) {
1.28      otto      752:                                        repl_missing = true;
1.17      deraadt   753:                                        goto hunk_done;
                    754:                                }
                    755:                                malformed();
                    756:                        }
                    757:                        /* set up p_len for strncmp() so we don't have to */
                    758:                        /* assume null termination */
                    759:                        if (p_line[p_end])
                    760:                                p_len[p_end] = strlen(p_line[p_end]);
                    761:                        else
                    762:                                p_len[p_end] = 0;
                    763:                }
                    764:
                    765: hunk_done:
                    766:                if (p_end >= 0 && !repl_beginning)
                    767:                        fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
                    768:
                    769:                if (repl_missing) {
                    770:
                    771:                        /* reset state back to just after --- */
                    772:                        p_input_line = repl_patch_line;
                    773:                        for (p_end--; p_end > repl_beginning; p_end--)
                    774:                                free(p_line[p_end]);
1.42      tobias    775:                        fseeko(pfp, repl_backtrack_position, SEEK_SET);
1.17      deraadt   776:
                    777:                        /* redundant 'new' context lines were omitted - set */
                    778:                        /* up to fill them in from the old file context */
                    779:                        if (!p_context && p_repl_lines == 1) {
                    780:                                p_repl_lines = 0;
                    781:                                p_max--;
                    782:                        }
                    783:                        fillsrc = 1;
                    784:                        filldst = repl_beginning + 1;
                    785:                        fillcnt = p_repl_lines;
                    786:                        p_end = p_max;
                    787:                } else if (!p_context && fillcnt == 1) {
                    788:                        /* the first hunk was a null hunk with no context */
                    789:                        /* and we were expecting one line -- fix it up. */
                    790:                        while (filldst < p_end) {
                    791:                                p_line[filldst] = p_line[filldst + 1];
                    792:                                p_char[filldst] = p_char[filldst + 1];
                    793:                                p_len[filldst] = p_len[filldst + 1];
                    794:                                filldst++;
                    795:                        }
                    796: #if 0
1.26      deraadt   797:                        repl_beginning--;       /* this doesn't need to be fixed */
1.17      deraadt   798: #endif
                    799:                        p_end--;
                    800:                        p_first++;      /* do append rather than insert */
                    801:                        fillcnt = 0;
                    802:                        p_ptrn_lines = 0;
                    803:                }
                    804:                if (diff_type == CONTEXT_DIFF &&
                    805:                    (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
                    806:                        if (verbose)
                    807:                                say("%s\n%s\n%s\n",
                    808:                                    "(Fascinating--this is really a new-style context diff but without",
                    809:                                    "the telltale extra asterisks on the *** line that usually indicate",
                    810:                                    "the new style...)");
                    811:                        diff_type = NEW_CONTEXT_DIFF;
                    812:                }
                    813:                /* if there were omitted context lines, fill them in now */
                    814:                if (fillcnt) {
1.26      deraadt   815:                        p_bfake = filldst;      /* remember where not to free() */
1.17      deraadt   816:                        p_efake = filldst + fillcnt - 1;
                    817:                        while (fillcnt-- > 0) {
                    818:                                while (fillsrc <= p_end && p_char[fillsrc] != ' ')
                    819:                                        fillsrc++;
                    820:                                if (fillsrc > p_end)
                    821:                                        fatal("replacement text or line numbers mangled in hunk at line %ld\n",
                    822:                                            p_hunk_beg);
                    823:                                p_line[filldst] = p_line[fillsrc];
                    824:                                p_char[filldst] = p_char[fillsrc];
                    825:                                p_len[filldst] = p_len[fillsrc];
                    826:                                fillsrc++;
                    827:                                filldst++;
                    828:                        }
                    829:                        while (fillsrc <= p_end && fillsrc != repl_beginning &&
                    830:                            p_char[fillsrc] != ' ')
                    831:                                fillsrc++;
                    832: #ifdef DEBUGGING
                    833:                        if (debug & 64)
                    834:                                printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
                    835:                                fillsrc, filldst, repl_beginning, p_end + 1);
1.1       deraadt   836: #endif
1.28      otto      837:                        if (fillsrc != p_end + 1 && fillsrc != repl_beginning)
                    838:                                malformed();
                    839:                        if (filldst != p_end + 1 && filldst != repl_beginning)
                    840:                                malformed();
1.1       deraadt   841:                }
1.17      deraadt   842:                if (p_line[p_end] != NULL) {
                    843:                        if (remove_special_line()) {
                    844:                                p_len[p_end] -= 1;
                    845:                                (p_line[p_end])[p_len[p_end]] = 0;
                    846:                        }
                    847:                }
                    848:        } else if (diff_type == UNI_DIFF) {
1.42      tobias    849:                off_t   line_beginning = ftello(pfp); /* file pos of the current line */
1.17      deraadt   850:                LINENUM fillsrc;        /* index of old lines */
                    851:                LINENUM filldst;        /* index of new lines */
                    852:                char    ch;
                    853:
                    854:                ret = pgets(buf, sizeof buf, pfp);
                    855:                p_input_line++;
1.21      otto      856:                if (ret == NULL || strnNE(buf, "@@ -", 4)) {
1.17      deraadt   857:                        next_intuit_at(line_beginning, p_input_line);
1.28      otto      858:                        return false;
1.1       deraadt   859:                }
1.17      deraadt   860:                s = buf + 4;
1.1       deraadt   861:                if (!*s)
1.17      deraadt   862:                        malformed();
1.45      tobias    863:                p_first = strtolinenum(s, &s);
1.17      deraadt   864:                if (*s == ',') {
1.45      tobias    865:                        p_ptrn_lines = strtolinenum(s + 1, &s);
1.17      deraadt   866:                } else
                    867:                        p_ptrn_lines = 1;
                    868:                if (*s == ' ')
                    869:                        s++;
                    870:                if (*s != '+' || !*++s)
                    871:                        malformed();
1.45      tobias    872:                p_newfirst = strtolinenum(s, &s);
1.1       deraadt   873:                if (*s == ',') {
1.45      tobias    874:                        p_repl_lines = strtolinenum(s + 1, &s);
1.17      deraadt   875:                } else
                    876:                        p_repl_lines = 1;
                    877:                if (*s == ' ')
                    878:                        s++;
                    879:                if (*s != '@')
                    880:                        malformed();
1.46      tobias    881:                if (p_first >= LINENUM_MAX - p_ptrn_lines ||
                    882:                    p_newfirst > LINENUM_MAX - p_repl_lines ||
                    883:                    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
                    884:                        malformed();
1.17      deraadt   885:                if (!p_ptrn_lines)
                    886:                        p_first++;      /* do append rather than insert */
                    887:                p_max = p_ptrn_lines + p_repl_lines + 1;
                    888:                while (p_max >= hunkmax)
                    889:                        grow_hunkmax();
                    890:                fillsrc = 1;
                    891:                filldst = fillsrc + p_ptrn_lines;
                    892:                p_end = filldst + p_repl_lines;
                    893:                snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,
1.26      deraadt   894:                    p_first + p_ptrn_lines - 1);
1.17      deraadt   895:                p_line[0] = savestr(buf);
                    896:                if (out_of_mem) {
                    897:                        p_end = -1;
1.28      otto      898:                        return false;
1.17      deraadt   899:                }
                    900:                p_char[0] = '*';
                    901:                snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,
1.26      deraadt   902:                    p_newfirst + p_repl_lines - 1);
1.17      deraadt   903:                p_line[filldst] = savestr(buf);
                    904:                if (out_of_mem) {
                    905:                        p_end = 0;
1.28      otto      906:                        return false;
1.1       deraadt   907:                }
1.17      deraadt   908:                p_char[filldst++] = '=';
                    909:                p_context = 100;
                    910:                context = 0;
                    911:                p_hunk_beg = p_input_line + 1;
                    912:                while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
1.42      tobias    913:                        line_beginning = ftello(pfp);
1.17      deraadt   914:                        ret = pgets(buf, sizeof buf, pfp);
                    915:                        p_input_line++;
1.21      otto      916:                        if (ret == NULL) {
1.17      deraadt   917:                                if (p_max - filldst < 3) {
                    918:                                        /* assume blank lines got chopped */
                    919:                                        strlcpy(buf, " \n", sizeof buf);
                    920:                                } else {
                    921:                                        fatal("unexpected end of file in patch\n");
                    922:                                }
                    923:                        }
                    924:                        if (*buf == '\t' || *buf == '\n') {
                    925:                                ch = ' ';       /* assume the space got eaten */
                    926:                                s = savestr(buf);
                    927:                        } else {
                    928:                                ch = *buf;
                    929:                                s = savestr(buf + 1);
                    930:                        }
                    931:                        if (out_of_mem) {
                    932:                                while (--filldst > p_ptrn_lines)
                    933:                                        free(p_line[filldst]);
                    934:                                p_end = fillsrc - 1;
1.28      otto      935:                                return false;
1.17      deraadt   936:                        }
                    937:                        switch (ch) {
                    938:                        case '-':
                    939:                                if (fillsrc > p_ptrn_lines) {
                    940:                                        free(s);
                    941:                                        p_end = filldst - 1;
                    942:                                        malformed();
                    943:                                }
                    944:                                p_char[fillsrc] = ch;
                    945:                                p_line[fillsrc] = s;
                    946:                                p_len[fillsrc++] = strlen(s);
                    947:                                if (fillsrc > p_ptrn_lines) {
                    948:                                        if (remove_special_line()) {
                    949:                                                p_len[fillsrc - 1] -= 1;
                    950:                                                s[p_len[fillsrc - 1]] = 0;
                    951:                                        }
                    952:                                }
                    953:                                break;
                    954:                        case '=':
                    955:                                ch = ' ';
                    956:                                /* FALL THROUGH */
                    957:                        case ' ':
                    958:                                if (fillsrc > p_ptrn_lines) {
                    959:                                        free(s);
                    960:                                        while (--filldst > p_ptrn_lines)
                    961:                                                free(p_line[filldst]);
                    962:                                        p_end = fillsrc - 1;
                    963:                                        malformed();
                    964:                                }
                    965:                                context++;
                    966:                                p_char[fillsrc] = ch;
                    967:                                p_line[fillsrc] = s;
                    968:                                p_len[fillsrc++] = strlen(s);
                    969:                                s = savestr(s);
                    970:                                if (out_of_mem) {
                    971:                                        while (--filldst > p_ptrn_lines)
                    972:                                                free(p_line[filldst]);
                    973:                                        p_end = fillsrc - 1;
1.28      otto      974:                                        return false;
1.34      otto      975:                                }
                    976:                                if (fillsrc > p_ptrn_lines) {
                    977:                                        if (remove_special_line()) {
                    978:                                                p_len[fillsrc - 1] -= 1;
                    979:                                                s[p_len[fillsrc - 1]] = 0;
                    980:                                        }
1.17      deraadt   981:                                }
                    982:                                /* FALL THROUGH */
                    983:                        case '+':
                    984:                                if (filldst > p_end) {
                    985:                                        free(s);
                    986:                                        while (--filldst > p_ptrn_lines)
                    987:                                                free(p_line[filldst]);
                    988:                                        p_end = fillsrc - 1;
                    989:                                        malformed();
                    990:                                }
                    991:                                p_char[filldst] = ch;
                    992:                                p_line[filldst] = s;
                    993:                                p_len[filldst++] = strlen(s);
                    994:                                if (fillsrc > p_ptrn_lines) {
                    995:                                        if (remove_special_line()) {
                    996:                                                p_len[filldst - 1] -= 1;
                    997:                                                s[p_len[filldst - 1]] = 0;
                    998:                                        }
                    999:                                }
                   1000:                                break;
                   1001:                        default:
                   1002:                                p_end = filldst;
                   1003:                                malformed();
1.1       deraadt  1004:                        }
1.17      deraadt  1005:                        if (ch != ' ' && context > 0) {
                   1006:                                if (context < p_context)
                   1007:                                        p_context = context;
                   1008:                                context = -1000;
1.1       deraadt  1009:                        }
1.17      deraadt  1010:                }               /* while */
                   1011:        } else {                /* normal diff--fake it up */
                   1012:                char    hunk_type;
                   1013:                int     i;
                   1014:                LINENUM min, max;
1.42      tobias   1015:                off_t   line_beginning = ftello(pfp);
1.17      deraadt  1016:
                   1017:                p_context = 0;
                   1018:                ret = pgets(buf, sizeof buf, pfp);
                   1019:                p_input_line++;
1.41      deraadt  1020:                if (ret == NULL || !isdigit((unsigned char)*buf)) {
1.17      deraadt  1021:                        next_intuit_at(line_beginning, p_input_line);
1.28      otto     1022:                        return false;
1.17      deraadt  1023:                }
1.45      tobias   1024:                p_first = strtolinenum(buf, &s);
1.17      deraadt  1025:                if (*s == ',') {
1.45      tobias   1026:                        p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1;
1.46      tobias   1027:                        if (p_ptrn_lines < 0)
                   1028:                                malformed();
1.17      deraadt  1029:                } else
                   1030:                        p_ptrn_lines = (*s != 'a');
1.46      tobias   1031:                if (p_first >= LINENUM_MAX - p_ptrn_lines)
                   1032:                        malformed();
1.17      deraadt  1033:                hunk_type = *s;
                   1034:                if (hunk_type == 'a')
                   1035:                        p_first++;      /* do append rather than insert */
1.45      tobias   1036:                min = strtolinenum(s + 1, &s);
1.17      deraadt  1037:                if (*s == ',')
1.45      tobias   1038:                        max = strtolinenum(s + 1, &s);
1.17      deraadt  1039:                else
                   1040:                        max = min;
1.46      tobias   1041:                if (min < 0 || min > max || max - min == LINENUM_MAX)
                   1042:                        malformed();
1.17      deraadt  1043:                if (hunk_type == 'd')
                   1044:                        min++;
1.46      tobias   1045:                p_newfirst = min;
                   1046:                p_repl_lines = max - min + 1;
                   1047:                if (p_newfirst > LINENUM_MAX - p_repl_lines ||
                   1048:                    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
                   1049:                        malformed();
                   1050:                p_end = p_ptrn_lines + p_repl_lines + 1;
1.17      deraadt  1051:                if (p_end > MAXHUNKSIZE)
1.16      deraadt  1052:                        fatal("hunk too large (%ld lines) at line %ld: %s",
1.17      deraadt  1053:                            p_end, p_input_line, buf);
                   1054:                while (p_end >= hunkmax)
1.1       deraadt  1055:                        grow_hunkmax();
1.17      deraadt  1056:                snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,
1.26      deraadt  1057:                    p_first + p_ptrn_lines - 1);
1.17      deraadt  1058:                p_line[0] = savestr(buf);
1.1       deraadt  1059:                if (out_of_mem) {
1.17      deraadt  1060:                        p_end = -1;
1.28      otto     1061:                        return false;
1.1       deraadt  1062:                }
1.17      deraadt  1063:                p_char[0] = '*';
                   1064:                for (i = 1; i <= p_ptrn_lines; i++) {
                   1065:                        ret = pgets(buf, sizeof buf, pfp);
                   1066:                        p_input_line++;
1.21      otto     1067:                        if (ret == NULL)
1.17      deraadt  1068:                                fatal("unexpected end of file in patch at line %ld\n",
                   1069:                                    p_input_line);
                   1070:                        if (*buf != '<')
                   1071:                                fatal("< expected at line %ld of patch\n",
                   1072:                                    p_input_line);
                   1073:                        p_line[i] = savestr(buf + 2);
                   1074:                        if (out_of_mem) {
                   1075:                                p_end = i - 1;
1.28      otto     1076:                                return false;
1.14      otto     1077:                        }
1.17      deraadt  1078:                        p_len[i] = strlen(p_line[i]);
                   1079:                        p_char[i] = '-';
1.14      otto     1080:                }
1.17      deraadt  1081:
                   1082:                if (remove_special_line()) {
                   1083:                        p_len[i - 1] -= 1;
                   1084:                        (p_line[i - 1])[p_len[i - 1]] = 0;
1.1       deraadt  1085:                }
1.17      deraadt  1086:                if (hunk_type == 'c') {
                   1087:                        ret = pgets(buf, sizeof buf, pfp);
                   1088:                        p_input_line++;
1.21      otto     1089:                        if (ret == NULL)
1.17      deraadt  1090:                                fatal("unexpected end of file in patch at line %ld\n",
                   1091:                                    p_input_line);
                   1092:                        if (*buf != '-')
                   1093:                                fatal("--- expected at line %ld of patch\n",
                   1094:                                    p_input_line);
1.1       deraadt  1095:                }
1.17      deraadt  1096:                snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max);
                   1097:                p_line[i] = savestr(buf);
1.1       deraadt  1098:                if (out_of_mem) {
1.17      deraadt  1099:                        p_end = i - 1;
1.28      otto     1100:                        return false;
1.1       deraadt  1101:                }
1.17      deraadt  1102:                p_char[i] = '=';
                   1103:                for (i++; i <= p_end; i++) {
                   1104:                        ret = pgets(buf, sizeof buf, pfp);
                   1105:                        p_input_line++;
1.21      otto     1106:                        if (ret == NULL)
1.17      deraadt  1107:                                fatal("unexpected end of file in patch at line %ld\n",
                   1108:                                    p_input_line);
                   1109:                        if (*buf != '>')
                   1110:                                fatal("> expected at line %ld of patch\n",
                   1111:                                    p_input_line);
                   1112:                        p_line[i] = savestr(buf + 2);
                   1113:                        if (out_of_mem) {
                   1114:                                p_end = i - 1;
1.28      otto     1115:                                return false;
1.14      otto     1116:                        }
1.17      deraadt  1117:                        p_len[i] = strlen(p_line[i]);
                   1118:                        p_char[i] = '+';
1.14      otto     1119:                }
1.17      deraadt  1120:
                   1121:                if (remove_special_line()) {
                   1122:                        p_len[i - 1] -= 1;
                   1123:                        (p_line[i - 1])[p_len[i - 1]] = 0;
1.14      otto     1124:                }
1.1       deraadt  1125:        }
1.17      deraadt  1126:        if (reverse)            /* backwards patch? */
                   1127:                if (!pch_swap())
                   1128:                        say("Not enough memory to swap next hunk!\n");
1.1       deraadt  1129: #ifdef DEBUGGING
1.17      deraadt  1130:        if (debug & 2) {
                   1131:                int     i;
                   1132:                char    special;
                   1133:
                   1134:                for (i = 0; i <= p_end; i++) {
                   1135:                        if (i == p_ptrn_lines)
                   1136:                                special = '^';
                   1137:                        else
                   1138:                                special = ' ';
                   1139:                        fprintf(stderr, "%3d %c %c %s", i, p_char[i],
                   1140:                            special, p_line[i]);
                   1141:                        fflush(stderr);
                   1142:                }
1.1       deraadt  1143:        }
                   1144: #endif
1.17      deraadt  1145:        if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
                   1146:                p_char[p_end + 1] = '^';        /* add a stopper for apply_hunk */
1.28      otto     1147:        return true;
1.1       deraadt  1148: }
                   1149:
1.17      deraadt  1150: /*
                   1151:  * Input a line from the patch file, worrying about indentation.
                   1152:  */
1.21      otto     1153: static char *
1.17      deraadt  1154: pgets(char *bf, int sz, FILE *fp)
                   1155: {
                   1156:        char    *s, *ret = fgets(bf, sz, fp);
                   1157:        int     indent = 0;
1.1       deraadt  1158:
1.21      otto     1159:        if (p_indent && ret != NULL) {
1.17      deraadt  1160:                for (s = buf;
                   1161:                    indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
                   1162:                    s++) {
                   1163:                        if (*s == '\t')
                   1164:                                indent += 8 - (indent % 7);
                   1165:                        else
                   1166:                                indent++;
                   1167:                }
                   1168:                if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
                   1169:                        fatal("buffer too small in pgets()\n");
                   1170:        }
                   1171:        return ret;
1.1       deraadt  1172: }
                   1173:
1.17      deraadt  1174: /*
                   1175:  * Reverse the old and new portions of the current hunk.
                   1176:  */
1.1       deraadt  1177: bool
1.17      deraadt  1178: pch_swap(void)
1.1       deraadt  1179: {
1.17      deraadt  1180:        char    **tp_line;      /* the text of the hunk */
                   1181:        short   *tp_len;        /* length of each line */
                   1182:        char    *tp_char;       /* +, -, and ! */
                   1183:        LINENUM i;
                   1184:        LINENUM n;
1.28      otto     1185:        bool    blankline = false;
1.17      deraadt  1186:        char    *s;
                   1187:
                   1188:        i = p_first;
                   1189:        p_first = p_newfirst;
                   1190:        p_newfirst = i;
                   1191:
                   1192:        /* make a scratch copy */
                   1193:
                   1194:        tp_line = p_line;
                   1195:        tp_len = p_len;
                   1196:        tp_char = p_char;
1.21      otto     1197:        p_line = NULL;  /* force set_hunkmax to allocate again */
                   1198:        p_len = NULL;
                   1199:        p_char = NULL;
1.17      deraadt  1200:        set_hunkmax();
1.28      otto     1201:        if (p_line == NULL || p_len == NULL || p_char == NULL) {
1.21      otto     1202:
1.28      otto     1203:                free(p_line);
1.17      deraadt  1204:                p_line = tp_line;
1.28      otto     1205:                free(p_len);
1.17      deraadt  1206:                p_len = tp_len;
1.28      otto     1207:                free(p_char);
1.17      deraadt  1208:                p_char = tp_char;
1.28      otto     1209:                return false;   /* not enough memory to swap hunk! */
1.17      deraadt  1210:        }
                   1211:        /* now turn the new into the old */
                   1212:
1.1       deraadt  1213:        i = p_ptrn_lines + 1;
1.17      deraadt  1214:        if (tp_char[i] == '\n') {       /* account for possible blank line */
1.28      otto     1215:                blankline = true;
1.17      deraadt  1216:                i++;
                   1217:        }
                   1218:        if (p_efake >= 0) {     /* fix non-freeable ptr range */
                   1219:                if (p_efake <= i)
                   1220:                        n = p_end - i + 1;
                   1221:                else
                   1222:                        n = -i;
                   1223:                p_efake += n;
                   1224:                p_bfake += n;
                   1225:        }
                   1226:        for (n = 0; i <= p_end; i++, n++) {
                   1227:                p_line[n] = tp_line[i];
                   1228:                p_char[n] = tp_char[i];
                   1229:                if (p_char[n] == '+')
                   1230:                        p_char[n] = '-';
                   1231:                p_len[n] = tp_len[i];
                   1232:        }
                   1233:        if (blankline) {
                   1234:                i = p_ptrn_lines + 1;
                   1235:                p_line[n] = tp_line[i];
                   1236:                p_char[n] = tp_char[i];
                   1237:                p_len[n] = tp_len[i];
                   1238:                n++;
                   1239:        }
1.28      otto     1240:        if (p_char[0] != '=')
1.29      deraadt  1241:                fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1.28      otto     1242:                    p_input_line, p_char[0]);
1.17      deraadt  1243:        p_char[0] = '*';
                   1244:        for (s = p_line[0]; *s; s++)
                   1245:                if (*s == '-')
                   1246:                        *s = '*';
                   1247:
                   1248:        /* now turn the old into the new */
                   1249:
1.28      otto     1250:        if (p_char[0] != '*')
1.29      deraadt  1251:                fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
                   1252:                    p_input_line, p_char[0]);
1.17      deraadt  1253:        tp_char[0] = '=';
                   1254:        for (s = tp_line[0]; *s; s++)
                   1255:                if (*s == '*')
                   1256:                        *s = '-';
                   1257:        for (i = 0; n <= p_end; i++, n++) {
                   1258:                p_line[n] = tp_line[i];
                   1259:                p_char[n] = tp_char[i];
                   1260:                if (p_char[n] == '-')
                   1261:                        p_char[n] = '+';
                   1262:                p_len[n] = tp_len[i];
                   1263:        }
1.28      otto     1264:
                   1265:        if (i != p_ptrn_lines + 1)
                   1266:                fatal("Malformed patch at line %ld: expected %ld lines, "
1.29      deraadt  1267:                    "got %ld\n",
                   1268:                    p_input_line, p_ptrn_lines + 1, i);
1.28      otto     1269:
1.17      deraadt  1270:        i = p_ptrn_lines;
                   1271:        p_ptrn_lines = p_repl_lines;
                   1272:        p_repl_lines = i;
1.21      otto     1273:
1.28      otto     1274:        free(tp_line);
                   1275:        free(tp_len);
                   1276:        free(tp_char);
                   1277:
                   1278:        return true;
1.1       deraadt  1279: }
                   1280:
1.17      deraadt  1281: /*
                   1282:  * Return the specified line position in the old file of the old context.
                   1283:  */
1.1       deraadt  1284: LINENUM
1.17      deraadt  1285: pch_first(void)
1.1       deraadt  1286: {
1.17      deraadt  1287:        return p_first;
1.1       deraadt  1288: }
                   1289:
1.17      deraadt  1290: /*
                   1291:  * Return the number of lines of old context.
                   1292:  */
1.1       deraadt  1293: LINENUM
1.17      deraadt  1294: pch_ptrn_lines(void)
1.1       deraadt  1295: {
1.17      deraadt  1296:        return p_ptrn_lines;
1.1       deraadt  1297: }
                   1298:
1.17      deraadt  1299: /*
                   1300:  * Return the probable line position in the new file of the first line.
                   1301:  */
1.1       deraadt  1302: LINENUM
1.17      deraadt  1303: pch_newfirst(void)
1.1       deraadt  1304: {
1.17      deraadt  1305:        return p_newfirst;
1.1       deraadt  1306: }
                   1307:
1.17      deraadt  1308: /*
                   1309:  * Return the number of lines in the replacement text including context.
                   1310:  */
1.1       deraadt  1311: LINENUM
1.17      deraadt  1312: pch_repl_lines(void)
1.1       deraadt  1313: {
1.17      deraadt  1314:        return p_repl_lines;
1.1       deraadt  1315: }
                   1316:
1.17      deraadt  1317: /*
                   1318:  * Return the number of lines in the whole hunk.
                   1319:  */
1.1       deraadt  1320: LINENUM
1.17      deraadt  1321: pch_end(void)
1.1       deraadt  1322: {
1.17      deraadt  1323:        return p_end;
1.1       deraadt  1324: }
                   1325:
1.17      deraadt  1326: /*
                   1327:  * Return the number of context lines before the first changed line.
                   1328:  */
1.1       deraadt  1329: LINENUM
1.17      deraadt  1330: pch_context(void)
1.1       deraadt  1331: {
1.17      deraadt  1332:        return p_context;
1.1       deraadt  1333: }
                   1334:
1.17      deraadt  1335: /*
                   1336:  * Return the length of a particular patch line.
                   1337:  */
1.1       deraadt  1338: short
1.17      deraadt  1339: pch_line_len(LINENUM line)
1.1       deraadt  1340: {
1.17      deraadt  1341:        return p_len[line];
1.1       deraadt  1342: }
                   1343:
1.17      deraadt  1344: /*
                   1345:  * Return the control character (+, -, *, !, etc) for a patch line.
                   1346:  */
1.1       deraadt  1347: char
1.17      deraadt  1348: pch_char(LINENUM line)
1.1       deraadt  1349: {
1.17      deraadt  1350:        return p_char[line];
1.1       deraadt  1351: }
                   1352:
1.17      deraadt  1353: /*
                   1354:  * Return a pointer to a particular patch line.
                   1355:  */
1.1       deraadt  1356: char *
1.17      deraadt  1357: pfetch(LINENUM line)
1.1       deraadt  1358: {
1.17      deraadt  1359:        return p_line[line];
1.1       deraadt  1360: }
                   1361:
1.17      deraadt  1362: /*
                   1363:  * Return where in the patch file this hunk began, for error messages.
                   1364:  */
1.1       deraadt  1365: LINENUM
1.17      deraadt  1366: pch_hunk_beg(void)
1.1       deraadt  1367: {
1.17      deraadt  1368:        return p_hunk_beg;
1.1       deraadt  1369: }
                   1370:
1.17      deraadt  1371: /*
                   1372:  * Apply an ed script by feeding ed itself.
                   1373:  */
1.1       deraadt  1374: void
1.17      deraadt  1375: do_ed_script(void)
1.1       deraadt  1376: {
1.17      deraadt  1377:        char    *t;
1.42      tobias   1378:        off_t   beginning_of_this_line;
1.36      otto     1379:        FILE    *pipefp = NULL;
1.17      deraadt  1380:
                   1381:        if (!skip_rest_of_patch) {
1.24      millert  1382:                if (copy_file(filearg[0], TMPOUTNAME) < 0) {
                   1383:                        unlink(TMPOUTNAME);
                   1384:                        fatal("can't create temp file %s", TMPOUTNAME);
                   1385:                }
1.27      millert  1386:                snprintf(buf, sizeof buf, "%s%s%s", _PATH_ED,
                   1387:                    verbose ? " " : " -s ", TMPOUTNAME);
1.17      deraadt  1388:                pipefp = popen(buf, "w");
                   1389:        }
                   1390:        for (;;) {
1.42      tobias   1391:                beginning_of_this_line = ftello(pfp);
1.21      otto     1392:                if (pgets(buf, sizeof buf, pfp) == NULL) {
1.17      deraadt  1393:                        next_intuit_at(beginning_of_this_line, p_input_line);
                   1394:                        break;
                   1395:                }
                   1396:                p_input_line++;
1.41      deraadt  1397:                for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++)
1.18      deraadt  1398:                        ;
1.22      millert  1399:                /* POSIX defines allowed commands as {a,c,d,i,s} */
1.41      deraadt  1400:                if (isdigit((unsigned char)*buf) &&
                   1401:                    (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) {
1.28      otto     1402:                        if (pipefp != NULL)
1.17      deraadt  1403:                                fputs(buf, pipefp);
                   1404:                        if (*t != 'd') {
1.21      otto     1405:                                while (pgets(buf, sizeof buf, pfp) != NULL) {
1.17      deraadt  1406:                                        p_input_line++;
1.28      otto     1407:                                        if (pipefp != NULL)
1.17      deraadt  1408:                                                fputs(buf, pipefp);
                   1409:                                        if (strEQ(buf, ".\n"))
                   1410:                                                break;
                   1411:                                }
                   1412:                        }
                   1413:                } else {
                   1414:                        next_intuit_at(beginning_of_this_line, p_input_line);
1.1       deraadt  1415:                        break;
                   1416:                }
                   1417:        }
1.28      otto     1418:        if (pipefp == NULL)
1.17      deraadt  1419:                return;
                   1420:        fprintf(pipefp, "w\n");
                   1421:        fprintf(pipefp, "q\n");
                   1422:        fflush(pipefp);
                   1423:        pclose(pipefp);
                   1424:        ignore_signals();
                   1425:        if (!check_only) {
                   1426:                if (move_file(TMPOUTNAME, outname) < 0) {
1.28      otto     1427:                        toutkeep = true;
1.17      deraadt  1428:                        chmod(TMPOUTNAME, filemode);
                   1429:                } else
                   1430:                        chmod(outname, filemode);
1.1       deraadt  1431:        }
1.17      deraadt  1432:        set_signals(1);
1.32      millert  1433: }
                   1434:
                   1435: /*
                   1436:  * Choose the name of the file to be patched based on POSIX rules.
                   1437:  * NOTE: the POSIX rules are amazingly stupid and we only follow them
                   1438:  *       if the user specified --posix or set POSIXLY_CORRECT.
                   1439:  */
                   1440: static char *
                   1441: posix_name(const struct file_name *names, bool assume_exists)
                   1442: {
                   1443:        char *path = NULL;
                   1444:        int i;
                   1445:
                   1446:        /*
                   1447:         * POSIX states that the filename will be chosen from one
                   1448:         * of the old, new and index names (in that order) if
                   1449:         * the file exists relative to CWD after -p stripping.
                   1450:         */
                   1451:        for (i = 0; i < MAX_FILE; i++) {
                   1452:                if (names[i].path != NULL && names[i].exists) {
                   1453:                        path = names[i].path;
                   1454:                        break;
                   1455:                }
                   1456:        }
                   1457:        if (path == NULL && !assume_exists) {
                   1458:                /*
                   1459:                 * No files found, look for something we can checkout from
1.44      tobias   1460:                 * RCS dirs.  Same order as above.
1.32      millert  1461:                 */
                   1462:                for (i = 0; i < MAX_FILE; i++) {
                   1463:                        if (names[i].path != NULL &&
                   1464:                            (path = checked_in(names[i].path)) != NULL)
                   1465:                                break;
                   1466:                }
                   1467:                /*
                   1468:                 * Still no match?  Check to see if the diff could be creating
                   1469:                 * a new file.
                   1470:                 */
                   1471:                if (path == NULL && ok_to_create_file &&
                   1472:                    names[NEW_FILE].path != NULL)
                   1473:                        path = names[NEW_FILE].path;
                   1474:        }
                   1475:
                   1476:        return path ? savestr(path) : NULL;
                   1477: }
                   1478:
                   1479: static char *
1.40      otto     1480: compare_names(const struct file_name *names, bool assume_exists, int phase)
1.32      millert  1481: {
                   1482:        size_t min_components, min_baselen, min_len, tmp;
                   1483:        char *best = NULL;
1.40      otto     1484:        char *path;
1.32      millert  1485:        int i;
                   1486:
                   1487:        /*
                   1488:         * The "best" name is the one with the fewest number of path
                   1489:         * components, the shortest basename length, and the shortest
                   1490:         * overall length (in that order).  We only use the Index: file
                   1491:         * if neither of the old or new files could be intuited from
                   1492:         * the diff header.
                   1493:         */
                   1494:        min_components = min_baselen = min_len = SIZE_MAX;
                   1495:        for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1.40      otto     1496:                path = names[i].path;
                   1497:                if (path == NULL ||
                   1498:                    (phase == 1 && !names[i].exists && !assume_exists) ||
                   1499:                    (phase == 2 && checked_in(path) == NULL))
1.32      millert  1500:                        continue;
1.40      otto     1501:                if ((tmp = num_components(path)) > min_components)
1.32      millert  1502:                        continue;
1.40      otto     1503:                if (tmp < min_components) {
                   1504:                        min_components = tmp;
                   1505:                        best = path;
                   1506:                }
                   1507:                if ((tmp = strlen(basename(path))) > min_baselen)
1.32      millert  1508:                        continue;
1.40      otto     1509:                if (tmp < min_baselen) {
                   1510:                        min_baselen = tmp;
                   1511:                        best = path;
                   1512:                }
                   1513:                if ((tmp = strlen(path)) > min_len)
1.32      millert  1514:                        continue;
                   1515:                min_len = tmp;
1.40      otto     1516:                best = path;
1.32      millert  1517:        }
1.40      otto     1518:        return best;
                   1519: }
                   1520:
                   1521: /*
                   1522:  * Choose the name of the file to be patched based the "best" one
                   1523:  * available.
                   1524:  */
                   1525: static char *
                   1526: best_name(const struct file_name *names, bool assume_exists)
                   1527: {
                   1528:        char *best;
                   1529:
                   1530:        best = compare_names(names, assume_exists, 1);
1.32      millert  1531:        if (best == NULL) {
1.40      otto     1532:                best = compare_names(names, assume_exists, 2);
1.32      millert  1533:                /*
                   1534:                 * Still no match?  Check to see if the diff could be creating
                   1535:                 * a new file.
                   1536:                 */
                   1537:                if (best == NULL && ok_to_create_file &&
                   1538:                    names[NEW_FILE].path != NULL)
                   1539:                        best = names[NEW_FILE].path;
                   1540:        }
                   1541:        return best ? savestr(best) : NULL;
                   1542: }
                   1543:
                   1544: static size_t
                   1545: num_components(const char *path)
                   1546: {
                   1547:        size_t n;
                   1548:        const char *cp;
                   1549:
                   1550:        for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) {
                   1551:                while (*cp == '/')
                   1552:                        cp++;           /* skip consecutive slashes */
                   1553:        }
                   1554:        return n;
1.45      tobias   1555: }
                   1556:
                   1557: /*
                   1558:  * Convert number at NPTR into LINENUM and save address of first
                   1559:  * character that is not a digit in ENDPTR.  If conversion is not
                   1560:  * possible, call fatal.
                   1561:  */
                   1562: static LINENUM
                   1563: strtolinenum(char *nptr, char **endptr)
                   1564: {
                   1565:        LINENUM rv;
                   1566:        char c;
                   1567:        char *p;
                   1568:        const char *errstr;
                   1569:
                   1570:        for (p = nptr; isdigit((unsigned char)*p); p++)
                   1571:                ;
                   1572:
                   1573:        if (p == nptr)
                   1574:                malformed();
                   1575:
                   1576:        c = *p;
                   1577:        *p = '\0';
                   1578:
                   1579:        rv = strtonum(nptr, 0, LINENUM_MAX, &errstr);
                   1580:        if (errstr != NULL)
                   1581:                fatal("invalid line number at line %ld: `%s' is %s\n",
                   1582:                    p_input_line, nptr, errstr);
                   1583:
                   1584:        *p = c;
                   1585:        *endptr = p;
                   1586:
                   1587:        return rv;
1.1       deraadt  1588: }