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

1.37.6.1! guenther    1: /*     $OpenBSD: inp.c,v 1.37 2013/11/26 13:19:07 deraadt 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) {
                     75:                i_size = 0;
1.16      otto       76:                free(i_ptr);
                     77:                i_ptr = NULL;
1.23      otto       78:                if (i_womp != NULL) {
                     79:                        munmap(i_womp, i_size);
                     80:                        i_womp = NULL;
                     81:                }
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;
                    114:        p = realloc(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.37.6.1! guenther  134:        char            *p, *s;
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:        }
1.37.6.1! guenther  164:        if (statfailed)
        !           165:                fatal("can't find %s\n", filename);
1.12      deraadt   166:        filemode = filestat.st_mode;
                    167:        if (!S_ISREG(filemode))
                    168:                fatal("%s is not a normal file--can't patch\n", filename);
                    169:        i_size = filestat.st_size;
                    170:        if (out_of_mem) {
                    171:                set_hunkmax();  /* make sure dynamic arrays are allocated */
1.22      otto      172:                out_of_mem = false;
                    173:                return false;   /* force plan b because plan a bombed */
1.12      deraadt   174:        }
1.23      otto      175:        if (i_size > SIZE_MAX) {
                    176:                say("block too large to mmap\n");
1.24      deraadt   177:                return false;
1.23      otto      178:        }
1.12      deraadt   179:        if ((ifd = open(filename, O_RDONLY)) < 0)
                    180:                pfatal("can't open file %s", filename);
1.16      otto      181:
1.36      ajacouto  182:        if (i_size) {
                    183:                i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
                    184:                if (i_womp == MAP_FAILED) {
                    185:                        perror("mmap failed");
                    186:                        i_womp = NULL;
                    187:                        close(ifd);
                    188:                        return false;
                    189:                }
                    190:        } else {
1.23      otto      191:                i_womp = NULL;
1.12      deraadt   192:        }
1.16      otto      193:
1.12      deraadt   194:        close(ifd);
1.30      mickey    195:        if (i_size)
                    196:                madvise(i_womp, i_size, MADV_SEQUENTIAL);
1.12      deraadt   197:
1.25      otto      198:        /* estimate the number of lines */
                    199:        lines_allocated = i_size / 25;
                    200:        if (lines_allocated < 100)
                    201:                lines_allocated = 100;
1.12      deraadt   202:
1.32      deraadt   203:        if (!reallocate_lines(&lines_allocated))
1.24      deraadt   204:                return false;
1.23      otto      205:
1.12      deraadt   206:        /* now scan the buffer and build pointer array */
                    207:        iline = 1;
                    208:        i_ptr[iline] = i_womp;
1.23      otto      209:        /* test for NUL too, to maintain the behavior of the original code */
                    210:        for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
1.25      otto      211:                if (*s == '\n') {
                    212:                        if (iline == lines_allocated) {
                    213:                                if (!reallocate_lines(&lines_allocated))
                    214:                                        return false;
                    215:                        }
                    216:                        /* these are NOT NUL terminated */
                    217:                        i_ptr[++iline] = s + 1;
                    218:                }
1.12      deraadt   219:        }
1.23      otto      220:        /* if the last line contains no EOL, append one */
                    221:        if (i_size > 0 && i_womp[i_size - 1] != '\n') {
1.26      otto      222:                last_line_missing_eol = true;
1.23      otto      223:                /* fix last line */
                    224:                sz = s - i_ptr[iline];
                    225:                p = malloc(sz + 1);
                    226:                if (p == NULL) {
                    227:                        free(i_ptr);
                    228:                        i_ptr = NULL;
                    229:                        munmap(i_womp, i_size);
                    230:                        i_womp = NULL;
                    231:                        return false;
                    232:                }
1.24      deraadt   233:
1.23      otto      234:                memcpy(p, i_ptr[iline], sz);
                    235:                p[sz] = '\n';
                    236:                i_ptr[iline] = p;
                    237:                /* count the extra line and make it point to some valid mem */
                    238:                i_ptr[++iline] = "";
1.26      otto      239:        } else
                    240:                last_line_missing_eol = false;
1.23      otto      241:
1.12      deraadt   242:        input_lines = iline - 1;
1.11      deraadt   243:
1.12      deraadt   244:        /* now check for revision, if any */
1.1       deraadt   245:
1.16      otto      246:        if (revision != NULL) {
1.12      deraadt   247:                if (!rev_in_string(i_womp)) {
                    248:                        if (force) {
                    249:                                if (verbose)
                    250:                                        say("Warning: this file doesn't appear "
                    251:                                            "to be the %s version--patching anyway.\n",
                    252:                                            revision);
                    253:                        } else if (batch) {
                    254:                                fatal("this file doesn't appear to be the "
                    255:                                    "%s version--aborting.\n",
                    256:                                    revision);
                    257:                        } else {
                    258:                                ask("This file doesn't appear to be the "
                    259:                                    "%s version--patch anyway? [n] ",
                    260:                                    revision);
                    261:                                if (*buf != 'y')
                    262:                                        fatal("aborted\n");
                    263:                        }
                    264:                } else if (verbose)
                    265:                        say("Good.  This file appears to be the %s version.\n",
                    266:                            revision);
                    267:        }
1.22      otto      268:        return true;            /* plan a will work */
1.1       deraadt   269: }
                    270:
                    271: /* Keep (virtually) nothing in memory. */
                    272:
1.16      otto      273: static void
1.19      otto      274: plan_b(const char *filename)
1.1       deraadt   275: {
1.12      deraadt   276:        FILE    *ifp;
1.34      otto      277:        size_t  i = 0, j, maxlen = 1;
1.27      otto      278:        char    *p;
1.16      otto      279:        bool    found_revision = (revision == NULL);
1.12      deraadt   280:
1.22      otto      281:        using_plan_a = false;
1.16      otto      282:        if ((ifp = fopen(filename, "r")) == NULL)
1.12      deraadt   283:                pfatal("can't open file %s", filename);
                    284:        (void) unlink(TMPINNAME);
                    285:        if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
                    286:                pfatal("can't open file %s", TMPINNAME);
1.16      otto      287:        while (fgets(buf, sizeof buf, ifp) != NULL) {
                    288:                if (revision != NULL && !found_revision && rev_in_string(buf))
1.22      otto      289:                        found_revision = true;
1.12      deraadt   290:                if ((i = strlen(buf)) > maxlen)
                    291:                        maxlen = i;     /* find longest line */
                    292:        }
1.27      otto      293:        last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
                    294:        if (last_line_missing_eol && maxlen == i)
                    295:                maxlen++;
1.32      deraadt   296:
1.16      otto      297:        if (revision != NULL) {
1.12      deraadt   298:                if (!found_revision) {
                    299:                        if (force) {
                    300:                                if (verbose)
                    301:                                        say("Warning: this file doesn't appear "
                    302:                                            "to be the %s version--patching anyway.\n",
                    303:                                            revision);
                    304:                        } else if (batch) {
                    305:                                fatal("this file doesn't appear to be the "
                    306:                                    "%s version--aborting.\n",
                    307:                                    revision);
                    308:                        } else {
                    309:                                ask("This file doesn't appear to be the %s "
                    310:                                    "version--patch anyway? [n] ",
                    311:                                    revision);
                    312:                                if (*buf != 'y')
                    313:                                        fatal("aborted\n");
                    314:                        }
                    315:                } else if (verbose)
                    316:                        say("Good.  This file appears to be the %s version.\n",
                    317:                            revision);
                    318:        }
1.16      otto      319:        fseek(ifp, 0L, SEEK_SET);       /* rewind file */
1.12      deraadt   320:        lines_per_buf = BUFFERSIZE / maxlen;
                    321:        tireclen = maxlen;
1.13      deraadt   322:        tibuf[0] = malloc(BUFFERSIZE + 1);
1.16      otto      323:        if (tibuf[0] == NULL)
1.12      deraadt   324:                fatal("out of memory\n");
1.13      deraadt   325:        tibuf[1] = malloc(BUFFERSIZE + 1);
1.16      otto      326:        if (tibuf[1] == NULL)
1.12      deraadt   327:                fatal("out of memory\n");
                    328:        for (i = 1;; i++) {
1.27      otto      329:                p = tibuf[0] + maxlen * (i % lines_per_buf);
                    330:                if (i % lines_per_buf == 0)     /* new block */
1.12      deraadt   331:                        if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    332:                                pfatal("can't write temp file");
1.27      otto      333:                if (fgets(p, maxlen + 1, ifp) == NULL) {
1.12      deraadt   334:                        input_lines = i - 1;
1.27      otto      335:                        if (i % lines_per_buf != 0)
1.12      deraadt   336:                                if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    337:                                        pfatal("can't write temp file");
                    338:                        break;
                    339:                }
1.27      otto      340:                j = strlen(p);
                    341:                /* These are '\n' terminated strings, so no need to add a NUL */
                    342:                if (j == 0 || p[j - 1] != '\n')
                    343:                        p[j] = '\n';
1.12      deraadt   344:        }
                    345:        fclose(ifp);
                    346:        close(tifd);
                    347:        if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
                    348:                pfatal("can't reopen file %s", TMPINNAME);
1.1       deraadt   349: }
                    350:
1.12      deraadt   351: /*
                    352:  * Fetch a line from the input file, \n terminated, not necessarily \0.
                    353:  */
1.1       deraadt   354: char *
1.12      deraadt   355: ifetch(LINENUM line, int whichbuf)
1.1       deraadt   356: {
1.12      deraadt   357:        if (line < 1 || line > input_lines) {
1.21      otto      358:                if (warn_on_invalid_line) {
                    359:                        say("No such line %ld in input file, ignoring\n", line);
1.22      otto      360:                        warn_on_invalid_line = false;
1.21      otto      361:                }
1.18      otto      362:                return NULL;
1.12      deraadt   363:        }
                    364:        if (using_plan_a)
                    365:                return i_ptr[line];
1.1       deraadt   366:        else {
1.12      deraadt   367:                LINENUM offline = line % lines_per_buf;
                    368:                LINENUM baseline = line - offline;
                    369:
                    370:                if (tiline[0] == baseline)
                    371:                        whichbuf = 0;
                    372:                else if (tiline[1] == baseline)
                    373:                        whichbuf = 1;
                    374:                else {
                    375:                        tiline[whichbuf] = baseline;
1.16      otto      376:
1.34      otto      377:                        if (lseek(tifd, (off_t) (baseline / lines_per_buf *
                    378:                            BUFFERSIZE), SEEK_SET) < 0)
                    379:                                pfatal("cannot seek in the temporary input file");
1.16      otto      380:
1.12      deraadt   381:                        if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
                    382:                                pfatal("error reading tmp file %s", TMPINNAME);
                    383:                }
                    384:                return tibuf[whichbuf] + (tireclen * offline);
1.1       deraadt   385:        }
                    386: }
                    387:
1.12      deraadt   388: /*
                    389:  * True if the string argument contains the revision number we want.
                    390:  */
1.16      otto      391: static bool
1.19      otto      392: rev_in_string(const char *string)
1.1       deraadt   393: {
1.19      otto      394:        const char      *s;
1.34      otto      395:        size_t          patlen;
1.1       deraadt   396:
1.16      otto      397:        if (revision == NULL)
1.22      otto      398:                return true;
1.12      deraadt   399:        patlen = strlen(revision);
1.37      deraadt   400:        if (strnEQ(string, revision, patlen) &&
                    401:            isspace((unsigned char)string[patlen]))
1.22      otto      402:                return true;
1.12      deraadt   403:        for (s = string; *s; s++) {
1.37      deraadt   404:                if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
                    405:                    isspace((unsigned char)s[patlen + 1])) {
1.22      otto      406:                        return true;
1.12      deraadt   407:                }
1.1       deraadt   408:        }
1.22      otto      409:        return false;
1.1       deraadt   410: }