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