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

1.17    ! deraadt     1: /*     $OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $   */
1.2       niklas      2:
1.1       deraadt     3: #ifndef lint
1.17    ! deraadt     4: static const char     rcsid[] = "$OpenBSD: inp.c,v 1.16 2003/07/22 17:18:49 otto Exp $";
        !             5: #endif /* not lint */
1.1       deraadt     6:
1.16      otto        7: #include <sys/types.h>
                      8: #include <sys/file.h>
                      9: #include <sys/stat.h>
                     10:
                     11: #include <ctype.h>
                     12: #include <libgen.h>
                     13: #include <limits.h>
                     14: #include <stdlib.h>
                     15: #include <string.h>
                     16: #include <unistd.h>
                     17:
1.1       deraadt    18: #include "EXTERN.h"
                     19: #include "common.h"
                     20: #include "util.h"
                     21: #include "pch.h"
                     22: #include "INTERN.h"
                     23: #include "inp.h"
                     24:
1.7       espie      25:
1.1       deraadt    26: /* Input-file-with-indexable-lines abstract type */
                     27:
1.12      deraadt    28: static off_t   i_size;         /* size of the input file */
                     29: static char    *i_womp;        /* plan a buffer for entire file */
                     30: static char    **i_ptr;        /* pointers to lines in i_womp */
                     31:
                     32: static int     tifd = -1;      /* plan b virtual string array */
                     33: static char    *tibuf[2];      /* plan b buffers */
                     34: static LINENUM tiline[2] = {-1, -1};   /* 1st line in each buffer */
                     35: static LINENUM lines_per_buf;  /* how many lines per buffer */
                     36: static int     tireclen;       /* length of records in tmp file */
1.1       deraadt    37:
1.16      otto       38: static bool    rev_in_string(char *);
                     39: static bool    plan_a(char *); /* returns false if insufficient memory */
                     40: static void    plan_b(char *);
1.11      deraadt    41:
1.1       deraadt    42: /* New patch--prepare to edit another file. */
                     43:
                     44: void
1.11      deraadt    45: re_input(void)
1.1       deraadt    46: {
1.12      deraadt    47:        if (using_plan_a) {
                     48:                i_size = 0;
1.16      otto       49:                free(i_ptr);
                     50:                free(i_womp);
                     51:                i_womp = NULL;
                     52:                i_ptr = NULL;
1.12      deraadt    53:        } else {
                     54:                using_plan_a = TRUE;    /* maybe the next one is smaller */
                     55:                close(tifd);
                     56:                tifd = -1;
                     57:                free(tibuf[0]);
                     58:                free(tibuf[1]);
1.16      otto       59:                tibuf[0] = tibuf[1] = NULL;
1.12      deraadt    60:                tiline[0] = tiline[1] = -1;
                     61:                tireclen = 0;
                     62:        }
1.1       deraadt    63: }
                     64:
                     65: /* Constuct the line index, somehow or other. */
                     66:
                     67: void
1.12      deraadt    68: scan_input(char *filename)
1.1       deraadt    69: {
1.12      deraadt    70:        if (!plan_a(filename))
                     71:                plan_b(filename);
                     72:        if (verbose) {
                     73:                say("Patching file %s using Plan %s...\n", filename,
                     74:                    (using_plan_a ? "A" : "B"));
                     75:        }
1.1       deraadt    76: }
                     77:
                     78: /* Try keeping everything in memory. */
                     79:
1.16      otto       80: static bool
1.12      deraadt    81: plan_a(char *filename)
1.1       deraadt    82: {
1.12      deraadt    83:        int     ifd, statfailed;
                     84:        char    *s, lbuf[MAXLINELEN];
                     85:        LINENUM iline;
1.1       deraadt    86:
1.12      deraadt    87:        if (!filename || *filename == '\0')
                     88:                return FALSE;
1.8       millert    89:
1.1       deraadt    90:        statfailed = stat(filename, &filestat);
1.12      deraadt    91:        if (statfailed && ok_to_create_file) {
1.1       deraadt    92:                if (verbose)
1.12      deraadt    93:                        say("(Creating file %s...)\n", filename);
                     94:
                     95:                /*
                     96:                 * in check_patch case, we still display `Creating file' even
                     97:                 * though we're not. The rule is that -C should be as similar
                     98:                 * to normal patch behavior as possible
                     99:                 */
                    100:                if (check_only)
                    101:                        return TRUE;
                    102:                makedirs(filename, TRUE);
                    103:                close(creat(filename, 0666));
                    104:                statfailed = stat(filename, &filestat);
                    105:        }
                    106:        if (statfailed && check_only)
                    107:                fatal("%s not found, -C mode, can't probe further\n", filename);
                    108:        /* For nonexistent or read-only files, look for RCS or SCCS versions.  */
                    109:        if (statfailed ||
                    110:            /* No one can write to it.  */
                    111:            (filestat.st_mode & 0222) == 0 ||
                    112:            /* I can't write to it.  */
                    113:            ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) {
1.16      otto      114:                char    *cs = NULL, *filebase, *filedir;
1.12      deraadt   115:                struct stat     cstat;
                    116:
                    117:                filebase = basename(filename);
                    118:                filedir = dirname(filename);
                    119:
                    120:                /* Leave room in lbuf for the diff command.  */
                    121:                s = lbuf + 20;
                    122:
                    123: #define try(f, a1, a2, a3) \
                    124:        (snprintf(s, sizeof lbuf - 20, f, a1, a2, a3), stat(s, &cstat) == 0)
                    125:
                    126:                if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
                    127:                    try("%s/RCS/%s%s", filedir, filebase, "") ||
                    128:                    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
                    129:                        snprintf(buf, sizeof buf, CHECKOUT, filename);
                    130:                        snprintf(lbuf, sizeof lbuf, RCSDIFF, filename);
                    131:                        cs = "RCS";
                    132:                } else if (try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) ||
                    133:                    try("%s/%s%s", filedir, SCCSPREFIX, filebase)) {
                    134:                        snprintf(buf, sizeof buf, GET, s);
                    135:                        snprintf(lbuf, sizeof lbuf, SCCSDIFF, s, filename);
                    136:                        cs = "SCCS";
                    137:                } else if (statfailed)
                    138:                        fatal("can't find %s\n", filename);
                    139:                /*
                    140:                 * else we can't write to it but it's not under a version
                    141:                 * control system, so just proceed.
                    142:                 */
                    143:                if (cs) {
                    144:                        if (!statfailed) {
                    145:                                if ((filestat.st_mode & 0222) != 0)
                    146:                                        /* The owner can write to it.  */
                    147:                                        fatal("file %s seems to be locked "
                    148:                                            "by somebody else under %s\n",
                    149:                                            filename, cs);
                    150:                                /*
                    151:                                 * It might be checked out unlocked.  See if
                    152:                                 * it's safe to check out the default version
                    153:                                 * locked.
                    154:                                 */
                    155:                                if (verbose)
                    156:                                        say("Comparing file %s to default "
                    157:                                            "%s version...\n",
                    158:                                            filename, cs);
                    159:                                if (system(lbuf))
                    160:                                        fatal("can't check out file %s: "
                    161:                                            "differs from default %s version\n",
                    162:                                            filename, cs);
                    163:                        }
                    164:                        if (verbose)
                    165:                                say("Checking out file %s from %s...\n",
                    166:                                    filename, cs);
                    167:                        if (system(buf) || stat(filename, &filestat))
                    168:                                fatal("can't check out file %s from %s\n",
                    169:                                    filename, cs);
                    170:                }
                    171:        }
                    172:        filemode = filestat.st_mode;
                    173:        if (!S_ISREG(filemode))
                    174:                fatal("%s is not a normal file--can't patch\n", filename);
                    175:        i_size = filestat.st_size;
                    176:        if (out_of_mem) {
                    177:                set_hunkmax();  /* make sure dynamic arrays are allocated */
                    178:                out_of_mem = FALSE;
                    179:                return FALSE;   /* force plan b because plan a bombed */
                    180:        }
1.16      otto      181:        if (i_size > SIZE_MAX - 2)
                    182:                fatal("block too large to allocate");
                    183:        i_womp = malloc((size_t)(i_size + 2));
                    184:        if (i_womp == NULL)
1.12      deraadt   185:                return FALSE;
                    186:        if ((ifd = open(filename, O_RDONLY)) < 0)
                    187:                pfatal("can't open file %s", filename);
1.16      otto      188:
1.12      deraadt   189:        if (read(ifd, i_womp, (size_t) i_size) != i_size) {
                    190:                close(ifd);     /* probably means i_size > 15 or 16 bits worth */
                    191:                free(i_womp);   /* at this point it doesn't matter if i_womp was */
                    192:                return FALSE;   /* undersized. */
                    193:        }
1.16      otto      194:
1.12      deraadt   195:        close(ifd);
                    196:        if (i_size && i_womp[i_size - 1] != '\n')
                    197:                i_womp[i_size++] = '\n';
                    198:        i_womp[i_size] = '\0';
                    199:
                    200:        /* count the lines in the buffer so we know how many pointers we need */
                    201:
                    202:        iline = 0;
                    203:        for (s = i_womp; *s; s++) {
                    204:                if (*s == '\n')
                    205:                        iline++;
                    206:        }
1.16      otto      207:
1.13      deraadt   208:        i_ptr = (char **) malloc((iline + 2) * sizeof(char *));
1.16      otto      209:
                    210:        if (i_ptr == NULL) {    /* shucks, it was a near thing */
1.12      deraadt   211:                free((char *) i_womp);
                    212:                return FALSE;
                    213:        }
                    214:        /* now scan the buffer and build pointer array */
                    215:
                    216:        iline = 1;
                    217:        i_ptr[iline] = i_womp;
                    218:        for (s = i_womp; *s; s++) {
                    219:                if (*s == '\n')
                    220:                        i_ptr[++iline] = s + 1; /* these are NOT null
                    221:                                                 * terminated */
                    222:        }
                    223:        input_lines = iline - 1;
1.11      deraadt   224:
1.12      deraadt   225:        /* now check for revision, if any */
1.1       deraadt   226:
1.16      otto      227:        if (revision != NULL) {
1.12      deraadt   228:                if (!rev_in_string(i_womp)) {
                    229:                        if (force) {
                    230:                                if (verbose)
                    231:                                        say("Warning: this file doesn't appear "
                    232:                                            "to be the %s version--patching anyway.\n",
                    233:                                            revision);
                    234:                        } else if (batch) {
                    235:                                fatal("this file doesn't appear to be the "
                    236:                                    "%s version--aborting.\n",
                    237:                                    revision);
                    238:                        } else {
                    239:                                ask("This file doesn't appear to be the "
                    240:                                    "%s version--patch anyway? [n] ",
                    241:                                    revision);
                    242:                                if (*buf != 'y')
                    243:                                        fatal("aborted\n");
                    244:                        }
                    245:                } else if (verbose)
                    246:                        say("Good.  This file appears to be the %s version.\n",
                    247:                            revision);
                    248:        }
                    249:        return TRUE;            /* plan a will work */
1.1       deraadt   250: }
                    251:
                    252: /* Keep (virtually) nothing in memory. */
                    253:
1.16      otto      254: static void
1.12      deraadt   255: plan_b(char *filename)
1.1       deraadt   256: {
1.12      deraadt   257:        FILE    *ifp;
                    258:        int     i = 0, maxlen = 1;
1.16      otto      259:        bool    found_revision = (revision == NULL);
1.12      deraadt   260:
                    261:        using_plan_a = FALSE;
1.16      otto      262:        if ((ifp = fopen(filename, "r")) == NULL)
1.12      deraadt   263:                pfatal("can't open file %s", filename);
                    264:        (void) unlink(TMPINNAME);
                    265:        if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
                    266:                pfatal("can't open file %s", TMPINNAME);
1.16      otto      267:        while (fgets(buf, sizeof buf, ifp) != NULL) {
                    268:                if (revision != NULL && !found_revision && rev_in_string(buf))
1.12      deraadt   269:                        found_revision = TRUE;
                    270:                if ((i = strlen(buf)) > maxlen)
                    271:                        maxlen = i;     /* find longest line */
                    272:        }
1.16      otto      273:        if (revision != NULL) {
1.12      deraadt   274:                if (!found_revision) {
                    275:                        if (force) {
                    276:                                if (verbose)
                    277:                                        say("Warning: this file doesn't appear "
                    278:                                            "to be the %s version--patching anyway.\n",
                    279:                                            revision);
                    280:                        } else if (batch) {
                    281:                                fatal("this file doesn't appear to be the "
                    282:                                    "%s version--aborting.\n",
                    283:                                    revision);
                    284:                        } else {
                    285:                                ask("This file doesn't appear to be the %s "
                    286:                                    "version--patch anyway? [n] ",
                    287:                                    revision);
                    288:                                if (*buf != 'y')
                    289:                                        fatal("aborted\n");
                    290:                        }
                    291:                } else if (verbose)
                    292:                        say("Good.  This file appears to be the %s version.\n",
                    293:                            revision);
                    294:        }
1.16      otto      295:        fseek(ifp, 0L, SEEK_SET);       /* rewind file */
1.12      deraadt   296:        lines_per_buf = BUFFERSIZE / maxlen;
                    297:        tireclen = maxlen;
1.13      deraadt   298:        tibuf[0] = malloc(BUFFERSIZE + 1);
1.16      otto      299:        if (tibuf[0] == NULL)
1.12      deraadt   300:                fatal("out of memory\n");
1.13      deraadt   301:        tibuf[1] = malloc(BUFFERSIZE + 1);
1.16      otto      302:        if (tibuf[1] == NULL)
1.12      deraadt   303:                fatal("out of memory\n");
                    304:        for (i = 1;; i++) {
                    305:                if (!(i % lines_per_buf))       /* new block */
                    306:                        if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    307:                                pfatal("can't write temp file");
                    308:                if (fgets(tibuf[0] + maxlen * (i % lines_per_buf),
1.16      otto      309:                    maxlen + 1, ifp) == NULL) {
1.12      deraadt   310:                        input_lines = i - 1;
                    311:                        if (i % lines_per_buf)
                    312:                                if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
                    313:                                        pfatal("can't write temp file");
                    314:                        break;
                    315:                }
                    316:        }
                    317:        fclose(ifp);
                    318:        close(tifd);
                    319:        if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
                    320:                pfatal("can't reopen file %s", TMPINNAME);
1.1       deraadt   321: }
                    322:
1.12      deraadt   323: /*
                    324:  * Fetch a line from the input file, \n terminated, not necessarily \0.
                    325:  */
1.1       deraadt   326: char *
1.12      deraadt   327: ifetch(LINENUM line, int whichbuf)
1.1       deraadt   328: {
1.12      deraadt   329:        if (line < 1 || line > input_lines) {
                    330:                say("No such line %ld in input file, ignoring\n", line);
1.15      otto      331:                return "";
1.12      deraadt   332:        }
                    333:        if (using_plan_a)
                    334:                return i_ptr[line];
1.1       deraadt   335:        else {
1.12      deraadt   336:                LINENUM offline = line % lines_per_buf;
                    337:                LINENUM baseline = line - offline;
                    338:
                    339:                if (tiline[0] == baseline)
                    340:                        whichbuf = 0;
                    341:                else if (tiline[1] == baseline)
                    342:                        whichbuf = 1;
                    343:                else {
                    344:                        tiline[whichbuf] = baseline;
1.16      otto      345:
1.12      deraadt   346:                        lseek(tifd, (off_t) (baseline / lines_per_buf *
1.16      otto      347:                            BUFFERSIZE), SEEK_SET);
                    348:
1.12      deraadt   349:                        if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
                    350:                                pfatal("error reading tmp file %s", TMPINNAME);
                    351:                }
                    352:                return tibuf[whichbuf] + (tireclen * offline);
1.1       deraadt   353:        }
                    354: }
                    355:
1.12      deraadt   356: /*
                    357:  * True if the string argument contains the revision number we want.
                    358:  */
1.16      otto      359: static bool
1.12      deraadt   360: rev_in_string(char *string)
1.1       deraadt   361: {
1.12      deraadt   362:        char    *s;
                    363:        int     patlen;
1.1       deraadt   364:
1.16      otto      365:        if (revision == NULL)
1.12      deraadt   366:                return TRUE;
                    367:        patlen = strlen(revision);
                    368:        if (strnEQ(string, revision, patlen) && isspace(string[patlen]))
                    369:                return TRUE;
                    370:        for (s = string; *s; s++) {
                    371:                if (isspace(*s) && strnEQ(s + 1, revision, patlen) &&
                    372:                    isspace(s[patlen + 1])) {
                    373:                        return TRUE;
                    374:                }
1.1       deraadt   375:        }
1.12      deraadt   376:        return FALSE;
1.1       deraadt   377: }