[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.49

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