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

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