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

Annotation of src/usr.bin/patch/inp.c, Revision 1.41

1.41    ! tobias      1: /*     $OpenBSD: inp.c,v 1.40 2014/11/22 15:49:28 tobias Exp $ */
1.28      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.16      otto       29: #include <sys/types.h>
                     30: #include <sys/file.h>
                     31: #include <sys/stat.h>
1.23      otto       32: #include <sys/mman.h>
1.16      otto       33:
                     34: #include <ctype.h>
                     35: #include <libgen.h>
                     36: #include <limits.h>
1.23      otto       37: #include <stddef.h>
1.19      otto       38: #include <stdio.h>
1.16      otto       39: #include <stdlib.h>
                     40: #include <string.h>
                     41: #include <unistd.h>
                     42:
1.1       deraadt    43: #include "common.h"
                     44: #include "util.h"
                     45: #include "pch.h"
                     46: #include "inp.h"
                     47:
1.7       espie      48:
1.1       deraadt    49: /* Input-file-with-indexable-lines abstract type */
                     50:
1.12      deraadt    51: static off_t   i_size;         /* size of the input file */
                     52: static char    *i_womp;        /* plan a buffer for entire file */
                     53: static char    **i_ptr;        /* pointers to lines in i_womp */
                     54:
                     55: static int     tifd = -1;      /* plan b virtual string array */
                     56: static char    *tibuf[2];      /* plan b buffers */
                     57: static LINENUM tiline[2] = {-1, -1};   /* 1st line in each buffer */
                     58: static LINENUM lines_per_buf;  /* how many lines per buffer */
                     59: static int     tireclen;       /* length of records in tmp file */
1.1       deraadt    60:
1.19      otto       61: static bool    rev_in_string(const char *);
1.25      otto       62: static bool    reallocate_lines(size_t *);
1.19      otto       63:
                     64: /* returns false if insufficient memory */
                     65: static bool    plan_a(const char *);
                     66:
                     67: static void    plan_b(const char *);
1.11      deraadt    68:
1.1       deraadt    69: /* New patch--prepare to edit another file. */
                     70:
                     71: void
1.11      deraadt    72: re_input(void)
1.1       deraadt    73: {
1.12      deraadt    74:        if (using_plan_a) {
1.16      otto       75:                free(i_ptr);
                     76:                i_ptr = NULL;
1.23      otto       77:                if (i_womp != NULL) {
                     78:                        munmap(i_womp, i_size);
                     79:                        i_womp = NULL;
                     80:                }
1.39      tobias     81:                i_size = 0;
1.12      deraadt    82:        } else {
1.22      otto       83:                using_plan_a = true;    /* maybe the next one is smaller */
1.12      deraadt    84:                close(tifd);
                     85:                tifd = -1;
                     86:                free(tibuf[0]);
                     87:                free(tibuf[1]);
1.16      otto       88:                tibuf[0] = tibuf[1] = NULL;
1.12      deraadt    89:                tiline[0] = tiline[1] = -1;
                     90:                tireclen = 0;
                     91:        }
1.1       deraadt    92: }
                     93:
1.33      miod       94: /* Construct the line index, somehow or other. */
1.1       deraadt    95:
                     96: void
1.19      otto       97: scan_input(const char *filename)
1.1       deraadt    98: {
1.12      deraadt    99:        if (!plan_a(filename))
                    100:                plan_b(filename);
                    101:        if (verbose) {
                    102:                say("Patching file %s using Plan %s...\n", filename,
                    103:                    (using_plan_a ? "A" : "B"));
                    104:        }
1.1       deraadt   105: }
                    106:
1.25      otto      107: static bool
                    108: reallocate_lines(size_t *lines_allocated)
                    109: {
1.29      otto      110:        char    **p;
                    111:        size_t  new_size;
1.25      otto      112:
1.29      otto      113:        new_size = *lines_allocated * 3 / 2;
1.38      doug      114:        p = reallocarray(i_ptr, new_size + 2, sizeof(char *));
1.25      otto      115:        if (p == NULL) {        /* shucks, it was a near thing */
                    116:                munmap(i_womp, i_size);
                    117:                i_womp = NULL;
                    118:                free(i_ptr);
                    119:                i_ptr = NULL;
1.29      otto      120:                *lines_allocated = 0;
1.25      otto      121:                return false;
                    122:        }
1.29      otto      123:        *lines_allocated = new_size;
1.25      otto      124:        i_ptr = p;
                    125:        return true;
                    126: }
                    127:
1.1       deraadt   128: /* Try keeping everything in memory. */
                    129:
1.16      otto      130: static bool
1.19      otto      131: plan_a(const char *filename)
1.1       deraadt   132: {
1.19      otto      133:        int             ifd, statfailed;
1.23      otto      134:        char            *p, *s, lbuf[MAXLINELEN];
1.20      deraadt   135:        struct stat     filestat;
1.23      otto      136:        off_t           i;
                    137:        ptrdiff_t       sz;
1.34      otto      138:        size_t          iline, lines_allocated;
1.23      otto      139:
                    140: #ifdef DEBUGGING
                    141:        if (debug & 8)
                    142:                return false;
                    143: #endif
1.1       deraadt   144:
1.19      otto      145:        if (filename == NULL || *filename == '\0')
1.22      otto      146:                return false;
1.8       millert   147:
1.1       deraadt   148:        statfailed = stat(filename, &filestat);
1.12      deraadt   149:        if (statfailed && ok_to_create_file) {
1.1       deraadt   150:                if (verbose)
1.12      deraadt   151:                        say("(Creating file %s...)\n", filename);
                    152:
                    153:                /*
                    154:                 * in check_patch case, we still display `Creating file' even
                    155:                 * though we're not. The rule is that -C should be as similar
                    156:                 * to normal patch behavior as possible
                    157:                 */
                    158:                if (check_only)
1.22      otto      159:                        return true;
                    160:                makedirs(filename, true);
1.12      deraadt   161:                close(creat(filename, 0666));
                    162:                statfailed = stat(filename, &filestat);
                    163:        }
                    164:        if (statfailed && check_only)
                    165:                fatal("%s not found, -C mode, can't probe further\n", filename);
1.40      tobias    166:        /* For nonexistent or read-only files, look for RCS versions.  */
1.12      deraadt   167:        if (statfailed ||
                    168:            /* No one can write to it.  */
                    169:            (filestat.st_mode & 0222) == 0 ||
                    170:            /* I can't write to it.  */
1.19      otto      171:            ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
1.16      otto      172:                char    *cs = NULL, *filebase, *filedir;
1.12      deraadt   173:                struct stat     cstat;
                    174:
                    175:                filebase = basename(filename);
                    176:                filedir = dirname(filename);
                    177:
                    178:                /* Leave room in lbuf for the diff command.  */
                    179:                s = lbuf + 20;
                    180:
                    181: #define try(f, a1, a2, a3) \
                    182:        (snprintf(s, sizeof lbuf - 20, f, a1, a2, a3), stat(s, &cstat) == 0)
                    183:
                    184:                if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
                    185:                    try("%s/RCS/%s%s", filedir, filebase, "") ||
                    186:                    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
                    187:                        snprintf(buf, sizeof buf, CHECKOUT, filename);
                    188:                        snprintf(lbuf, sizeof lbuf, RCSDIFF, filename);
                    189:                        cs = "RCS";
                    190:                } else if (statfailed)
                    191:                        fatal("can't find %s\n", filename);
                    192:                /*
                    193:                 * else we can't write to it but it's not under a version
                    194:                 * control system, so just proceed.
                    195:                 */
                    196:                if (cs) {
                    197:                        if (!statfailed) {
                    198:                                if ((filestat.st_mode & 0222) != 0)
                    199:                                        /* The owner can write to it.  */
                    200:                                        fatal("file %s seems to be locked "
                    201:                                            "by somebody else under %s\n",
                    202:                                            filename, cs);
                    203:                                /*
                    204:                                 * It might be checked out unlocked.  See if
                    205:                                 * it's safe to check out the default version
                    206:                                 * locked.
                    207:                                 */
                    208:                                if (verbose)
                    209:                                        say("Comparing file %s to default "
                    210:                                            "%s version...\n",
                    211:                                            filename, cs);
                    212:                                if (system(lbuf))
                    213:                                        fatal("can't check out file %s: "
                    214:                                            "differs from default %s version\n",
                    215:                                            filename, cs);
                    216:                        }
                    217:                        if (verbose)
                    218:                                say("Checking out file %s from %s...\n",
                    219:                                    filename, cs);
                    220:                        if (system(buf) || stat(filename, &filestat))
                    221:                                fatal("can't check out file %s from %s\n",
                    222:                                    filename, cs);
                    223:                }
                    224:        }
                    225:        filemode = filestat.st_mode;
                    226:        if (!S_ISREG(filemode))
                    227:                fatal("%s is not a normal file--can't patch\n", filename);
                    228:        i_size = filestat.st_size;
                    229:        if (out_of_mem) {
                    230:                set_hunkmax();  /* make sure dynamic arrays are allocated */
1.22      otto      231:                out_of_mem = false;
                    232:                return false;   /* force plan b because plan a bombed */
1.12      deraadt   233:        }
1.23      otto      234:        if (i_size > SIZE_MAX) {
                    235:                say("block too large to mmap\n");
1.24      deraadt   236:                return false;
1.23      otto      237:        }
1.12      deraadt   238:        if ((ifd = open(filename, O_RDONLY)) < 0)
                    239:                pfatal("can't open file %s", filename);
1.16      otto      240:
1.36      ajacouto  241:        if (i_size) {
                    242:                i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
                    243:                if (i_womp == MAP_FAILED) {
                    244:                        perror("mmap failed");
                    245:                        i_womp = NULL;
                    246:                        close(ifd);
                    247:                        return false;
                    248:                }
                    249:        } else {
1.23      otto      250:                i_womp = NULL;
1.12      deraadt   251:        }
1.16      otto      252:
1.12      deraadt   253:        close(ifd);
1.30      mickey    254:        if (i_size)
                    255:                madvise(i_womp, i_size, MADV_SEQUENTIAL);
1.12      deraadt   256:
1.25      otto      257:        /* estimate the number of lines */
                    258:        lines_allocated = i_size / 25;
                    259:        if (lines_allocated < 100)
                    260:                lines_allocated = 100;
1.12      deraadt   261:
1.32      deraadt   262:        if (!reallocate_lines(&lines_allocated))
1.24      deraadt   263:                return false;
1.23      otto      264:
1.12      deraadt   265:        /* now scan the buffer and build pointer array */
                    266:        iline = 1;
                    267:        i_ptr[iline] = i_womp;
1.23      otto      268:        /* test for NUL too, to maintain the behavior of the original code */
                    269:        for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
1.25      otto      270:                if (*s == '\n') {
                    271:                        if (iline == lines_allocated) {
                    272:                                if (!reallocate_lines(&lines_allocated))
                    273:                                        return false;
                    274:                        }
                    275:                        /* these are NOT NUL terminated */
                    276:                        i_ptr[++iline] = s + 1;
                    277:                }
1.12      deraadt   278:        }
1.23      otto      279:        /* if the last line contains no EOL, append one */
                    280:        if (i_size > 0 && i_womp[i_size - 1] != '\n') {
1.26      otto      281:                last_line_missing_eol = true;
1.23      otto      282:                /* fix last line */
                    283:                sz = s - i_ptr[iline];
                    284:                p = malloc(sz + 1);
                    285:                if (p == NULL) {
                    286:                        free(i_ptr);
                    287:                        i_ptr = NULL;
                    288:                        munmap(i_womp, i_size);
                    289:                        i_womp = NULL;
                    290:                        return false;
                    291:                }
1.24      deraadt   292:
1.23      otto      293:                memcpy(p, i_ptr[iline], sz);
                    294:                p[sz] = '\n';
                    295:                i_ptr[iline] = p;
                    296:                /* count the extra line and make it point to some valid mem */
                    297:                i_ptr[++iline] = "";
1.26      otto      298:        } else
                    299:                last_line_missing_eol = false;
1.23      otto      300:
1.12      deraadt   301:        input_lines = iline - 1;
1.11      deraadt   302:
1.12      deraadt   303:        /* now check for revision, if any */
1.1       deraadt   304:
1.16      otto      305:        if (revision != NULL) {
1.41    ! tobias    306:                if (i_womp == NULL || !rev_in_string(i_womp)) {
1.12      deraadt   307:                        if (force) {
                    308:                                if (verbose)
                    309:                                        say("Warning: this file doesn't appear "
                    310:                                            "to be the %s version--patching anyway.\n",
                    311:                                            revision);
                    312:                        } else if (batch) {
                    313:                                fatal("this file doesn't appear to be the "
                    314:                                    "%s version--aborting.\n",
                    315:                                    revision);
                    316:                        } else {
                    317:                                ask("This file doesn't appear to be the "
                    318:                                    "%s version--patch anyway? [n] ",
                    319:                                    revision);
                    320:                                if (*buf != 'y')
                    321:                                        fatal("aborted\n");
                    322:                        }
                    323:                } else if (verbose)
                    324:                        say("Good.  This file appears to be the %s version.\n",
                    325:                            revision);
                    326:        }
1.22      otto      327:        return true;            /* plan a will work */
1.1       deraadt   328: }
                    329:
                    330: /* Keep (virtually) nothing in memory. */
                    331:
1.16      otto      332: static void
1.19      otto      333: plan_b(const char *filename)
1.1       deraadt   334: {
1.12      deraadt   335:        FILE    *ifp;
1.34      otto      336:        size_t  i = 0, j, maxlen = 1;
1.27      otto      337:        char    *p;
1.16      otto      338:        bool    found_revision = (revision == NULL);
1.12      deraadt   339:
1.22      otto      340:        using_plan_a = false;
1.16      otto      341:        if ((ifp = fopen(filename, "r")) == NULL)
1.12      deraadt   342:                pfatal("can't open file %s", filename);
                    343:        (void) unlink(TMPINNAME);
                    344:        if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
                    345:                pfatal("can't open file %s", TMPINNAME);
1.16      otto      346:        while (fgets(buf, sizeof buf, ifp) != NULL) {
                    347:                if (revision != NULL && !found_revision && rev_in_string(buf))
1.22      otto      348:                        found_revision = true;
1.12      deraadt   349:                if ((i = strlen(buf)) > maxlen)
                    350:                        maxlen = i;     /* find longest line */
                    351:        }
1.27      otto      352:        last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
                    353:        if (last_line_missing_eol && maxlen == i)
                    354:                maxlen++;
1.32      deraadt   355:
1.16      otto      356:        if (revision != NULL) {
1.12      deraadt   357:                if (!found_revision) {
                    358:                        if (force) {
                    359:                                if (verbose)
                    360:                                        say("Warning: this file doesn't appear "
                    361:                                            "to be the %s version--patching anyway.\n",
                    362:                                            revision);
                    363:                        } else if (batch) {
                    364:                                fatal("this file doesn't appear to be the "
                    365:                                    "%s version--aborting.\n",
                    366:                                    revision);
                    367:                        } else {
                    368:                                ask("This file doesn't appear to be the %s "
                    369:                                    "version--patch anyway? [n] ",
                    370:                                    revision);
                    371:                                if (*buf != 'y')
                    372:                                        fatal("aborted\n");
                    373:                        }
                    374:                } else if (verbose)
                    375:                        say("Good.  This file appears to be the %s version.\n",
                    376:                            revision);
                    377:        }
1.16      otto      378:        fseek(ifp, 0L, SEEK_SET);       /* rewind file */
1.12      deraadt   379:        lines_per_buf = BUFFERSIZE / maxlen;
                    380:        tireclen = maxlen;
1.13      deraadt   381:        tibuf[0] = malloc(BUFFERSIZE + 1);
1.16      otto      382:        if (tibuf[0] == NULL)
1.12      deraadt   383:                fatal("out of memory\n");
1.13      deraadt   384:        tibuf[1] = malloc(BUFFERSIZE + 1);
1.16      otto      385:        if (tibuf[1] == NULL)
1.12      deraadt   386:                fatal("out of memory\n");
                    387:        for (i = 1;; i++) {
1.27      otto      388:                p = tibuf[0] + maxlen * (i % lines_per_buf);
                    389:                if (i % lines_per_buf == 0)     /* new block */
1.12      deraadt   390:                        if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    391:                                pfatal("can't write temp file");
1.27      otto      392:                if (fgets(p, maxlen + 1, ifp) == NULL) {
1.12      deraadt   393:                        input_lines = i - 1;
1.27      otto      394:                        if (i % lines_per_buf != 0)
1.12      deraadt   395:                                if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    396:                                        pfatal("can't write temp file");
                    397:                        break;
                    398:                }
1.27      otto      399:                j = strlen(p);
                    400:                /* These are '\n' terminated strings, so no need to add a NUL */
                    401:                if (j == 0 || p[j - 1] != '\n')
                    402:                        p[j] = '\n';
1.12      deraadt   403:        }
                    404:        fclose(ifp);
                    405:        close(tifd);
                    406:        if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
                    407:                pfatal("can't reopen file %s", TMPINNAME);
1.1       deraadt   408: }
                    409:
1.12      deraadt   410: /*
                    411:  * Fetch a line from the input file, \n terminated, not necessarily \0.
                    412:  */
1.1       deraadt   413: char *
1.12      deraadt   414: ifetch(LINENUM line, int whichbuf)
1.1       deraadt   415: {
1.12      deraadt   416:        if (line < 1 || line > input_lines) {
1.21      otto      417:                if (warn_on_invalid_line) {
                    418:                        say("No such line %ld in input file, ignoring\n", line);
1.22      otto      419:                        warn_on_invalid_line = false;
1.21      otto      420:                }
1.18      otto      421:                return NULL;
1.12      deraadt   422:        }
                    423:        if (using_plan_a)
                    424:                return i_ptr[line];
1.1       deraadt   425:        else {
1.12      deraadt   426:                LINENUM offline = line % lines_per_buf;
                    427:                LINENUM baseline = line - offline;
                    428:
                    429:                if (tiline[0] == baseline)
                    430:                        whichbuf = 0;
                    431:                else if (tiline[1] == baseline)
                    432:                        whichbuf = 1;
                    433:                else {
                    434:                        tiline[whichbuf] = baseline;
1.16      otto      435:
1.34      otto      436:                        if (lseek(tifd, (off_t) (baseline / lines_per_buf *
                    437:                            BUFFERSIZE), SEEK_SET) < 0)
                    438:                                pfatal("cannot seek in the temporary input file");
1.16      otto      439:
1.12      deraadt   440:                        if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
                    441:                                pfatal("error reading tmp file %s", TMPINNAME);
                    442:                }
                    443:                return tibuf[whichbuf] + (tireclen * offline);
1.1       deraadt   444:        }
                    445: }
                    446:
1.12      deraadt   447: /*
                    448:  * True if the string argument contains the revision number we want.
                    449:  */
1.16      otto      450: static bool
1.19      otto      451: rev_in_string(const char *string)
1.1       deraadt   452: {
1.19      otto      453:        const char      *s;
1.34      otto      454:        size_t          patlen;
1.1       deraadt   455:
1.16      otto      456:        if (revision == NULL)
1.22      otto      457:                return true;
1.12      deraadt   458:        patlen = strlen(revision);
1.37      deraadt   459:        if (strnEQ(string, revision, patlen) &&
                    460:            isspace((unsigned char)string[patlen]))
1.22      otto      461:                return true;
1.12      deraadt   462:        for (s = string; *s; s++) {
1.37      deraadt   463:                if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
                    464:                    isspace((unsigned char)s[patlen + 1])) {
1.22      otto      465:                        return true;
1.12      deraadt   466:                }
1.1       deraadt   467:        }
1.22      otto      468:        return false;
1.1       deraadt   469: }